allthingssecurity opened a new pull request, #30630: URL: https://github.com/apache/camel/pull/30630
# Description [CAMEL-27339](https://issues.apache.org/jira/browse/CAMEL-27339) `JmsProducer.process` and `SjmsProducer.process` check `isRunAllowed()` and then call `initReplyManager()` for an InOut exchange. The stop does not wait for a request that is between the two: `doStop` stops the reply manager and sets `started=false`. A request that passed the check just before the stop then finds `started=false`, takes the producer's lock (the same lock `BaseService.stop` holds, so it waits for the stop to end) and creates and starts a new reply manager for the stopped producer: a JMS connection and listener container (with a temporary queue by default), a timeout checker thread and an onTimeout thread pool. Nothing stops it. When the producer is started again, the next request finds `started=true`, uses that reply manager, and the following stop stops it, so the leak lasts until then; a producer that is not started again (a removed route, a stopped `ProducerTemplate`) leaks it for good. The exchange itself completes, so this is a leak of a connection and threads, not a lost exchange. The change: `initReplyManager` checks `isRunAllowed()` again once it holds the producer's lock, and fails the request with `RejectedExecutionException` (completed by the `catch` of `process()`, as for a producer that is not running) instead of creating a reply manager. As the check runs under the lock that `BaseService.stop` holds while it sets `STOPPING` and calls `doStop`, a request either creates the reply manager before the stop (which then stops it) or sees the producer stopping or stopped. The fast path without the lock (`started=true`) is unchanged. Same change in camel-jms and camel-sjms. This is independent of CAMEL-27321 (a separate PR in the same area, about the requests that wait for a reply while the reply manager stops): it touches only `initReplyManager` of the two producers, which that change does not, and the two merge cleanly in either order. `RocketMQProducer.initReplyManager` (camel-rocketmq) was copied from the same code and has the same gap (there the late request creates the reply manager and then fails on the `mqProducer` that the stop set to null); I left it for a follow-up so this PR stays in the JMS components. Found with a TLA+ model of the producer, its reply manager and the stop (the model of the CAMEL-27321 work, extended with this check). On main a request that passed the running check creates a reply manager after the stop (NoReplyManagerLeak, 38 distinct states). With the check it holds, and every exchange completes at most once: 2 producers 6,414 distinct states, 3 producers with a failed send and a forced executor shutdown 174,864, and with a temporary reply queue that loses its connection (including the bounded wait of the separate temporary reply queue fix) 663,533. Same counts with TLC and everystate. Tests: new `JmsProducerStopReplyManagerTest` (camel-jms) and `SjmsProducerStopReplyManagerTest` (camel-sjms), without a broker (a connection factory whose connections fail, and `createReplyManager` overridden to count the reply managers): - `testNoReplyManagerAfterStop`: the request waits where it reads the destination name header (after the running check, before `initReplyManager`), the producer is stopped, and the request continues: no reply manager must be created, and the exchange fails with `RejectedExecutionException`; - `testReplyManagerCreatedOnceWhileRunningAndAfterRestart` (control, passes without the change): a running producer creates its reply manager once, and again after a restart. On main `testNoReplyManagerAfterStop` fails in both modules (`reply managers created after the producer stopped ==> expected: <0> but was: <1>`); the control passes. With the change both pass in both modules. Each new class 10 times in one JVM on main with the change: 20 run, 0 failures. Module tests on main with the change: `camel-jms`: 522 run, 0 failures, 11 skipped (and its two other surefire executions, 16 and 6, 0 failures); `camel-sjms`: 228 run, 0 failures, 5 skipped. # 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-27339](https://issues.apache.org/jira/browse/CAMEL-27339). # 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 and tested `components/camel-jms` and `components/camel-sjms`, including the formatter and import-sort plugins. No option or generated file changes. 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]
