shashank created CAMEL-27510:
--------------------------------

             Summary: camel-azure-servicebus - the Camel lock renewer shares 
the timer thread of the period task scheduler: a slow periodic task lets 
message locks expire, and every consumer start leaves one more renewer task 
registered
                 Key: CAMEL-27510
                 URL: https://issues.apache.org/jira/browse/CAMEL-27510
             Project: Camel
          Issue Type: Bug
          Components: camel-azure-servicebus
            Reporter: shashank
            Assignee: shashank


h3. What happens

The Camel lock renewer of the Service Bus consumer (peek-lock mode, 
{{maxAutoLockRenewDuration}} above 0, no sessions: on by default) is a task of 
the period task scheduler of the CamelContext 
({{PeriodTaskScheduler.schedulePeriodTask}}). That scheduler calls all its 
tasks one after another on one shared timer thread. Two problems follow from 
that:

* *A slow periodic task holds up the renewals.* The renewer renews a lock that 
expires within 20 seconds, so its first renewal comes with about 14 to 20 
seconds left. Any other task on the same thread that takes longer than that 
(for example a vault secret refresh task that waits on a remote call, or the 
context reload such a task starts on that thread, which waits up to the 
shutdown timeout for the exchanges in flight) lets the lock expire. Service Bus 
then delivers the message again while the first exchange is still running, and 
the first exchange cannot complete its message.
* *Every start leaves one more task registered.* The {{PeriodTaskScheduler}} 
has no way to remove a task, and {{doStop}} only sets a flag. Each route 
stop/start, supervised restart or context reload adds one more task that stays 
until the CamelContext stops. It keeps the stopped consumer (and its endpoint) 
reachable and runs every 5 seconds doing nothing.

Also, with a slow broker (a renewal that answers after more than one run of the 
renewer), each run sends one more renewal of the same message, and an answer 
that arrives after a newer one moves the stored lock expiry backwards (the 
stored value is only used to decide when to renew, so this makes the renewer 
renew sooner, it does not lose a lock).

h3. How to reproduce

With a real {{DefaultCamelContext}} and mocked Service Bus clients:
* 5 stop/start cycles of one consumer leave 5 renewer tasks on the period task 
scheduler; 3 more consumers started and stopped make it 8.
* another periodic task that blocks the timer thread for 14 seconds: a lock 
with 12 seconds left was first renewed 4 seconds after it expired.
* a broker that has not answered yet: 3 runs send 3 renewals of one message; 
answers delivered newest first leave the lock expiry 10 seconds earlier.

Found with a TLA+ model of the renewer, the shared timer thread, stop/start and 
the reload that runs on that thread, and reproduced against the real consumer.

h3. Fix

The renewer runs on a scheduled executor of its own, created from the 
{{ExecutorServiceManager}} of the CamelContext in {{doStart}} (one thread per 
consumer, named {{ServiceBusLockRenewer[<topic or queue name>]}}) and shut down 
in {{doStop}}/{{doShutdown}}. It still runs every 5 seconds and renews a lock 
that expires within 20 seconds (CAMEL-27066). A run does not send a renewal for 
a message while one is in flight, unless that one has not answered for 10 
seconds (the SDK ends a request only after its tryTimeout, 1 minute by default, 
which can be after the lock expired). An answer only moves the stored expiry 
forward. CAMEL-27066 chose to keep the renewer on the shared timer with no new 
thread; this ticket reverses that choice, because the shared timer thread is 
the cause of the first two problems.

_Filed with Claude Code on behalf of allthingssecurity._



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

Reply via email to