shashank created CAMEL-27534:
--------------------------------

             Summary: camel-jms - with asyncStartListener=true, a consumer 
suspended or resumed before its listener container started keeps consuming, or 
runs it with doubled consumers
                 Key: CAMEL-27534
                 URL: https://issues.apache.org/jira/browse/CAMEL-27534
             Project: Camel
          Issue Type: Bug
          Components: camel-jms
            Reporter: shashank
            Assignee: shashank


h3. What happens

With {{asyncStartListener=true}}, {{JmsConsumer.doStart}} submits a task that 
initializes and starts the listener container later. That task checks that the 
consumer still owns the container (CAMEL-26536 made this safe against a stop), 
but {{doSuspend}} and {{doResume}} do not know about it:
* *a route suspended while the start is pending keeps consuming.* {{doSuspend}} 
stops a container that is not started yet, then the task starts it. The route 
and the consumer are Suspended, the listener container runs, and messages reach 
the suspended route.
* *a route resumed while the start is pending gets doubled consumers.* 
{{doResume}} sees {{initialized=false}} and calls {{doStart}} again: the 
endpoint counts the same container twice ({{onListenerContainerStarting}}), and 
a second start task is submitted. Both tasks call {{afterPropertiesSet()}}, and 
Spring's {{DefaultMessageListenerContainer}} schedules another 
{{concurrentConsumers}} invokers on each initialization. On a topic without a 
shared subscription every message is then processed twice; on a queue the 
concurrency doubles (ordering with {{concurrentConsumers=1}} is lost). The 
extra count stays after the route is stopped, so {{JmsEndpoint.stop()}} and 
{{shutdown()}} never stop the endpoint.
* *suspend and resume after a failed async start do the same.* With 
{{testConnectionOnStartup=true}} and the broker unreachable, the task has 
already initialized the container when the connection test fails; it only logs. 
A suspend and resume then runs {{doStart}} again: counted twice and initialized 
twice.

Anything that suspends a route during the start window triggers it: the route 
controller, JMX, route policies. The window is long exactly when 
{{asyncStartListener}} helps, a slow or unreachable broker.

h3. How to reproduce

Hold the tasks of the component's async start/stop pool and run them from the 
test. Start the route, suspend it, run the task: the container runs, and a 
message sent to the queue reaches the suspended route. Start, suspend, resume, 
run the tasks: 2 tasks were submitted, the container with 
{{concurrentConsumers=1}} has 2 scheduled consumers, the endpoint counts 2 
running listeners, and 1 after the route is stopped. The same on a topic: one 
published message is processed twice. With {{testConnectionOnStartup=true}}, 
fail the first connection test, then suspend and resume: 2 scheduled consumers, 
and 1 listener still counted after the stop.

Found with a TLA+ model of the consumer lifecycle (start, stop, suspend, resume 
on the route controller thread against the async start and stop tasks), and 
reproduced against the real consumer with the embedded Artemis broker.

h3. Fix

The consumer tracks, under its {{containerLock}}, whether {{doStart}} counted 
the current listener container and whether its asynchronous start task has 
checked the consumer status yet. The task initializes the container only when 
it is not active yet, then checks the status once under the lock: it marks the 
consumer initialized and starts the container only when the consumer is not 
suspending or suspended. After {{start()}} it checks once more under the lock 
and stops the container if the consumer was suspended meanwhile. It calls 
{{start()}} at most once (it does not ask the container whether it runs: 
Camel's containers report not running while the CamelContext is stopping or the 
endpoint is not started). {{doResume}} with {{initialized=false}} only changes 
the status while the task has not checked it yet; when the container was 
counted but its start failed, it runs the start of the same container again 
without counting it again; otherwise (never started) it calls {{doStart}} as 
before. With {{initialized=true}} it starts the container as before, also while 
the task is still in {{start()}}. {{doSuspend}} does not change. With 
{{asyncStartListener=false}} the same code runs on the lifecycle thread, where 
no suspend or resume can run at the same time: there the only changes are that 
an already active container is not initialized again, and that {{initialized}} 
is set before {{start()}} and reset when the start fails.

_Filed with Claude Code on behalf of allthingssecurity._



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

Reply via email to