[ 
https://issues.apache.org/jira/browse/CAMEL-27043?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

shashank updated CAMEL-27043:
-----------------------------
    Description: 
h3. What happens

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 is still in the route at that moment (a route step slower 
than the reconnect delay, 1 second for the first failure), the connect never 
completes.

The defect is in the lifecycle of the client threads in Paho 1.2.5, read in its 
sources. {{MqttClient.disconnect}} quiesces only the message queue, which is 
already empty (the callback thread took the message off the queue before 
handling it). {{CommsCallback.stop()}} then sets its target state to STOPPED 
and returns at once, 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 still in the route, and then sleeps in 
{{while (!isRunning())}} forever, as nothing sets the target state back to 
RUNNING. {{CommsSender}} has the same start/stop pattern, and an earlier 
reproduction hung there. The reconnect thread of the consumer waits on the 
connect token without a time limit.

Against Artemis 2.57 (embedded, MQTT 5) it gets worse: the broker closes a 
socket that sends no CONNECT within 10 seconds, the consumer's catch schedules 
another reconnect, 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 still in the route loses its 
PUBACK, is sent again, is in the route at the next reconnect, and the next 
connect hangs again, also after the failed message itself was acknowledged. 
After about 45 seconds Artemis deleted the pending messages of the subscription 
queue and the consumer stayed connected with no deliveries: the route is 
Started, {{isConnected()}} is true, and the messages are lost.

h3. How to reproduce

A route 
{{from("paho-mqtt5:topic?qos=1&manualAcksEnabled=true&cleanStart=false&sessionExpiryInterval=60&clientId=c")}}
 whose processor throws for the first delivery of {{fail-1}} and waits 3 
seconds (any time longer than the reconnect delay) for the first delivery of 
{{slow-2}}. Publish {{fail-1}} and {{slow-2}}, then {{after-3}}. The consumer 
never routes {{fail-1}} again nor {{after-3}}; a thread dump shows {{MQTT Con}} 
in {{CommsCallback.start}} called from {{ClientComms$ConnectBG.run}}, and 
{{PahoMqtt5-Reconnect}} in {{MqttClient.connectWithResult}} called from 
{{PahoMqtt5Consumer.reconnect}}. Reproduced with the real Paho 1.2.5 client 
against the embedded Artemis MQTT broker of the module's tests (new 
{{PahoMqtt5ConsumerReconnectWhileRoutingTest}}), and with a TLA+ model of the 
consumer and the Paho callback lifecycle.

h3. Fix

The root defect is in Paho 1.2.5; Camel now avoids triggering it. The reconnect 
after a failed exchange no longer connects the same {{MqttClient}} again: it 
disconnects and closes it, and connects a new client with the same client id, 
which resumes the session 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. The callback is set on the new client before its connect, as at 
start.

Exchanges still in the route on the closed client are not acknowledged (the 
broker sends their messages again to the new client, so they are routed twice, 
at-least-once as for QoS 1 in general) and do not reset the backoff, as for an 
exchange that completes after a stop (CAMEL-27040). A lock orders the 
replacement against the stop (the stop either closes the new client or the new 
client is never used, so no left-over client takes the session, see 
CAMEL-27041) and against the acknowledgement of an exchange (never on a closed 
client). A failed connect or a failed creation of the new client is retried 
with the backoff. {{getClient()}} of the consumer returns the new instance 
afterwards; the component page and the 4.24 upgrade guide say so.

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 the 
route; that is up to Paho.

_Filed with Claude Code on behalf of allthingssecurity._

  was:
