Skip to content

Conversation

@rembrandtreyes
Copy link

Description

This PR fixes a nil pointer dereference panic in recoverLoop that occurs when Connect() is called with a nil onConnect callback and a Redis reconnection event is triggered.

The fix normalizes a nil onConnect parameter to a no-op function at the API boundary in Connect(), preventing the panic in recoverLoop when it attempts to invoke the callback.

Changes:

  • storage/connection_handler.go: Add nil check in Connect() to normalize nil callback to func() {}
  • storage/connection_handler_test.go: Add two regression tests:
  • TestConnectWithNilOnConnect: Verifies recoverLoop processes reconnect signals without panic
  • TestConnectNormalizesNilCallback: Verifies Connect() with nil callback doesn't panic on reconnect

Related Issue

#7355

Motivation and Context

Several code paths call Connect() with a nil callback because they don't need reconnection notifications:

  • gateway/coprocess_api.go (production code)
  • Various test files

When Redis experiences a failover (e.g., Sentinel switching masters), statusCheck() detects the reconnection and sends a signal to recoverLoop(), which then attempts to call the nil callback, causing a panic:

panic: runtime error: invalid memory address or nil pointer dereference
goroutine 216 [running]:
github.com/TykTechnologies/tyk/storage.(*ConnectionHandler).recoverLoop(...)
    github.com/TykTechnologies/tyk/storage/connection_handler.go:122 +0x33

This fix ensures the API handles nil gracefully, which is the expected behavior for optional callbacks in Go.

How This Has Been Tested

  1. Unit tests: Added two new tests that verify nil callback handling
  2. Existing tests: All storage package tests pass (100+ tests)
  3. Linting: golangci-lint passes with 0 issues

Screenshots (if appropriate)

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Refactoring or add test (improvements in base code or adds test coverage to functionality)

Checklist

  • I ensured that the documentation is up to date
  • I explained why this PR updates go.mod in detail with reasoning why it's required
  • I would like a code coverage CI quality gate exception and have explained why

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant