shashank created CAMEL-27530:
--------------------------------

             Summary: camel-mllp - the producer adds an idle check chain on 
every reconnect and on every restart, and process() on a stopped producer 
leaves a connection open with no idle check
                 Key: CAMEL-27530
                 URL: https://issues.apache.org/jira/browse/CAMEL-27530
             Project: Camel
          Issue Type: Bug
          Components: camel-mllp
            Reporter: shashank
            Assignee: shashank


h3. What happens

With {{idleTimeout}} set, {{MllpTcpClientProducer.checkConnection()}} schedules 
an idle check ({{run()}}) on {{idleTimeoutExecutor}} for every new connection, 
and {{run()}} schedules itself again as long as the connection it finds in the 
{{socket}} field is open and not idle. Nothing cancels a check that is already 
scheduled, and {{run()}} does not look at which connection it was scheduled for:
* every reconnect adds one more check chain. When the producer reconnects (the 
receiver, a firewall or a load balancer dropped the connection, so the next 
write or ack read fails and {{process()}} reconnects and resends), the check of 
the old connection is still pending. When it runs it finds the new connection 
open and reschedules itself, next to the check scheduled for the new 
connection. Each later reconnect adds one more, and they only end when the 
connection is idle for a whole {{idleTimeout}}. Against a receiver that closes 
the connection after every message, the producer gets one more chain per 
message; every chain runs at least once per {{idleTimeout}} and takes the same 
lock as {{process()}}. No message is lost or duplicated by this.
* a check survives a stop and start. {{doStop}} calls {{shutdown()}} on the 
executor, which still runs its delayed tasks (the default policy of 
{{ScheduledThreadPoolExecutor}}). After {{start()}}, the old check runs on the 
old executor's thread, finds the connection of the new start open and 
reschedules itself on the executor of the new start: two chains after one 
restart.
* {{process()}} does not check the state of the producer. A route thread that 
called {{process()}} and waited for the lock while {{stop()}} held it runs on 
the stopped producer: it opens a new connection, then fails with a 
{{NullPointerException}} (wrapped in a {{RuntimeException}}) because {{doStop}} 
set the executor to null. The connection stays open while the producer is 
stopped, and after the next {{start()}} the producer uses it with no idle 
check, so {{idleTimeout}} never closes it. Without {{idleTimeout}} the same 
call sends the message on a stopped producer and leaves its connection open.

h3. How to reproduce

A producer with {{idleTimeout}} and a receiver that closes the connection after 
every message: after 6 messages sent 600 ms apart (idleTimeout 4000 ms), 6 idle 
checks are pending, and 11 runs of the check were needed to close the one idle 
connection. Stop and start the producer and send a message: the check of the 
first start runs on the old executor's thread and reschedules itself, two 
checks are pending on the new executor. Hold the lock of the producer, call 
{{process()}} from another thread, stop the producer and release the lock: 
{{process()}} throws a {{RuntimeException}} caused by a 
{{NullPointerException}}, the producer is stopped with an open connection, and 
after a restart that connection is used and never closed by the idle check.

Found with a TLA+ model of the producer (process with its reconnect and resend, 
the idle check, stop and start), and reproduced against the real producer.

h3. Fix

The producer keeps the future of its idle check in a field. Every schedule (a 
new connection in {{checkConnection()}}, and the reschedule in {{run()}}) 
cancels the pending check first, with the lock held, so there is at most one. 
The reschedule in {{run()}} cancels it too: a check that was already taken by 
the executor thread when a reconnect cancelled it (it waits for the lock while 
{{process()}} reconnects) cannot be stopped by the cancel, and it then replaces 
the check of the new connection instead of adding one. {{doStop}} cancels the 
check and calls {{shutdownNow()}} on the executor (it is created by {{doStart}} 
for this producer only). {{process()}} fails the exchange with a 
{{RejectedExecutionException}} when the producer is not started, before it 
connects.

_Filed with Claude Code on behalf of allthingssecurity._



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

Reply via email to