allthingssecurity opened a new pull request, #30632: URL: https://github.com/apache/camel/pull/30632
# Description [CAMEL-27357](https://issues.apache.org/jira/browse/CAMEL-27357) The A2A consumer limits async tasks (`returnImmediately`) with `maxConcurrentTasks` (a semaphore) and `taskQueueSize` (a queue that is drained when a permit is given back). The permit of a task was given back by whoever removed the task's entry from `inFlightTasks`, a map keyed by task id. Some orders of events lost a permit for good, or left a queued task waiting: - A follow-up message on a running task. `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` replaced the entry of the first run. The first run's `finally` then removed the second run's entry and gave back one permit; the second run found no entry and gave back none. Two permits were taken and one was given back. After `maxConcurrentTasks` such messages, every sync and streaming request got HTTP 429 and every async request waited in the queue for good (or got 429 when `taskQueueSize` is 0). The first run's `finally` also cancelled the second run's timeout. This is an ordinary client call, no failure is involved. - An async timeout that fires as the task completes. The timeout removed the entry and then checked `f.isDone()`. When the task completed in between, its `finally` found no entry and the timeout saw a done task, so neither gave the permit back. The streaming path has the same code. - A task queued while the running task ends. The request fails `tryAcquirePermit()`; before its `offer`, the running task ends, gives its permit back and finds the queue empty. The task stayed SUBMITTED in the queue with a free permit until some other task ended (for ever without more traffic; `stuckTaskTtlMs` is off by default). - A cancel of a queued task racing the drain. The drain has taken the task out of the queue and not submitted it yet; the cancel finds it neither in `inFlightTasks` nor in the queue and returns CANCELED. The drain then submitted it, and the route ran for a task the client was told is canceled. - (low) The request stored SUBMITTED after it queued the task. A drain on another thread could run the task first; when its route failed, FAILED was not stored (the task was not in the store yet), and the SUBMITTED stored afterwards stayed for good. Changes (`A2AConsumer` only): - A run of a task on the async executor is a `TaskRun`, which holds the permit and its timeout. The permit belongs to the run, not to the map entry: whoever ends the run first (its worker, its timeout, a cancel or the stop) ends it with a compare-and-set and gives the permit back, exactly once, to the semaphore it was taken from. So an earlier run of a task that the stop does not reach (see below) and that ends after the consumer started again does not add a permit to the new start. `inFlightTasks` maps a task id to its latest run, and a run removes only its own entry (`remove(taskId, run)`). The `timeoutFutures` map is gone: each run cancels its own timeout. - The timeout ends the run only when the worker has not ended it yet, and then cancels it, stores FAILED and gives the permit back, without the `isDone()` check. Async and streaming tasks share this code (`submitRun`, `endRun`). - A queued task is stored as SUBMITTED before it is queued (`queueTask`). A full queue is refused before the store is touched, as before: a put of a new task can evict the oldest terminal task once `maxStoredTasks` is reached. Only when another request takes the last place between that check and the `offer` is what was in the store put back (deleted for a new task); the request gets 429 as before. After it queued the task, the request drains the queue once more, so a permit given back meanwhile starts the task. - A run does not start the route when its task is already CANCELED (it only gives its permit back). This covers the drain race and the same window on the immediate path (a cancel between `store.put(SUBMITTED)` and the submit, by a client that learned the id from `GET /tasks`). - `drainPendingQueue` reads the queue and the semaphore once (the stop sets both to null), and `cleanupTaskSubmissionFailure` no longer cancels the in-flight run of the task id: a run whose submission failed removes itself, and a run in the map belongs to another message of the same task. What users can notice: none of the above leaks or waits any more. An async message (`returnImmediately`) on a task that is already CANCELED no longer runs the route (its result was dropped anyway, as the store keeps a terminal state); a blocking `message:send` on such a task still runs it, as before. Found with a TLA+ model of the consumer's permits, queue, cancel, async timeout and stop. I adapted it to the fix. On main it finds the five cases above (permits not conserved, a task left in the queue with a free permit, a canceled task started, a task left SUBMITTED). With the fix all of them hold: 2 requests with a cancel and the timeout (654,309 states), 3 requests where the third continues the first task, with `maxConcurrentTasks=2`, a cancel and the timeout (8,645,032), and 3 requests with 2 cancels (9,025,755). TLC gives the same counts on the small scopes. The model has no restart of the consumer and no cap on the task store; the last two tests below cover those. Tests: new `A2AConsumerTaskPermitsTest` (7 tests). Each test forces one order of events with a hook on the consumer's queue, executor or in-flight map (the hook only holds one thread at one step): - a follow-up message on a running task (no hook): both permits must come back, and two tasks must then run at once; - the timeout takes the task out of the in-flight map, and the task completes right then: the permit must come back and the next task must run; - the running task ends after the request found no permit and before it queued its task: the queued task must run; - a cancel while the drain holds the task: the route must not run for the canceled task, and the permit must come back; - the queued task runs and fails before the request returns: it must end FAILED; - a full queue while the store is at `maxStoredTasks` (control, passes on main): 429, the rejected task is not stored and the canceled task the store keeps is not evicted; - a stop and a new start while an earlier run of a task (replaced by a follow-up message, so the stop does not reach it) still runs (control, passes on main): when that run ends, the new start still has `maxConcurrentTasks` permits. On main 5 of the 7 fail (the two controls pass): the permits stay lost (`Condition ... was not fulfilled within 5 seconds` for the follow-up and the timeout), the queued task is not started (within 10 seconds), the route ran for the canceled task (`[task1, task2]` instead of `[task1]`), and the failed task stays `SUBMITTED` (expected `FAILED`). With the change the module tests pass: 586 run, 0 failures. `A2AConsumerTaskPermitsTest` 20 times in one JVM: 140 run, 0 failures. Not changed: - A follow-up message on a running task still runs a second time, concurrently with the first, and a cancel or the stop reaches only the latest run of a task (as before). Whether such a message should wait for the first run, or be refused, is a separate question. - The model also finds, with a stop: a send request that is still running while the consumer stops can leave its task SUBMITTED, and a double release when the submit fails after an early timeout during the stop (no effect, the semaphore is dropped right after). Both are separate from this change. # Target - [x] I checked that the commit is targeting the correct branch (Camel 4 uses the `main` branch) # Tracking - [x] If this is a large change, bug fix, or code improvement, I checked there is a [JIRA issue](https://issues.apache.org/jira/browse/CAMEL) filed for the change (usually before you start working on it): [CAMEL-27357](https://issues.apache.org/jira/browse/CAMEL-27357). # Apache Camel coding standards and style - [x] I checked that each commit in the pull request has a meaningful subject line and body. - [ ] I have run `mvn clean install -DskipTests` locally from root folder and I have committed all auto-generated changes. (I built and tested camel-a2a, including the formatter and import-sort plugins. No option and no doc changed. I did not run the full root build.) # AI-assisted contributions - [x] If this PR includes AI-generated code, commits have proper co-authorship attribution (e.g., `Co-authored-by` trailers) and the PR description identifies the AI tool used. This PR was prepared with Claude Code (Claude Opus 5.5). The commit carries a `Co-Authored-By` trailer. _Claude Code on behalf of allthingssecurity_ 🤖 Generated with [Claude Code](https://claude.com/claude-code) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
