allthingssecurity opened a new pull request, #29998: URL: https://github.com/apache/camel/pull/29998
# Description [CAMEL-26915](https://issues.apache.org/jira/browse/CAMEL-26915) Follow-up of #29788 (CAMEL-26884) and #29042 (CAMEL-26462): the gap both left open, a token per start of the consumer. `NatsConsumer` subscribes on a task of its executor, and the task checked `isRunAllowed()` to see whether the consumer stopped meanwhile. That is the state of the consumer, not of one start of it. When the same consumer is stopped and started again while the task of the first start is still in its subscribe, the old task saw the consumer running when its subscribe returned: it kept its subscription and wrote `dispatcher`, `dispatcherHandler` and `jetStreamSubscription` over those of the new start (and a pull task ran its fetch loop on its own subscription). The next stop then ended only what the fields pointed at, so a dispatcher or subscription of one of the two starts was never ended, and with a shared connection the route could receive each message twice. Where the same consumer is stopped and started: a route policy that stops the consumer of its route and starts it again, such as `ThrottlingInflightRoutePolicy` and `ThrottlingExceptionRoutePolicy` (`suspendOrStopConsumer` stops the NATS consumer, as it is not suspendable), and the Hazelcast, Quartz (`ScheduledRoutePolicy`) and DNS route policies. A route that is stopped and started again, or suspended and resumed (`suspendRoute` stops a route whose consumer is not suspendable), gets a new consumer and was never affected; the JIRA names a route restart as the example, which is not one of these cases. The window is a subscribe in flight across the stop and the start (for JetStream, a request to the server). Not seen in production. Change, in `NatsConsumer`: - each start creates a run (`NatsConsumer.Run`) that holds what its task makes: the dispatcher and its handler, the JetStream subscription, the push consumer keeper, and the executor of that start. The task works on the run of its start and asks `run.isCurrent()` (this run is the current one and the consumer runs) where it asked `isRunAllowed()`; - a task whose run is no longer current ends what it made itself, as it did for a stop: it closes a dispatcher it made before any subscribe, ends a pull subscription with the interrupt of the stop cleared for the unsubscribe (as in CAMEL-26884), stops its keeper, and its pull fetch loop ends; - `doStop` clears the current run before it reads what the task made (all fields volatile; the task writes each field before it checks the run), so either the task sees the stop, or the stop sees what the task made, or both, and both end it, as before; - `NatsJetStreamPushConsumerKeeper` takes a `BooleanSupplier` for whether its run is current (instead of `consumer.isRunAllowed()`), and uses the executor of its run, so a keeper of an old start does not act after the next start. This also covers the second point of the JIRA (after the task of a start gave up, the fields kept pointing at its closed dispatcher until the next start set them): the fields belong to the run of that start now. Tests, new `NatsConsumerRestartTest` (fake connections, no server, as the other `NatsConsumer*Test` classes; the consumer of the route is stopped and started with `ServiceHelper`, as a route policy does): - `pullSubscriptionOfTheStoppedStartIsEndedAndTheNewOneIsKept`: the pull subscribe of the first start returns after the stop and the next start: its subscription is ended (not on an interrupted thread) and never fetched from, the subscription of the new start is kept, and the next stop ends it; - `dispatcherOfTheStoppedStartIsClosedAndTheNewOneIsKept`: the first start gets its dispatcher only after the stop and the next start (JetStream push): that dispatcher is closed, the subscription of the new start is kept and still consumes, and the next stop ends it and closes its dispatcher. `NatsConsumerReplyTest` creates the consuming task directly and now passes a run. Without the change both new tests fail (the subscription and the dispatcher of the stopped start are never ended); with it the camel-nats unit tests pass (46 run, 0 failures). The ITs need a NATS container and were not run locally (CI runs them). No documentation or upgrade guide change. Backport: main only; the code builds on CAMEL-26884 and CAMEL-26462, which are on main only. # Target - [x] I checked that the commit is targeting the correct branch (Camel 4 uses the `main` branch) # Tracking - [x] If this is a large change, bug fix, or code improvement, I checked there is a [JIRA issue](https://issues.apache.org/jira/browse/CAMEL) filed for the change (usually before you start working on it): [CAMEL-26915](https://issues.apache.org/jira/browse/CAMEL-26915). # Apache Camel coding standards and style - [x] I checked that each commit in the pull request has a meaningful subject line and body. - [ ] I have run `mvn clean install -DskipTests` locally from root folder and I have committed all auto-generated changes. (I built camel-nats and the core modules with the formatter and import-sort plugins and ran the camel-nats unit tests. I did not run the full root build.) # AI-assisted contributions - [x] If this PR includes AI-generated code, commits have proper co-authorship attribution (e.g., `Co-authored-by` trailers) and the PR description identifies the AI tool used. This PR was prepared with Claude Code (Claude Opus 5.5). The commit carries a `Co-Authored-By` trailer. _Claude Code on behalf of allthingssecurity_ 🤖 Generated with [Claude Code](https://claude.com/claude-code) -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
