allthingssecurity opened a new pull request, #30246:
URL: https://github.com/apache/camel/pull/30246

   # Description
   
   [CAMEL-27040](https://issues.apache.org/jira/browse/CAMEL-27040)
   
   Noticed while reading the consumer, and left out of the Paho DST world of 
#30138 (CAMEL-26958), whose operator stops the route only while no message is 
in the route. Reproduced here with a plain unit test.
   
   With `manualAcksEnabled=true` each exchange gets an on-completion that 
acknowledges its message: `onComplete` called 
`PahoMqtt5Consumer.this.client.messageArrivedComplete(id, qos)`, and 
`onFailure` calls `rollback`, which acknowledges a message the broker does not 
send again, or reconnects for one it does. A route stop does not wait for the 
exchanges in flight before it stops the consumer: `DefaultConsumer` is neither 
`Suspendable` nor `ShutdownAware`, so `DefaultShutdownStrategy` stops it first 
and then waits for the in-flight exchanges. `doStop` closes the own client of 
the consumer and sets `client` to null. An exchange that completed after that:
   
   - `onComplete` threw a `NullPointerException` (only `MqttException` was 
caught), which the unit of work logs as a WARN with the stack trace;
   - after the consumer started again (a route restart, or the supervising 
route controller), `client` was the new client: `onComplete` acknowledged the 
packet id of the closed connection on the new client, and `onFailure` 
acknowledged it there or made the new client reconnect. A packet id only names 
a message within one session (MQTT 5.0, 2.2.1), so this could acknowledge a 
message of the new client that its own exchange had not finished.
   
   Change: the on-completion keeps the client the message arrived on, and 
`onComplete` and `rollback` do nothing (a DEBUG log) when the current client is 
null or another one. Once that client is closed there is nothing to 
acknowledge: on a session the broker keeps (`cleanStart=false` and 
`sessionExpiryInterval` > 0) the broker sends the unacknowledged QoS 1 message 
again when a client with the same client id connects (MQTT 5.0, 4.4), so with a 
fixed `clientId` the next start gets it again; with a generated client id (the 
default) each start has a new session, and the old one expires after 
`sessionExpiryInterval`. Otherwise the session ended with the connection. A 
client given with the `client` option is the same object before and after a 
restart, so its messages are acknowledged as before. An exchange of a closed 
client no longer resets the backoff of the reconnects after failed messages of 
the current client.
   
   Not changed: the consumer still closes its client before the exchanges in 
flight complete, so their acknowledgements are not sent. Keeping the client 
open until they complete (making the consumer `ShutdownAware`) is a larger 
change of the stop, and is not needed to stop the exception and the 
acknowledgement on the wrong client. A stop that closes the client between that 
check and the acknowledgement can still make the acknowledgement fail inside 
Paho (a narrow window on another thread; it is reported as before). An exchange 
that completes after an automatic reconnect of Paho on the same client still 
acknowledges its packet id on the new connection; that is the same client 
object, so it is not covered here, and with the consumer's own reconnect after 
a failed message (CAMEL-25793) it is the resent copy of the same message on the 
same session. Telling connections apart needs a connection counter that 
`connectComplete` moves on, which also changes that reconnect, so I would 
rather op
 en a follow-up JIRA for it. The deprecated camel-paho (MQTT 3) consumer has 
the same on-completion (`PahoConsumer.this.client.messageArrivedComplete`) and 
is left as is here.
   
   Tests, new `PahoMqtt5ConsumerAckAfterStopTest` (a mocked `MqttClient`, the 
real consumer and a `DefaultCamelContext`; the exchange is created by the 
consumer and its on-completions are run by the test):
   
   - `exchangeCompletingAfterStopIsNotAcknowledged`: on main, the 
`NullPointerException` from `onComplete`.
   - `exchangeCompletingAfterRestartIsNotAcknowledgedOnTheNewClient` and 
`exchangeFailingAfterRestartIsNotAcknowledgedOnTheNewClient`: on main, packet 
id 7 is acknowledged on the client of the next start.
   - `exchangeCompletingWhileRunningIsAcknowledged` and 
`exchangeOfClientOptionCompletingAfterRestartIsAcknowledged` (a client given 
with the `client` option, kept across the restart): the controls, they pass on 
main too.
   
   Without the change the first three fail; with it all five pass, and the 
camel-paho-mqtt5 unit tests pass (80 run, 0 failures, 0 errors, 0 skipped). 
Merged locally with the Paho DST world of #30138 and the other two 
camel-paho-mqtt5 fixes of this batch, the world passes 1000 of 1000 seeds. The 
ITs need Docker and were not run locally (CI runs them). I built offline with 
the versions cached on my machine (Caffeine 3.3.0, commons-lang3 3.21.0, Guava 
33.7.2); Paho 1.2.5 is the version of main.
   
   The three camel-paho-mqtt5 fixes of this batch (CAMEL-27040, CAMEL-27041, 
CAMEL-27042) all change `PahoMqtt5Consumer`, in different methods. They merge 
cleanly in either order, and merged together on main (with the fake change of 
the CAMEL-27042 PR) the camel-paho-mqtt5 unit tests and the world pass (1089 
run including 1000 seeds, 0 failures).
   
   No upgrade guide entry: nothing that worked before behaves differently.
   
   # 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-27040](https://issues.apache.org/jira/browse/CAMEL-27040).
   
   # 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. 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]

Reply via email to