You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by "shibd (via GitHub)" <gi...@apache.org> on 2026/05/18 01:49:55 UTC
[PR] [fix][cpp] Clear stale consumer connection after reconnect subscribe failure [pulsar-client-cpp]
shibd opened a new pull request, #577:
URL: https://github.com/apache/pulsar-client-cpp/pull/577
### Motivation
A consumer can be created successfully and run for some time, then later reconnect after a broker restart, bundle unload, or topic ownership change. During that reconnect, `ConsumerImpl::connectionOpened()` sets `connection_` and registers the consumer before sending the broker `SUBSCRIBE` command.
If that reconnect `SUBSCRIBE` is rejected, for example with an authorization error, `handleCreateConsumer()` treats the already-created consumer as retryable. However, the stale `connection_` remained set. The next reconnect timer then called `grabCnx()`, saw a non-null connection, returned early as "already connected", and stopped reconnecting. `isConnected()` could keep returning true because it only checks that `connection_` is non-null and the state is `Ready`, while broker stats show no active consumer.
### Changes
- Clear the stale consumer connection when an already-created consumer fails to re-subscribe during reconnect.
- Add a regression test that simulates a reconnect `SUBSCRIBE` rejection and verifies the stale connection is cleared and `isConnected()` returns false.
- Expose the internal `handleCreateConsumer()` path through `PulsarFriend` for the regression test.
### Validation
- `./build-support/docker-format.sh`
- Attempted `./build/tests/pulsar-tests --gtest_filter=ConsumerTest.testIsConnectedFalsePositiveAfterSubscribeRejectedOnReconnect`, but the local Pulsar service was not running on `localhost:6650`, so the test timed out during initial subscribe before reaching the regression path.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [fix][cpp] Clear stale consumer connection after reconnect subscribe failure [pulsar-client-cpp]
Posted by "BewareMyPower (via GitHub)" <gi...@apache.org>.
BewareMyPower commented on code in PR #577:
URL: https://github.com/apache/pulsar-client-cpp/pull/577#discussion_r3256071797
##########
lib/ConsumerImpl.cc:
##########
@@ -371,8 +371,9 @@ Result ConsumerImpl::handleCreateConsumer(const ClientConnectionPtr& cnx, Result
}
if (consumerCreatedPromise_.isComplete()) {
- // Consumer had already been initially created, we need to retry connecting in any case
+ // Clear the connection set before SUBSCRIBE so the next reconnect is not skipped.
LOG_WARN(getName() << "Failed to reconnect consumer: " << strResult(result));
+ resetCnx();
Review Comment:
The fix here is okay for consumer path. But a thorough solution is to reset the internal states of `HandlerBase` when `connectionOpened` fails.
See the Java client implementation:
1. https://github.com/apache/pulsar/blob/8f9f5b49d631e235e86d79e48a63722e74db4413/pulsar-client/src/main/java/org/apache/pulsar/client/impl/ConnectionHandler.java#L148
2. https://github.com/apache/pulsar/blob/8f9f5b49d631e235e86d79e48a63722e74db4413/pulsar-client/src/main/java/org/apache/pulsar/client/impl/ConnectionHandler.java#L173
3. https://github.com/apache/pulsar/blob/8f9f5b49d631e235e86d79e48a63722e74db4413/pulsar-client/src/main/java/org/apache/pulsar/client/impl/ConnectionHandler.java#L179-L180
The `cnx` is reset to null and `duringConnect` is reset to false if `connectionOpened` failed. This change is effectively same with `resetCnx(nullptr)` in C++ client
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [fix][cpp] Clear stale consumer connection after reconnect subscribe failure [pulsar-client-cpp]
Posted by "shibd (via GitHub)" <gi...@apache.org>.
shibd commented on code in PR #577:
URL: https://github.com/apache/pulsar-client-cpp/pull/577#discussion_r3256124989
##########
lib/ConsumerImpl.cc:
##########
@@ -371,8 +371,9 @@ Result ConsumerImpl::handleCreateConsumer(const ClientConnectionPtr& cnx, Result
}
if (consumerCreatedPromise_.isComplete()) {
- // Consumer had already been initially created, we need to retry connecting in any case
+ // Clear the connection set before SUBSCRIBE so the next reconnect is not skipped.
LOG_WARN(getName() << "Failed to reconnect consumer: " << strResult(result));
+ resetCnx();
Review Comment:
I checked `ProducerImpl` as well. In the producer path, `connection_` is not set until `handleCreateProducer(..., ResultOk)` is called, so a failed reconnect `PRODUCER` response will not leave a stale connection that could cause `grabCnx()` to return early. The consumer path is different because `connectionOpened()` sets `connection_` before the broker confirms `SUBSCRIBE`. For this reason, the cleanup is intentionally kept in `ConsumerImpl`.
I’ll merge this PR first. Letting the caller decide whether the connection needs to be reset makes the responsibility clearer.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] [fix][cpp] Clear stale consumer connection after reconnect subscribe failure [pulsar-client-cpp]
Posted by "shibd (via GitHub)" <gi...@apache.org>.
shibd merged PR #577:
URL: https://github.com/apache/pulsar-client-cpp/pull/577
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org