Fix nil pointer dereference in Connect #7596
Open
+94
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 panicTestConnectNormalizesNilCallback: Verifies Connect() with nil callback doesn't panic on reconnectRelated 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)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:
This fix ensures the API handles nil gracefully, which is the expected behavior for optional callbacks in Go.
How This Has Been Tested
Screenshots (if appropriate)
Types of changes
Checklist