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

   # Description
   
   [CAMEL-27041](https://issues.apache.org/jira/browse/CAMEL-27041)
   
   Found by the DST world in #30138 (CAMEL-26958, the Paho world), which leaves 
this case out of its default set: with `-Ddst.paho.stopWhileConnecting=true`, 7 
of 2000 seeds fail, 6 on "the stopped consumer disconnects and closes its 
client" and 1 on "every message is routed once the faults cleared". Reproduced 
here with a plain unit test against the real Paho 1.2.5 client.
   
   The own client of the consumer connects in the background in two cases: the 
automatic reconnect of Paho after the broker closed the connection or could not 
be reached (`automaticReconnect=true` is the default; an attempt connects 
during the TCP connect, bounded by `connectionTimeout`, and then waits for the 
CONNACK without a time limit), and the consumer's own reconnect after a failed 
message on a kept session (CAMEL-25793), which the stop interrupts. A route 
stop at that moment saw a client that was not connected, skipped the disconnect 
and called `close(true)`. Paho 1.2.5 does not close a connecting client, also 
with force: `ClientComms.close(boolean)` throws `MqttException` 32110 (Connect 
already in progress) when it is connecting (read with javap). The stop failed 
with that exception, the consumer dropped its reference, and the client went 
on: once its connect completed it stayed connected with nobody using or closing 
it. With a fixed `clientId` the next start creates a client
  with the same client id, the broker disconnects one of the two with Session 
taken over (0x8E, MQTT 5.0, 3.1.4), the automatic reconnect of that one 
connects again, and the two take the session from each other; messages 
delivered to the left-over client reach no route.
   
   Change, in `closeOwnedClient` (used by `doStop` and by a failed `doStart`):
   
   - when `close(true)` fails with 32110, the consumer calls 
`disconnectForcibly(0, 0, false)` (no quiesce, no DISCONNECT packet) and closes 
the client again. `ClientComms.disconnectForcibly` works in any state: it shuts 
down what the connect opened so far and leaves the client disconnected, so the 
close passes. A connect still in its TCP connect then fails on the closed 
client before it sends its CONNECT, and no new automatic reconnect cycle starts 
(the forced disconnect reports no lost connection to the reconnect callback). 
An attempt of the reconnect cycle that was already scheduled fails on the 
closed client (32111) and is not scheduled again;
   - when `close(true)` fails with 32100 (Client is connected), because the 
connect completed between the `isConnected()` check of the stop and the close, 
the client is disconnected as a connected client is, and then closed (if that 
disconnect fails because the client was disconnected again in the meantime, the 
client is disconnected forcibly as above).
   
   Any other close failure is reported as before.
   
   Not changed: as before, the idle timer thread of Paho's reconnect cycle 
("MQTT Reconnect: ...") ends only when the closed client is garbage collected; 
that is the same for a client stopped between two reconnect attempts. A connect 
that is still in its TCP connect when the client is closed is not interrupted: 
once the TCP connect succeeds, Paho fails it before it sends the CONNECT, but 
the socket and its receiver and sender threads stay until the broker closes the 
connection. The deprecated camel-paho (MQTT 3) consumer closes its client the 
same way and is left as is here.
   
   Tests, new `PahoMqtt5ConsumerStopWhileConnectingTest`:
   
   - `stopWhileTheClientReconnectsClosesTheClient`: the real Paho client 
against a minimal broker on a local socket, which answers the first CONNECT and 
SUBSCRIBE, closes the connection, and leaves the CONNECT of the automatic 
reconnect unanswered. The stop must not throw, the client must close its 
connection to the broker, and a later `connect()` must fail with 32111 (client 
closed). On main the stop throws 32110 and the client is still connecting.
   - `stopWhileConnectingAbortsTheConnectBeforeClosing` and 
`stopRightAfterTheConnectCompletedDisconnectsBeforeClosing`: a mocked client 
whose first close fails with 32110, or with 32100; on main the stop throws.
   - `failedDisconnectRightAfterTheConnectCompletedAbortsBeforeClosing`: 32100, 
and then the disconnect fails (32101).
   - `otherCloseFailureIsThrown`: the control, any other close failure is still 
thrown.
   
   Without the change the four failure cases fail (on main the stop throws 
32110 or 32100); with it all five pass, and the camel-paho-mqtt5 unit tests 
pass (80 run, 0 failures, 0 errors, 0 skipped). With the change merged locally 
into the world of #30138 (and its fake taught `disconnectForcibly`), 
`-Ddst.paho.stopWhileConnecting=true` passes 5000 of 5000 seeds (2000 of 2000 
in an earlier run); the same world without the change fails 7 of 2000 (seeds 
37, 276, 505, 797, 1061, 1160 and one more), as #30138 reports for main. The 
default set passes 5000 of 5000.
   
   #30138 is merged, and the fake of its world does not model 
`MqttClient.disconnectForcibly`, as main does not call it. Its default set 
leaves this case out, so it still passes with this change (1000 of 1000 seeds 
on main with this change and the CAMEL-27040 fix). The test-only change that 
models it (with the two corrections from the review of #30138: a forced 
disconnect during an automatic reconnect reschedules the cycle, and an older 
connect does not clear the connecting flag of a newer one) is in the 
CAMEL-27042 PR of this batch, which needs it for `IMqttToken.getReasonCodes`. 
Once both are in, the exclusion of this case from the default set of the world 
can be dropped.
   
   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).
   
   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.
   
   No upgrade guide entry: a stop that failed now succeeds, nothing else 
changes.
   
   # 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-27041](https://issues.apache.org/jira/browse/CAMEL-27041).
   
   # 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