davsclaus opened a new pull request, #30796:
URL: https://github.com/apache/camel/pull/30796

   ## Description
   
   [CAMEL-27499](https://issues.apache.org/jira/browse/CAMEL-27499) (Threads 
EIP) and [CAMEL-27501](https://issues.apache.org/jira/browse/CAMEL-27501) (Wire 
Tap EIP) have the same root cause. With the `CallerRuns` rejected policy (the 
default), a thread pool that is shut down neither runs a new task nor throws: 
Camel's `CallerRuns` handler is the JDK's 
`ThreadPoolExecutor.CallerRunsPolicy`, which discards the task when the pool is 
shut down. So:
   
   - **Threads EIP**: `process` returns false and waits for a callback that 
never comes. The caller (a `ProducerTemplate` request, a consumer thread) waits 
for ever, the exchange stays in flight, and a graceful shutdown waits its full 
timeout. This happens with `threads().executorService(sharedPool)` when the 
owner shuts the pool down, when the route is removed while an exchange is being 
handed over, and with a JDK pool (`Executors`): its `AbortPolicy` throws, and 
the EIP's own `CallerRuns` policy then drops the call too.
   - **Wire Tap EIP**: the tap is lost with nothing logged, and it stays 
counted in `getPendingExchangesSize()`, so every graceful stop of the route 
waits until the shutdown timeout. With the `Block` policy, a caller released by 
the `shutdownNow` of the pool (the drain makes space) puts its task into the 
queue of a pool that no longer runs it.
   
   ### Fix
   
   The tickets propose per-EIP claim flags and checks after the submit. This PR 
fixes the root cause in `ThreadPoolRejectedPolicy` instead:
   
   - **CallerRuns**: on a pool that is shut down, a `Rejectable` task is 
rejected, as the `Abort` and `Block` policies already do. A plain task is still 
dropped, as with `ThreadPoolExecutor.CallerRunsPolicy`, so nothing changes for 
tasks that are not `Rejectable`. Today only the tasks of the Threads EIP and 
the Multicast EIP are `Rejectable`, and the Multicast EIP hands plain lambdas 
to its pool.
   - **Block**: after the blocking `put`, when the pool was shut down meanwhile 
and the task can still be taken out of the queue, it is rejected (the same 
recheck `ThreadPoolExecutor.execute` does).
   - **Threads EIP**: no change needed. Its `ProcessCall.reject()` already 
fails the exchange with a `RejectedExecutionException` and calls the callback 
once. The pool decides between running and rejecting inside `execute`, so a 
task is never both run and rejected.
   - **Wire Tap EIP**: its task is now `Rejectable`. A rejection makes the 
submit fail, so `process` takes the tap off the pending count and sets the 
`RejectedExecutionException` on the exchange, so the error handler can react. 
That is what the `Abort` policy already did (`WireTapAbortPolicyTest`). It 
differs from CAMEL-27501, which proposed to log a WARN and leave the original 
exchange alone. I kept the existing Wire Tap design: a tap the pool refuses 
fails the exchange.
   
   The threading model page said `CallerRuns` never loses a task. It now 
explains what happens with a thread pool that is shut down.
   
   Not covered: a JDK thread pool configured with the JDK's own 
`CallerRunsPolicy` still drops the task. The EIP cannot see that from the 
outside.
   
   ## Tests
   
   - New `ThreadPoolRejectedPolicyTest` (camel-util): CallerRuns rejects a 
`Rejectable` task of a shut down pool (with `execute` and `submit`), drops a 
plain task, and still runs the task in the caller when the pool is full. Block 
rejects a task put into a pool that was shut down while the caller waited. 3 of 
the 5 tests fail on main.
   - New `ThreadsShutDownPoolTest`: with a Camel pool and with a JDK pool that 
is shut down, the exchange fails with a `RejectedExecutionException` and 
nothing is left in flight. On main both time out after 10s.
   - New `WireTapShutDownPoolTest`: the exchanges fail with a 
`RejectedExecutionException`, and nothing is pending. On main 3 taps stay 
pending.
   - All camel-util tests (361), and the related camel-core tests (889: 
`Threads*`, `WireTap*`, `*ThreadPool*`, `*Rejected*`, `*Shutdown*`, 
`AsyncDelayed*`, `Delay*`, `Throttl*`, `Multicast*`, `*Parallel*`) pass.
   
   _Claude Code on behalf of Claus Ibsen (davsclaus)_
   
   🤖 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]

Reply via email to