allthingssecurity opened a new pull request, #30701: URL: https://github.com/apache/camel/pull/30701
# Description [CAMEL-27383](https://issues.apache.org/jira/browse/CAMEL-27383) Three cases in which the consumer of the new `camel-vertx-mqtt` component (CAMEL-26735) stops getting messages, or never gets them: 1. **A consumer stopped and started on the connection of its producer never reads again.** Stopping the consumer pauses the client (`stopReading()`), so that no message arrives while it drains. When a producer of the same endpoint still holds the connection, `releaseConnection()` does not stop it, and nothing resumed it: the connection keeps `paused`, and applies it to every new client too. The restarted consumer received nothing; the producer's QoS 1 and 2 publishes failed with "did not acknowledge the message within the ack timeout", as the PUBACK was never read; and as the PINGRESP was not read either, Vert.x dropped the connection every 1.5 keep alive intervals and the new client was paused again. This happens with a plain route stop and start (the consumer is not `Suspendable`, so a suspend is the same). The docs say that while such a consumer is stopped, the messages that arrive are acknowledged and dropped, which needs the client reading. 2. **The messages of a kept session sent right after the CONNACK are dropped.** The consumer posted `subscribe` (which sets the message handler) after `acquireConnection()` returned. A broker that kept the session (`cleanStart=false`) sends the messages it holds right after the CONNACK, and when the CONNACK is handled first, the publish handler finds no handler ("without a subscriber"). With automatic acknowledgements (the default) the Vert.x client then acknowledges the message anyway (`MqttClientImpl.handlePublish` of vertx-mqtt 5.2.3 sends the PUBACK before it calls the handler), so it is lost; with manual acknowledgements it stays in flight until a reconnect. Without `automaticReconnect`, `start()` waits for the CONNACK before the consumer can subscribe, so the messages are always dropped; with it, it is a race. This is the same as CAMEL-25944 for camel-paho. 3. **A message that failed on two connections is not delivered again.** A failed message (QoS 1, kept session, manual acknowledgements, `automaticReconnect`) is redelivered by closing the connection once the pending exchanges are done. The consumer kept only the client of the first failure, and `redeliver` does nothing unless that client is the current one. When the connection dropped while the message was being processed, the session sent it again on the new connection; the old copy failed first, so the consumer kept the old client, and when the new copy failed too, no close happened. The message stayed unacknowledged on the live connection, holding a broker in-flight slot, until the connection happened to drop. Change (`VertxMqttConsumer`, `VertxMqttConnection`, `VertxMqttEndpoint`): 1. The consumer resumes the connection in `doStop` when a producer still holds it: after it unsubscribed (and after its pending exchanges are done, as before) and gave the connection back (`releaseConnection()` now says whether the connection keeps running). Messages that arrive afterwards find no handler and are acknowledged and dropped, as the docs say. When the consumer is the last user, the connection stops still paused, as before: the messages waiting unread on the client are not acknowledged, and a kept session delivers them again after the next connect. (Resuming it before the stop would let the client read and acknowledge them, with automatic acknowledgements, and drop them.) 2. `VertxMqttEndpoint.acquireConnection(Consumer<VertxMqttConnection>)` gives the connection to the caller before it starts it (or the started one, when another user created it). The consumer takes its connection and subscribes there; `VertxMqttConnection.subscribe` before `start()` only records the subscription, and the first connect subscribes as before. The handler is then set before the CONNACK can be handled, and the consumer has its connection for the acknowledgements and the back pressure before the first message. The producer still uses `acquireConnection()`. 3. The consumer keeps every client a failed message arrived on, and `redeliver` closes the connection when one of them is the current client. A failure that only came from earlier connections needs no close: the session sends the message again on the current connection, as before. Model: found with a TLA+ model of the connection, the consumer and the broker session (reconnects, network drops, keep-alive drops of a paused client, failed exchanges, failed connects, a consumer stop and start, with and without a producer sharing the connection, manual acknowledgements and a kept session). I set the three changes as switches of the same model. On main it finds the consumer that stays paused after a restart on a shared connection (`NotStuckPaused`), and messages that are never processed (`EventuallyProcessed`: the kept-session messages, and the message that failed on two connections). With the three changes these hold (everystate, liveness with fairness on the code's own steps: 16,687 to 61,094 distinct states at up to 4 client attempts, 2 failures, a drop, a keep-alive drop, a failed connect and a restart; TLC gives the same counts on 5 configurations). Each change switched off alone fails again. The model has no messages waiting unread on a paused client, so it does not tell whether the last user may resume before the stop; the fifth test below covers that. The safety properties (a failed message is never acknowledged, at most one open client, the current client is the newest) hold on main and with the changes at the largest scope (2 messages, 4 attempts, 2 failures, a restart: 36,126,209 distinct states without the producer, 34,439,343 with it). Tests: new `VertxMqttConsumerSessionTest`, on the real Vert.x MQTT client, with the module's `TestBroker` for the first case and a broker on the Vert.x MQTT server for the others (where a session is kept, it answers with session present and sends the message of the session): - the consumer is stopped and started while the producer keeps the connection: a QoS 1 publish of the producer must be acknowledged, and the consumer must receive a message again; - a broker with a kept session sends a QoS 1 message in the same flush as the CONNACK, `automaticReconnect=false`, with automatic and with manual acknowledgements: the consumer must receive it, and the broker must get one PUBACK; - the first copy of a message is processed while its connection drops, the session sends it again on the new connection, then both copies fail: the consumer must close the new connection, and the third copy is processed and acknowledged; - a consumer alone on its connection, `cleanStart=false`, `maxPendingMessages=1`, automatic acknowledgements: two messages wait unread while the first is processed, then the consumer stops, held where it gives the connection back. The two messages must not be acknowledged. On main the first 4 tests fail (ack timeout of the producer; nothing received; no third connection); the fifth passes on main, which never resumed at the stop, and guards the change of the first case. With the change, a Maven `test` run of the module (Vert.x 5.2.3, as in the parent pom) passes: 91 run, 0 failures, the 5 new tests included. The new test 20 times in one JVM: 100 run, 0 failures; with `VertxMqttConsumerTest` 20 times: 600 run, 0 failures. The Mosquitto and HiveMQ ITs were not run locally (CI runs them). The formatter and import-sort plugins ran on the module. Not changed: - A pause longer than about 1.5 times `keepAliveInterval` (back pressure at `maxPendingMessages`, or a redelivery waiting for the pending exchanges) drops the connection, as a paused client does not read the PINGRESP. With manual acknowledgements the broker then sends the unacknowledged messages again. That is the back-pressure design (TCP), and needs a separate change or a line in the docs; I can open a JIRA. - When the consumer of a shared connection stops while one of its exchanges fails, the failed message is not redelivered on the connection the producer keeps (the redelivery close is skipped while draining), until that connection drops. Before this change the stopped consumer left the connection paused, which hid this case. I can open a separate JIRA. - When the producer of an endpoint has started the connection before the consumer, the messages a kept session sends between the CONNACK and the consumer's start still find no handler, as the docs say for a stopped consumer on a shared connection. # 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-27383](https://issues.apache.org/jira/browse/CAMEL-27383). # 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. 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]
