shashank created CAMEL-27041:
--------------------------------

             Summary: camel-paho-mqtt5 - a stop while the client connects fails 
to close it (32110), and the left-over client takes the session from the client 
of the next start
                 Key: CAMEL-27041
                 URL: https://issues.apache.org/jira/browse/CAMEL-27041
             Project: Camel
          Issue Type: Bug
          Components: camel-paho-mqtt5
            Reporter: shashank
            Assignee: shashank


Found by the Paho DST world of CAMEL-26958 (PR #30138, branch 
{{camel-paho-mqtt5-dst-world-v2}}), which leaves this case out of its default 
set: with {{-Ddst.paho.stopWhileConnecting=true}}, 7 of 2000 seeds fail, 6 on 
the invariant "the stopped consumer disconnects and closes its client" and 1 on 
"every message is routed once the faults cleared". Reproduced outside the DST 
harness with a plain unit test against the real Paho 1.2.5 client (below).

h3. What happens

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}}, the default; an attempt is connecting 
during the TCP connect, bounded by {{connectionTimeout}}, and then until the 
CONNACK, for which Paho has no time limit), and the reconnect of the consumer 
after a failed message on a kept session (CAMEL-25793), which the stop 
interrupts. When the route stops then, {{doStop}} sees a client that is not 
connected, skips the disconnect and calls {{close(true)}}. Paho 1.2.5 refuses 
to close a connecting client, also with force: {{ClientComms.close(boolean)}} 
throws {{MqttException}} 32110 (Connect already in progress) when 
{{isConnecting()}} (read with javap). The stop fails with that exception, the 
consumer drops its reference to the client, and the client goes on: its connect 
completes later (or the reconnect cycle tries again), so it stays connected 
with nobody using or closing it.

With a fixed {{clientId}} the next start of the route 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, so messages go to the left-over 
client, whose callback no longer belongs to a route. With a generated client id 
the left-over client stays connected next to the new one.

h3. Expected

A stop releases the client it created, whatever the state of its connection.

h3. Reproduction

New {{PahoMqtt5ConsumerStopWhileConnectingTest}}: 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. On main the stop throws {{MqttException}} 32110 and the 
client is still connecting afterwards. More tests with a mocked client: a first 
close that fails with 32110 (aborted, then closed), with 32100 (disconnected, 
then closed), and with 32100 followed by a failed disconnect (aborted, then 
closed).

h3. Fix

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 sets the client to disconnecting and shuts down what the connect opened so 
far, the client ends disconnected, and the close succeeds; a connect still in 
its TCP connect is not interrupted, but fails on the closed client before it 
sends its CONNECT (its socket and threads stay until the broker closes the 
connection), and the automatic reconnect does not start again (read with javap; 
checked in the test against a broker that does not answer the CONNECT). A 
connect that completes between the check of {{doStop}} and the close makes the 
close throw 32100 (client is connected): the client is then disconnected and 
closed. Branch {{camel-paho-mqtt5-stop-while-connecting}}.

_Filed with Claude Code on behalf of allthingssecurity._



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to