Found by the Paho MQTT 5 DST world of CAMEL-26958 (PR #30138): with 
{{-Ddst.paho.strictCallbackThread=true}} about 8 to 9 % of the seeds fail 
"every message is routed once the faults cleared" (100 of the first 1157 
seeds). The default set of the world leaves this case out until it is 
reproduced. *Needs reproduction*: so far this is read from the byte code of 
Paho 1.2.5 ({{org.eclipse.paho.mqttv5.client}}) and modelled in the fake, not 
seen against a broker. If it reproduces, it may be a Paho issue rather than a 
Camel one.

h3. What the world shows

A connect that comes while the callback thread of the client is still in 
{{messageArrived}} after the connection closed never completes. In the world 
that happens when the connection is lost while a message is in the route, and 
then either the consumer reconnects after a failed message (CAMEL-25793) or the 
automatic reconnect of Paho runs, while that message is still in the route. The 
client then stays connecting, the consumer delivers nothing more, and the route 
stop cannot close the client ({{close()}} throws 32110, a connect in progress).

h3. Why (as read in Paho 1.2.5)

{{ClientComms.ConnectBG}} calls {{CommsCallback.start}} before it sends 
CONNECT. {{start()}} only starts the thread again when its state is STOPPED, 
and then waits until it is RUNNING. A disconnect or a lost connection asks the 
thread to stop, but a thread that is still in {{messageArrived}} has not 
reached STOPPED yet, so {{start()}} sets nothing and its wait does not end.

The fake leaves out the quiesce of a disconnect (Paho waits up to 30 seconds 
for the callback queues to empty), so the window in the real client may be 
smaller, or closed in some paths. The numbers above are of the fake only.

h3. Next steps

* Reproduce with a plain test against a broker (Mosquitto from test-infra): a 
route that blocks in the first message, then the broker drops the connection, 
and the consumer or the automatic reconnect connects while the route still 
holds the message; check whether the connect completes once the route returns.
* If it hangs, check whether Paho 1.2.5 (and the latest Paho) is at fault and 
report it upstream; the Camel side may then only need a guard (for example not 
connecting from the callback thread while an exchange is in progress).
* Once settled, the world can check this case by default (drop 
{{dst.paho.strictCallbackThread}}).

_Filed with Claude Code on behalf of allthingssecurity._

       Priority: Major  (was: Minor)
        Summary: camel-paho-mqtt5 - manualAcksEnabled: the reconnect after a 
failed message hangs while another message is still in a synchronous route, and 
on Artemis the consumer ends up connected but idle, with messages lost  (was: 
camel-paho-mqtt5 - a reconnect while the Paho callback thread is still in 
messageArrived may hang the client (needs reproduction, maybe a Paho issue))

> camel-paho-mqtt5 - manualAcksEnabled: the reconnect after a failed message 
> hangs while another message is still in a synchronous route, and on Artemis 
> the consumer ends up connected but idle, with messages lost
> ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
>
>                 Key: CAMEL-27043
>                 URL: https://issues.apache.org/jira/browse/CAMEL-27043
>             Project: Camel
>          Issue Type: Bug
>          Components: camel-paho-mqtt5
>            Reporter: shashank
>            Assignee: shashank
>            Priority: Major
>
> h3. What happens
> 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 is still in the route at that moment (a route step 
> slower than the reconnect delay, 1 second for the first failure), the connect 
> never completes.
> The defect is in the lifecycle of the client threads in Paho 1.2.5, read in 
> its sources. {{MqttClient.disconnect}} quiesces only the message queue, which 
> is already empty (the callback thread took the message off the queue before 
> handling it). {{CommsCallback.stop()}} then sets its target state to STOPPED 
> and returns at once, 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 still in the route, and then 
> sleeps in {{while (!isRunning())}} forever, as nothing sets the target state 
> back to RUNNING. {{CommsSender}} has the same start/stop pattern, and an 
> earlier reproduction hung there. The reconnect thread of the consumer waits 
> on the connect token without a time limit.
> Against Artemis 2.57 (embedded, MQTT 5) it gets worse: the broker closes a 
> socket that sends no CONNECT within 10 seconds, the consumer's catch 
> schedules another reconnect, 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 still in the 
> route loses its PUBACK, is sent again, is in the route at the next reconnect, 
> and the next connect hangs again, also after the failed message itself was 
> acknowledged. After about 45 seconds Artemis deleted the pending messages of 
> the subscription queue and the consumer stayed connected with no deliveries: 
> the route is Started, {{isConnected()}} is true, and the messages are lost.
> h3. How to reproduce
> A route 
> {{from("paho-mqtt5:topic?qos=1&manualAcksEnabled=true&cleanStart=false&sessionExpiryInterval=60&clientId=c")}}
>  whose processor throws for the first delivery of {{fail-1}} and waits 3 
> seconds (any time longer than the reconnect delay) for the first delivery of 
> {{slow-2}}. Publish {{fail-1}} and {{slow-2}}, then {{after-3}}. The consumer 
> never routes {{fail-1}} again nor {{after-3}}; a thread dump shows {{MQTT 
> Con}} in {{CommsCallback.start}} called from {{ClientComms$ConnectBG.run}}, 
> and {{PahoMqtt5-Reconnect}} in {{MqttClient.connectWithResult}} called from 
> {{PahoMqtt5Consumer.reconnect}}. Reproduced with the real Paho 1.2.5 client 
> against the embedded Artemis MQTT broker of the module's tests (new 
> {{PahoMqtt5ConsumerReconnectWhileRoutingTest}}), and with a TLA+ model of the 
> consumer and the Paho callback lifecycle.
> h3. Fix
> The root defect is in Paho 1.2.5; Camel now avoids triggering it. The 
> reconnect after a failed exchange no longer connects the same {{MqttClient}} 
> again: it disconnects and closes it, and connects a new client with the same 
> client id, which resumes the session 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. The callback is set on the new client before its 
> connect, as at start.
> Exchanges still in the route on the closed client are not acknowledged (the 
> broker sends their messages again to the new client, so they are routed 
> twice, at-least-once as for QoS 1 in general) and do not reset the backoff, 
> as for an exchange that completes after a stop (CAMEL-27040). A lock orders 
> the replacement against the stop (the stop either closes the new client or 
> the new client is never used, so no left-over client takes the session, see 
> CAMEL-27041) and against the acknowledgement of an exchange (never on a 
> closed client). A failed connect or a failed creation of the new client is 
> retried with the backoff. {{getClient()}} of the consumer returns the new 
> instance afterwards; the component page and the 4.24 upgrade guide say so.
> 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 the route; that is up to Paho.
> _Filed with Claude Code on behalf of allthingssecurity._



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

Reply via email to