allthingssecurity opened a new pull request, #30915: URL: https://github.com/apache/camel/pull/30915
# Description [CAMEL-27537](https://issues.apache.org/jira/browse/CAMEL-27537) With `testConnectionOnStartup=false`, the delayed start of the consumer gets the account's shared connection: `XmppComponent.acquireConnection` adds the consumer to the connection's users, then waits for the account's lock, which another consumer or producer of the same account holds while it connects or logs in (for example to a slow server). `ReentrantLock.lock()` does not react to an interrupt. When the route was stopped while the delayed start waited there, `doStop` shut the executor down (`shutdownNow` does not wait for the running task) and had nothing to release, as the `connection` field was still null. When the delayed start got the lock, it went on with the stopped consumer: - a chat consumer finished its start: it registered its chat listener and connection listener, and `getExecutor()` created a new executor (the stop had cleared the field) with a connection monitor. The stopped route's consumer kept routing incoming chat messages into the route. - a room consumer was interrupted in the room join, so its start failed. Before CAMEL-27506 (#30830) it also stayed a user of the connection; that fix gives the connection back. So the stopped chat consumer stayed a user of the account's connection, so the connection was not closed when its other users released it, and the session stayed logged in on the server until the component stopped. This builds on the fix of CAMEL-27506 (#30830, already on main), which made the delayed start try again after any failure, in its own method. Before that fix, a delayed start that failed while the consumer was stopping also scheduled its next attempt on a new executor; that fix checks `isRunAllowed()` and whether the executor was shut down before it schedules, and this change makes the check and the scheduling one step under the stop's lock. Change (`XmppConsumer`): - both starts (`doStart`, and the delayed start's own method from CAMEL-27506) keep the connection they got in a local variable. Under a new lock (`startLock`) they check `isRunAllowed()`, and only then set the `connection` field, listen on the connection and enter the room (`subscribe()`), and, once that worked, start the connection monitor. When the consumer is stopping, they give the connection back (`endpoint.releaseConnection(this)`) and return. - a delayed start whose `subscribe()` fails releases what it got (listeners, room, its use of the connection) under the same lock, so a stop that waits for the lock finds nothing of that attempt left. - the next attempt of the delayed start (and the first one, from `scheduleDelayedStart`) is scheduled under the same lock, and only while the consumer is not stopping, so a delayed start that fails while the consumer stops creates no new executor and no further attempt. This replaces the check of CAMEL-27506 on the executor of the failed attempt (a `RejectedExecutionException` is still caught). The exception handler call for the first failure stays outside the lock. - `doStop` still shuts the executor down first, which cancels a pending delayed start and interrupts a running one (a room join ends with an `InterruptedException`). It then takes `startLock`: it waits for a delayed start that is subscribing at that moment and shuts down the executor that start created for its monitor. A delayed start that gets to the lock after the stop began sees that the consumer is stopping. The lock also makes the fields that the delayed start set visible to the stopping thread. - `super.doStop()` now runs after the executor is shut down instead of before. The synchronous start (`testConnectionOnStartup=true`, and the first attempt with `false`) takes the same lock without waiting for anyone and is otherwise unchanged; the lock is never held while waiting for the account's lock, nor while the exception handler runs. No upgrade guide entry: nothing changes for a route that is not stopped during a delayed start. Not changed here: - a restart of the route while the delayed start of the previous start is still connecting (for example a socket connect to a slow server): when that old attempt fails, `acquireConnection` releases the consumer from the connection's users, although the new start uses the connection (the users set holds the consumer once). That needs a change in the shared connection's bookkeeping; I can file a follow-up. - a restart of the route while the delayed start of the previous start still waits for the account's lock: when that old attempt gets the lock, the consumer is running again, so the old attempt subscribes a second time (a second connection monitor). This is the same on main. - the other leads found in the same review (the room not entered again when the connection logs in again between the room join and `addConnectionListener`, `rejoinRoom = false` overwriting a newer `true`) are not touched. Tests: new `XmppConsumerStopDuringDelayedStartTest` (package-private), against the module's fake XMPP server (`FakeXmppServer` gets a constructor for a fixed port, so it can start where the consumer already tries to connect). The server is down at start, then up; a second user of the account holds the account's lock until the delayed start waits for it; the route is stopped, the lock released and the second user released: - `chatConsumerStoppedWhileItsDelayedStartWaitsForTheLockIsNotStarted`: afterwards the account's connection has no user, the server has no session, no connectionPoll thread runs, the consumer holds no connection, the route is Stopped. - `roomConsumerStoppedWhileItsDelayedStartWaitsForTheLockIsNotStarted`: the same for a room consumer, which is not in its room. Results: the camel-xmpp module tests on this branch, rebased on main with CAMEL-27506, ran 36 tests with no failure (5 skipped, the same ones as on main). The new test class passed 10 runs out of 10 (20 of 20 tests), and the test class of CAMEL-27506 passed 10 runs out of 10 on this code as well. With main's `XmppConsumer`, which has the CAMEL-27506 fix, the chat test fails: after the stop the account's connection still has the consumer as a user, so the wait for no users times out after 10 seconds. The room test passes there, because that fix already gives the connection back when the interrupted room join fails, so the chat case is the one this change is needed for. Before CAMEL-27506, both new tests failed in the same way. Found with a TLA+ model of the consumer's delayed start and stop with a producer of the same account ("a stopped consumer holds no connection, listener or monitor" fails), and reproduced against the real consumer with the fake server. # 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-27537](https://issues.apache.org/jira/browse/CAMEL-27537). # 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-xmpp and the core modules offline with the formatter and import-sort plugins and ran the tests listed above. I did not run the full root build. No option or generated file changed.) # 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]
