allthingssecurity opened a new pull request, #30921: URL: https://github.com/apache/camel/pull/30921
# Description [CAMEL-27043](https://issues.apache.org/jira/browse/CAMEL-27043) With `manualAcksEnabled=true`, QoS 1, `cleanStart=false` and `sessionExpiryInterval` greater than 0, a failed exchange makes the consumer disconnect and connect its own client again after the backoff delay (CAMEL-25793), so that the broker sends the message again. The route is synchronous by default, so the exchange runs on the callback thread of Paho. When another message was still in the route at that moment (a route step slower than the reconnect delay, 1 second for the first failure), the connect never completed. The root defect is in the thread lifecycle of Paho 1.2.5, read in its sources: - `MqttClient.disconnect` quiesces only the message queue of the callback, which is already empty: the callback thread takes a message off the queue before it handles it. - `CommsCallback.stop()` sets its target state to STOPPED and returns at once (its `isRunning()` is false as soon as the target is STOPPED), without waiting for the thread that is still in `messageArrived`. - The next connect of the same client (`ClientComms.ConnectBG`) calls `CommsCallback.start()` before it sends the CONNECT. `start()` starts a thread only when the current state is STOPPED, which it is not while the old thread is in the route, and then sleeps in `while (!isRunning())` forever: nothing sets the target state back to RUNNING. `CommsSender` has the same start/stop pattern, and an earlier reproduction of ours hung there instead. - The reconnect thread of the consumer waits on the connect token without a time limit. On Artemis 2.57 it does not stay a hang. The broker closes a socket that sends no CONNECT within 10 seconds, the consumer's catch schedules another reconnect and that one connects, and the stuck connect then sends its stale CONNECT on the new connection, so the broker disconnects the client with Session taken over (0x8E). Each time the message in the route lost its PUBACK, was sent again, was in the route at the next reconnect, and the next connect hung again, also after the failed message itself had been acknowledged. In our runs, after about 45 seconds Artemis deleted the pending messages of the subscription queue and the consumer stayed connected with no deliveries: route Started, `isConnected()` true, messages lost. Paho cannot be changed here, so Camel now avoids triggering the defect. The reconnect after a failed exchange no longer connects the same `MqttClient` again: it disconnects it, closes it, and connects a new client with the same client id, which resumes the session that the broker kept. A new client has its own callback and sender threads, so its connect does not wait for the threads of the old connection. In `PahoMqtt5Consumer.reconnect`: - the client is closed (with the forced close of CAMEL-27041 when needed) before the new one is created. With file persistence both clients use the directory of the client id, and Paho 1.2.5 does not fail when another client holds its lock (`MqttDefaultFilePersistence.open` ignores a failed lock), so the old client is done with it first; - the callback is set on the new client before its connect, as at start (the broker sends the messages of the session right after CONNACK); - exchanges still in the route on the closed client are not acknowledged (the broker sends their messages again to the new client) and do not reset the backoff, the same rule as for an exchange that completes after a stop (CAMEL-27040). A client is marked as replaced before it is closed, so an exchange is never acknowledged on a closed client (Paho fails such an acknowledgement with a `NullPointerException`); - a lock orders the replacement against the stop and against the acknowledgement of an exchange. `doStop` reads the client under it once `isRunAllowed()` is false, so either the stop closes the new client, or the reconnect sees the stop and closes the new client itself without connecting it: no left-over client takes the session from the next start (CAMEL-27041). The stop now also sets the client to null under the lock, which closes the same small window for an exchange that completes while the consumer stops; - `rollback` schedules the reconnect under the same lock, so a reconnect is always scheduled for the client that is current, and a failed connect or a failed creation of the new client is retried with the backoff, only for the current client; - the `ownReconnect` flag is gone. It was there because Paho reports every connect of a client that once lost its connection as a reconnect. The first connect of a new client is not a reconnect, so `connectComplete` subscribes again only after Paho's own automatic reconnect, as before. Design choices and trade-offs: - Waiting for the callback thread before the connect (option a) does not work on its own: we tried a guard that joins the callback thread first, and the connect still hung, then in `CommsSender.start`, because the sender thread of the old connection is still exiting too. It would also hold the reconnect for as long as the slow exchange takes. - A timeout on the connect token (option c) would not avoid the defect, only retry it, and a timed-out connect that goes on in the background is exactly what sends the stale CONNECT. With a new client there is no old thread for the connect to wait for, so no timeout is added. A broker that accepts the TCP connection and never answers the CONNECT keeps the reconnect waiting, as it does at start. - What users can see: `getClient()` of the consumer returns the new instance after such a reconnect, and an exchange that was in the route at the reconnect is no longer acknowledged on the next connection, so the broker sends its message again and it is routed twice, the second time possibly while the first exchange is still running (QoS 1 is at-least-once; before, its PUBACK could go out on the next connection, and with a broker that keeps packet ids it also acknowledged the copy sent again). Both are said on the component page and folded into the existing 4.24 upgrade-guide section of CAMEL-25793, which is not released yet. - With the default memory persistence the new client starts with empty Paho state, as after a route restart. The consumer has nothing outgoing to keep, and Paho completes the QoS 2 flow of a received message on its own. - A client set with the `client` option is never replaced: the consumer does not reconnect such a client after a failed exchange (unchanged), so only the client that the consumer created for itself is closed and created again. Not changed: Paho's own automatic reconnect after a lost connection reconnects the same client and can still hang the same way while a message is in a synchronous route; only Paho can fix that. The deprecated camel-paho (MQTT 3) consumer has no reconnect after a failed exchange. Tests: - new `PahoMqtt5ConsumerReconnectWhileRoutingTest`: the real Paho 1.2.5 client against the embedded Artemis MQTT broker of the module's tests (no Docker). A synchronous route fails the first delivery of `fail-1` and holds the first delivery of `slow-2` on a latch. While `slow-2` is still held, the consumer must route `fail-1` and `slow-2` again, and then a new message `after-3`; the closed client must refuse a `connect()` with 32111 (client closed); once `slow-2` is released, the consumer is still connected, the backoff is 0, and the subscription queue at the broker is empty, so nothing is left unacknowledged. On main it fails after 15 seconds, with nothing routed again. - new `PahoMqtt5ConsumerReconnectNewClientTest`, with mocked clients: the old client is disconnected and closed before the new one is created and connected, with manual acks and the callback set; an exchange of the replaced client that completes is acknowledged on neither client and leaves the backoff alone, and one that fails schedules no reconnect; a stop while the new client is being created closes it and never connects it; a failed creation of the new client (what Paho throws when the file persistence directory cannot be written) and a failed connect of the new client are both retried. All six fail on main. - `PahoMqtt5SubscribeRefusedTest` (its client factory returns the same mock every time) now counts the close of the reconnect. - Not part of the PR: a local probe against the same broker made 100 such reconnects in a row (and 20 with file persistence, and 15 with a slow message in the route at each reconnect). Each time a new client took over, the old client's Paho threads ended, the thread count stayed flat, and no message was left unacknowledged. - The DST world of the consumer and its fake describe the change. The fake already keeps the callback state per client, so a new client does not hang there either. Without the change the seven new tests fail (the real-broker test after 15 seconds, the mock tests on their first wait); with it they pass, `PahoMqtt5ConsumerReconnectWhileRoutingTest` 20 of 20 runs and `PahoMqtt5ConsumerReconnectNewClientTest` 20 of 20 runs (120 tests), and the camel-paho-mqtt5 unit tests pass on current main (1096 run including the 1000 seeds of the DST world, 0 failures, 0 errors, 0 skipped). I built offline with the versions cached on my machine (Netty 4.2.18.Final, Caffeine 3.3.0, Jackson 2.23.0 and 3.2.3, Spring 7.0.9, Micrometer 1.17.2, Log4j 2.25.5, pooled-jms 3.2.5); Paho 1.2.5 and Artemis 2.57.0 are the versions of main. The ITs need Docker and were not run locally (CI runs them). # 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-27043](https://issues.apache.org/jira/browse/CAMEL-27043). # 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-paho-mqtt5 and the core modules with the formatter and import-sort plugins and ran the camel-paho-mqtt5 unit tests; the component page change is copied to the catalog. 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]
