fix: prevent stale disconnect from marking reconnected broker offline#303
Merged
Conversation
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.
…ce (#131) The onDisconnect callback previously used separate ReleaseRuntimeBrokerConnection and UpdateRuntimeBrokerHeartbeat calls. When a broker disconnects and reconnects rapidly, the stale disconnect's offline stamp can clobber the new connection's online status because UpdateRuntimeBrokerHeartbeat has no session guard — it unconditionally overwrites status. Provider statuses are also clobbered and never restored by heartbeats, leaving the broker permanently invisible until hub restart. Add ReleaseAndMarkBrokerOffline which atomically clears affinity AND stamps status=offline in a single CAS write. If a concurrent reconnect has already claimed the broker with a new session, the compare fails and the callback is a no-op. Also add a re-check guard before updating provider statuses.
9a7a7c1 to
1f903ff
Compare
6 tasks
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