fix: prevent stale disconnect from marking reconnected broker offline#303
Open
ptone wants to merge 2 commits into
Open
fix: prevent stale disconnect from marking reconnected broker offline#303ptone wants to merge 2 commits into
ptone wants to merge 2 commits into
Conversation
When a broker's control channel disconnects and reconnects in the same second, the old connection's deferred removeConnection was deleting the new connection from the map and firing the onDisconnect callback. This left onlineProviders=0 despite a live WebSocket session. Guard removeConnection with a pointer comparison: only remove the map entry and fire the disconnect callback when the connection being cleaned up is still the active one for that brokerID. If a reconnect has already replaced it, skip both — the new session is healthy. Fixes #131
Contributor
There was a problem hiding this comment.
Code Review
This pull request addresses a race condition where a rapid disconnect and reconnect of a broker's control channel could leave the broker permanently offline. The fix updates removeConnection to accept and verify the specific *BrokerConnection pointer, ensuring that stale deferred disconnects do not remove newly established connections. Corresponding unit tests have been added to verify this behavior. There are no review comments, and I have no additional feedback to provide.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
removeConnectiongoroutine races with the new connection. It removes the new connection from the map and firesonDisconnect, leavingonlineProviders=0despite a live WebSocket session.removeConnectionnow takes a*BrokerConnectionpointer and only removes the map entry / fires the disconnect callback when the passed connection is still the active one for thatbrokerID. Superseded connections are silently skipped.Fixes issue 131
Test plan
TestControlChannelManager_ReconnectDoesNotTriggerDisconnect— verifies a staleremoveConnectionfrom the old connection does NOT fireonDisconnector remove the new connectionTestControlChannelManager_StaleRemoveAfterReconnect_ThenRealDisconnect— verifies the full lifecycle: stale remove is skipped, then a real disconnect of the current connection fires the callback correctlyTestControlChannelManager_OnDisconnectCallbackand_NilSafeupdated and passingpkg/hub/...test suite passes