shashank created CAMEL-27357:
--------------------------------

             Summary: camel-a2a - the consumer loses task permits 
(maxConcurrentTasks) and leaves queued tasks waiting: a follow-up message on a 
running task, an async timeout as the task completes, a task queued while 
another ends
                 Key: CAMEL-27357
                 URL: https://issues.apache.org/jira/browse/CAMEL-27357
             Project: Camel
          Issue Type: Bug
          Components: camel-ai
            Reporter: shashank
            Assignee: shashank


h3. What happens

The A2A consumer limits async tasks ({{returnImmediately}}) with 
{{maxConcurrentTasks}} (a semaphore) and {{taskQueueSize}} (a queue drained 
when a permit is given back). The permit of a task is given back by whoever 
removes the task's entry from {{inFlightTasks}}, which is keyed by task id. 
Several orders of events lose a permit or leave a queued task behind:

* *A follow-up message on a running task leaks a permit.* {{message:send}} with 
the {{taskId}} of a task that is still running (and a free permit) starts a 
second run of the same task. Its {{inFlightTasks.put}} replaces the entry of 
the first run; the first run's {{finally}} then removes the second run's entry 
and gives back one permit, and the second run finds no entry and gives back 
none. Two permits taken, one given back. After {{maxConcurrentTasks}} such 
messages every sync and streaming request gets HTTP 429 and every async request 
waits in the queue for good (or gets 429 when {{taskQueueSize}} is 0). The 
first run's {{finally}} also cancels the second run's timeout. No failure is 
needed, it is an ordinary client call.
* *An async timeout that fires as the task completes leaks a permit.* The 
timeout removes the entry, then checks {{f.isDone()}}. If the task completes in 
between, its {{finally}} finds no entry and the timeout sees a done task: 
neither gives the permit back. The streaming path has the same code.
* *A task queued while the running task ends waits with a free permit.* The 
request fails {{tryAcquirePermit()}}; before its {{offer}}, the running task 
ends, gives its permit back and finds the queue empty. The task stays SUBMITTED 
in the queue until some other task ends (for ever without more traffic; 
{{stuckTaskTtlMs}} is off by default).
* *A cancel of a queued task can race the drain.* The drain has taken the task 
out of the queue and has not submitted it yet; the cancel finds it neither in 
{{inFlightTasks}} nor in the queue and returns CANCELED. The drain then submits 
it and the route runs for a task the client was told is canceled.
* (low) The request stores SUBMITTED after it queued the task. A drain on 
another thread can run the task before that; if the route fails, FAILED is not 
stored (the task is not in the store yet) and the later SUBMITTED stays for 
good.

h3. How to reproduce

{{maxConcurrentTasks=2}}, {{returnImmediately=true}}: send a message, and while 
its route runs send a second message with {{"taskId": <the first task>}}. Once 
both runs ended, only 1 of the 2 permits is free; repeat once more and every 
request is refused with 429.

Found with a TLA+ model of the consumer's permits, queue, cancel and async 
timeout.

h3. Fix

The permit belongs to the run of a task ({{TaskRun}}, with an "ended" flag set 
by compare-and-set), not to the map entry: whoever ends the run first (its 
worker, its timeout, a cancel or the stop) gives the permit back, exactly once, 
to the semaphore it was taken from, and entries are removed only by their own 
run. The request drains the queue once more after it queued a task, stores 
SUBMITTED before it queues the task (a full queue is refused before the store 
is touched), and a run does not start the route when its task is already 
CANCELED.

_Filed with Claude Code on behalf of allthingssecurity._



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

Reply via email to