Skip to content

fix(client): only deregister the broker registered under that ID - #3752

Merged
dnwe merged 1 commit into
IBM:mainfrom
hsdfat:fix-deregister-broker-3145
Sep 20, 2026
Merged

dnwe merged 1 commit into
IBM:mainfrom
hsdfat:fix-deregister-broker-3145

Conversation

@hsdfat

@hsdfat hsdfat commented Sep 19, 2026

Copy link
Copy Markdown
Contributor

deregisterBroker looks the broker up by ID only, so it can remove a different broker from the one it was given.

Every caller gets a broker from LeastLoadedBroker(), drops the client lock, sends a request, and on failure closes that broker and calls deregisterBroker(broker). In the meantime a metadata refresh or coordinator lookup can register a new *Broker under the same ID, for example:

  • updateBroker or registerBroker replaces it because its address changed. The old instance is closed.
  • checkBrokersHealth drops it after a socket error, and updateBroker registers the instance from the response in its place.

The caller's request fails because the old instance is closed or its socket is gone. Its deregisterBroker(old) then finds the ID in the map and deletes the new broker. That broker is never closed (Close and RefreshBrokers only close what is in the map), and the client has lost a live broker until the next refresh registers another one. This matches the leaks under unstable networks that the issue describes.

As suggested in the issue, deregisterBroker now removes the entry only when client.brokers[broker.ID()] == broker.

On whether a stale broker should fall through to the seed broker handling: it does, and nothing happens there. Seed brokers are only created by NewBroker in randomizeSeedBrokers (ID -1), and the brokers map only holds brokers decoded from Metadata and FindCoordinator responses. The two sets never share an instance, and the seed branch already compares broker == client.seedBrokers[0] by pointer, so a stale registered broker never matches it. Falling through leaves the rest of the function as it was.

TestDeregisterBroker replaces a broker through updateBroker, deregisters the old instance, and checks that the new one is still registered and the seed list is unchanged. It then checks that deregistering the registered instance still removes it. On main the first assertion fails because the map entry is gone.

Fixes #3145

deregisterBroker removed whichever broker was registered under the
failed broker's ID. If a metadata refresh had replaced that broker in
the meantime (its address changed, or it was dropped after a socket
error and registered again), a caller still holding the old instance
removed the new one instead. The new broker was then never closed and
the client lost track of it.

Only remove the broker if it is the instance that is registered. An
instance that is no longer registered falls through to the seed broker
check as before, which also compares by pointer, so it is left alone.

Fixes IBM#3145

Signed-off-by: phatlc <[email protected]>

@dnwe dnwe left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@dnwe dnwe added the fix label Sep 20, 2026
@dnwe
dnwe merged commit da8867a into IBM:main Sep 20, 2026
20 checks passed
@hsdfat
hsdfat deleted the fix-deregister-broker-3145 branch November 6, 2026 11:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Function deregisterBroker Maybe Delete Wrong Broker Without Close Broker

2 participants