allthingssecurity opened a new pull request, #30830: URL: https://github.com/apache/camel/pull/30830
# Description [CAMEL-27506](https://issues.apache.org/jira/browse/CAMEL-27506) With `testConnectionOnStartup=false`, a consumer that cannot reach the server at start schedules a delayed start, which runs `doStart` again after `connectionPollDelay` seconds. `doStart` only schedules the next attempt when getting the connection throws a `SmackException` (the server is not reachable). Any other failure ends the delayed start for good: its task caught the exception, logged "Ignoring an exception caught in the startup connection poller thread." and nothing scheduled another attempt. The connection monitor is only started at the end of a successful start, so nothing else tried again either. Once the server was reachable again, this happened when: - the login failed with an `XMPPException`, such as SASL `temporary-auth-failure` while the authentication backend of the server is not up yet; - entering the room failed: `XMPPErrorException` (`service-unavailable` while the MUC service is not ready), or a `NoResponseException` / `NotConnectedException` when the connection dropped during the join. The route stayed Started and received nothing until it was restarted by hand, and the failure never reached the consumer's exception handler. A room consumer also stayed a user of the account's shared connection while it was not in its room. The option's documentation says the consumer polls for a connection until it is established. Change (`XmppConsumer`): - the delayed start runs its own method instead of `doStart`. It tries again after any failure, with the same delay (`connectionPollDelay`), until the consumer is stopped; a failure while the consumer is stopping does not schedule another attempt. - before the next attempt it releases what the failed attempt got: its connection listener, the room's message listener, its chat and stanza listeners, and its use of the shared connection (the connection is closed when no other consumer or producer of the account uses it). The next attempt starts from the beginning, and a consumer that is not started does not keep the account's session. - logging: the first failed attempt is logged at WARN with the stack trace and passed to the exception handler, the next ones at DEBUG (with the attempt count). A start that works after failed attempts logs one INFO line with the number of failed attempts. An `Error` is handled like an exception; a `VirtualMachineError` is thrown on (as in CAMEL-27469). - `XmppEndpoint.createConnection` closes (`instantShutdown()`) the new connection when connecting, creating the account or logging in fails, and throws the same exception as before. Smack keeps a connection whose login failed open (socket, reader and writer threads) so that the caller can log in again, but nobody else had a reference to it. Before, a failed login of the delayed start left one such connection; with the retries it would leave one per attempt, so the fix is part of this change. It also helps the producers, which create a connection for each exchange while they have none. `instantShutdown` rather than `disconnect`, because a connection that is not logged in would wait for the server to close the stream. - the part of `doStart` after getting the connection moved to a private `subscribe()` method, used by both starts. The synchronous start (`testConnectionOnStartup=true`, or the first attempt of the delayed mode) is unchanged. Behaviour for users: with `testConnectionOnStartup=false`, a consumer now keeps trying until it is started, also when the login or the room entry fails. A login that keeps failing (for example a wrong password) is tried again every `connectionPollDelay` seconds; the connection monitor already does the same for a running consumer whose login fails after a lost connection. While the server is not reachable, the delayed attempts after the first one log at DEBUG and are not passed to the exception handler again (before, each attempt logged a WARN and called the exception handler). No upgrade guide entry: this is a bug fix, and the only other change is the quieter log. The consumer has no health check of its own, so the route status stays the only health signal and still says Started while the delayed start retries; a consumer health check would be a separate improvement. Not changed here (found in the same review of the delayed start and the connection monitor, none reproduced): - a follow-up PR (CAMEL-27537, stacked on this one, opened once this one is merged) fixes a stop of the route while the delayed start waits for the account's lock: the delayed start then starts the stopped consumer. - a restart of the route while the delayed start of the previous start is still connecting: the failure path of the old attempt can release the new start's use of the shared connection. - the connection logged in again by the ReconnectionManager between the room join and `addConnectionListener` is not noticed, so the room is not entered again; and `rejoinRoom = false` after a rejoin can overwrite a newer `true`. Both windows are a few instructions long. I can file follow-ups for these. Tests: new `XmppConsumerDelayedStartRetryTest` (package-private), against the module's fake XMPP server (`FakeXmppServer`, which gets a constructor for a fixed port, so it can start where the consumer already tries to connect, and two switches: refuse the next room entries with `service-unavailable`, fail the next SASL logins with `temporary-auth-failure`, with counters, and the number of open client connections): - `roomIsEnteredWhenTheFirstEntryAfterTheDelayedStartWasRefused`: server down at start, then up with the first room entry refused: the consumer enters the room at its second attempt and receives a room message. - `chatIsConsumedWhenTheFirstLoginAfterTheDelayedStartFailed`: server down at start, then up with the first login failing: the consumer logs in at its second attempt and receives a chat message, and the connection whose login failed is closed (one open connection on the server). Both fail on main (no second attempt; checked by running the new class against the `XmppConsumer` and `XmppEndpoint` of main): 2 of 2 fail. With only the `XmppEndpoint` of main, the chat test fails: the connection whose login failed stays open. With the change: the new class passes 20 of 20 runs (40 tests); the camel-xmpp unit tests pass (34 run, 0 failures, 5 skipped: the Google Talk tests and the disabled robust-connection test). The integration tests need an XMPP server container and were not run locally (CI runs them). Found with a TLA+ model of the consumer's delayed start, connection monitor, room rejoin and the account's shared connection ("a Started consumer is subscribed or a retry is scheduled" fails after one failed join or login), 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-27506](https://issues.apache.org/jira/browse/CAMEL-27506). # 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]
