[fix][cpp] Clear stale consumer connection after reconnect subscribe failure#577
Merged
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes a bug where a C++ Pulsar consumer could end up permanently disconnected (while isConnected() falsely returns true) after a reconnect-time SUBSCRIBE rejection (e.g. authorization error). Previously handleCreateConsumer() returned ResultRetryable but left the stale connection_ set, causing the subsequent grabCnx() to short-circuit. The fix calls resetCnx() on the retryable-after-creation branch so reconnect can proceed.
Changes:
- Call
resetCnx()inConsumerImpl::handleCreateConsumer()when a reconnect SUBSCRIBE fails after the consumer was already initially created. - Add a regression test in
ConsumerTest.ccsimulating a reconnect SUBSCRIBE rejection. - Expose
handleCreateConsumer()to tests viaPulsarFriend::consumerHandleCreateConsumer().
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| lib/ConsumerImpl.cc | Adds resetCnx() call so a failed reconnect SUBSCRIBE clears the stale connection, allowing the reconnect timer to retry. |
| tests/PulsarFriend.h | Adds a static helper that exposes ConsumerImpl::handleCreateConsumer() for testing. |
| tests/ConsumerTest.cc | Adds regression test verifying connection_ is cleared and isConnected() returns false after reconnect SUBSCRIBE rejection. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
BewareMyPower
approved these changes
May 18, 2026
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.
Motivation
A consumer can be created successfully and consume messages for some time. Later, after a broker restart, namespace bundle unload, or topic ownership change, the client reconnects and sends a new broker-side
SUBSCRIBEcommand for the existing consumer.Before this fix, if that reconnect
SUBSCRIBEfailed, the local consumer could get stuck withisConnected() == trueeven though the broker no longer had an active consumer for the subscription.Failure path
The key issue is that
connectionOpened()setsconnection_before the broker confirms theSUBSCRIBE. If theSUBSCRIBEfails for an already-created consumer, the reconnect path returnsResultRetryable, but the staleconnection_must also be cleared. Otherwise the next reconnect attempt is skipped bygrabCnx()because it sees a non-null connection.Changes
SUBSCRIBErejection and verifies the stale connection is cleared andisConnected()returns false.handleCreateConsumer()path throughPulsarFriendfor the regression test.Validation
./build-support/docker-format.sh./build/tests/pulsar-tests --gtest_filter=ConsumerTest.testIsConnectedFalsePositiveAfterSubscribeRejectedOnReconnect, but the local Pulsar service was not running onlocalhost:6650, so the test timed out during initial subscribe before reaching the regression path.