shashank created CAMEL-27413:
--------------------------------

             Summary: camel-seda - a SedaConsumer stopped and started again 
while its threads process exchanges runs more threads than concurrentConsumers, 
can be shut down while a thread processes an exchange, and can leave a thread 
spinning
                 Key: CAMEL-27413
                 URL: https://issues.apache.org/jira/browse/CAMEL-27413
             Project: Camel
          Issue Type: Bug
          Components: camel-seda
            Reporter: shashank
            Assignee: shashank


h3. What happens

{{SedaConsumer.doStop}} shuts its executor down with {{shutdownNow}}: it 
interrupts the polling threads and does not wait for them. When the same 
consumer instance is started again while a thread is still processing an 
exchange, that thread sees {{isRunAllowed() == true}} and goes on polling. A 
route policy does this: {{RoutePolicySupport.stopConsumer}} / {{startConsumer}} 
({{HazelcastRoutePolicy}}, {{MasterRoutePolicy}}, {{DnsActivationPolicy}}). A 
stop and start through the route controller creates a new consumer and is not 
affected.

* More polling threads than {{concurrentConsumers}}: {{doStart}} creates a 
latch of {{concurrentConsumers}}, and {{setupTasks}} starts 
{{max(concurrentConsumers - taskCount, 1)}} new tasks. With 
{{concurrentConsumers=2}} and both threads busy at the stop, 3 threads poll 
after the start and 3 exchanges are processed at the same time; the latch 
counts 2.
* A graceful shutdown that does not wait: the 2 idle threads release the latch, 
so {{prepareShutdown}} returns while the third thread still processes an 
exchange, and the strategy stops the consumer.
* A spinning thread: when the processor does not clear the interrupt of the 
stop, the thread goes on after the start with the flag set; every 
{{queue.poll}} throws {{InterruptedException}} at once and the catch sets the 
flag again. It uses a whole CPU, never takes an exchange, and every later 
graceful shutdown waits for its full timeout.

CAMEL-22390 was the same loop when user code interrupts a SEDA thread; here the 
interrupt comes from Camel's own stop.

h3. How to reproduce

{{seda:foo?concurrentConsumers=2}}, a processor that blocks and ignores 
interrupts, both threads busy, then {{ServiceHelper.stopService(consumer)}} and 
{{startService(consumer)}}: after the processors return, 3 exchanges are 
processed at once, {{prepareShutdown}} returns while an exchange is processed, 
and (when the processor keeps the interrupt flag) the old thread spins and 
{{prepareShutdown}} waits for its timeout with nothing pending.

Found with a TLA+ model of the polling threads and the shutdown strategy.

h3. Fix

Each start submits its own polling tasks, which poll only while their start is 
the current one and count down the latch of their start; {{prepareShutdown}} 
also waits for threads of earlier starts that still process an exchange. 
{{ThreadPerTaskSedaConsumer}} declares its single coordinator through a new 
{{getPollingTasks()}}.

_Filed with Claude Code on behalf of allthingssecurity._



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

Reply via email to