allthingssecurity opened a new pull request, #30807: URL: https://github.com/apache/camel/pull/30807
# Description [CAMEL-27435](https://issues.apache.org/jira/browse/CAMEL-27435) Four problems of the connection recovery of the sjms consumer (`SimpleMessageListenerContainer`), all at the edges of its lifecycle. They follow CAMEL-25312, CAMEL-25612 and CAMEL-25659, which hardened the same mechanism; none of them covers these cases. 1. **A recovery can end with a started connection and no JMS consumer, and report success.** `refreshConnectionAndCountFailures` closes the old connection, which also closes its sessions and consumers, but leaves the `sessions` and `consumers` fields set. `initConsumers` creates nothing when `consumers` is set, and the success check of the recovery only asks whether `consumers` is null or whether a new failure was counted. So when a recovery starts while `consumers` still holds the consumers of the connection it is about to close, it ends with a new, started connection without consumers, cancels its schedule and returns success. Nothing starts another recovery: the route stays started and silently stops consuming until it is restarted. Two ways to get there: the connection fails between `createConnection` and `initConsumers` of `doStart` (the failure report finds no consumers to drop, schedules a recovery, and `doStart` then creates the consumers), or a failure report of the old c onnection arrives after the consumer was stopped and the same consumer instance is started again, as JMX does (see 2). 2. **A failure report after the stop starts a recovery that opens a connection and consumers that nothing closes.** `scheduleConnectionRecovery` did not look at the state of the container, and `doStop` sets the recovery pool and task to null, so a late `onException` (the provider calls it on its own thread) created a new pool and scheduled a recovery. After `doShutdown`, that recovery created a connection and its consumers (not started) that no lifecycle method closes any more. Right after a stop, the attempt still running in the old pool (`shutdown()` does not wait for it) and the new one could both be in flight. 3. **`doShutdown` read and cleared `connection` without `connectionLock`.** A recovery that replaced the connection between the close and the `null` left the new connection, with its consumers, open and unreferenced. 4. **An `Error` in a recovery attempt disabled the recovery for good.** `recoverConnection` catches `Exception` only. An `Error` from the client (for example a `NoClassDefFoundError` while it creates a session or consumer on the new connection) ends the schedule of the `BackgroundTask`, which still answers `isRunning()` (only `cancel()` clears it), so `scheduleConnectionRecovery` never scheduled a new recovery for any later failure. The new connection stays open (and started) without consumers, and when it fails later, nothing recovers it. Change (`SimpleMessageListenerContainer` only, no option or public API change): - `refreshConnectionAndCountFailures` sets `sessions` and `consumers` to null right after it closed the old connection, so `initConsumers` creates the consumers of the new connection (1). `refreshConnection()` does the same. - A new `recoveryAllowed` flag, guarded by `connectionLock`, is true from the start of `doStart` until the start of `doStop`. `onException` drops the consumers and schedules the recovery in one `connectionLock` region, and does neither when the flag is false; `scheduleConnectionRecovery` checks it too (the batch container calls it directly). A failure reported during `doStart` is still recovered (2). - A recovery attempt goes on only while it is the container's `recoverTask` (which `doStop` clears): it checks this together with the flag in the same `connectionLock` region as its refresh, and returns at once when it no longer is; its final check returns too, without touching the consumers, which `doShutdown` closes with the connection or which belong to the container started again since. So an attempt that was already running when the container stopped opens no connection after it, and does not cancel or drop the recovery of a later start (2). The schedule lambda now passes the task it scheduled instead of reading the `recoverTask` field when it runs. - `doShutdown` closes and clears the connection under `connectionLock`, and `doStop` shuts down the recovery pool and cancels the task under it (3). - `recoverConnection` catches `Error`, cancels its own task (which clears its running flag and removes it from the task registry), clears `recoverTask` if it is still that task, and rethrows, so `BackgroundTask` handles it as before (it logs it once and ends the task) and the next failure schedules a new recovery (4). When the `Error` comes from creating the connection itself, no connection is left that could report a later failure: as on main, the route needs a restart then. With 2 in place, a recovery can no longer run during `doShutdown`, so the lock of 3 is not needed by the container itself today: it keeps `doShutdown` consistent with every other writer of `connection` (and with a subclass that calls `refreshConnection()`), and the test of 3 passes on a version of this change without that lock (see Tests). Model: found with a TLA+ model of the recovery (failure reports that are late, repeated or arrive during the start, recovery attempts in old and new pools, stop, shutdown, failed start, restart of the same instance, suspend and resume, broker restarts, throws and an `Error` of the client). I added this change as a switch. With 2 consumers, one broker restart, one connection failure, one repeated report, one failed session and one suspend, the property "the consumers the container tracks end up on the started connection" is violated on main and holds with the change (769,784 distinct states, exhaustive); the safety invariants of the model hold in the same scope. With stop, shutdown and a restart of the same instance (1 consumer), on main "no open connection after the shutdown" and "every open connection is the one the container references" are violated, and the liveness property above too; with the change all three hold (56,505 distinct states). With an `Error` in a recovery attemp t, "no finished recovery task blocks later recoveries" is violated on main and holds with the change. TLC gives the same verdicts and state counts in the stop/restart and `Error` scopes. "At most one recovery task in flight" still fails in the stop/restart scope with the change: an attempt of the previous start that is preempted inside `initConsumers` for a whole stop, shutdown and start can still create its consumers, on the connection of the new start, where they are tracked; the other properties hold with that case included. Tests: new `SjmsConsumerRecoveryLifecycleTest` (package-private, JUnit assertions as the module, embedded broker as the other tests of the module, a connection factory that proxies and counts the connections, sessions and consumers; Awaitility and latches, no sleeps): - `failureReportedBeforeInitConsumersIsRecoveredWithConsumers` (1): the connection reports a failure as soon as the container sets its exception listener, before `doStart` created the consumers. On main the recovery ends with no consumer and a message sent afterwards is not received. - `lateFailureThenRestartKeepsConsumers` (1 and 2): the consumer is stopped, a failure report of the old connection arrives, and the same consumer instance is started again. On main the late report replaces the connection of the restart with one without consumers, and the next message is not received. - `lateFailureAfterStopOpensNoConnection` (2): a failure report after the route was stopped. On main a second connection with 2 consumers is left open. - `failureDuringShutdownLeavesNoConnectionOpen` (3): `doShutdown` is held in the close of the connection while the provider reports a failure on its own thread. On main a recovery creates a connection that stays open after the stop. - `errorInRecoveryDoesNotBlockLaterRecovery` (4): the first recovery attempt creates the new connection and gets a `NoClassDefFoundError` from its first `createSession`; the connection stays open without consumers. After that connection fails too, the consumer must receive again. On main nothing is recovered and the message is not received. All 5 fail on main. Without the nulling in the refresh only the first fails, without the `catch (Error)` only the last; without the lock in `doShutdown` all 5 pass (see above). With the change: camel-sjms 236 tests, 0 failures; the new class 20 times in one JVM, 100 run, 0 failures. Formatter and import-sort checks pass. Not changed (other results of the same model, low): - When the first `initConsumers` of `doStart` is still running a second after a failure report and a recovery replaces the connection under it, its last consumers are created on the new connection outside the tracked set, and the recovery adds its own: more consumers than `concurrentConsumers` run, and a stop does not close the extra ones (only the connection close does). The same can happen when an attempt of the previous start is still in `initConsumers` while a recovery of the next start replaces the connection. Needs a very slow consumer creation; not reproduced. # 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-27435](https://issues.apache.org/jira/browse/CAMEL-27435). # 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 the affected module, including the formatter and import-sort plugins. No option or generated file changed. 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 commits carry 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]
