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

   # Description
   
   [CAMEL-27413](https://issues.apache.org/jira/browse/CAMEL-27413)
   
   `SedaConsumer.doStop` shuts its executor down with `shutdownNow`: it 
interrupts the polling threads and does not wait for them. When the same 
consumer instance is started again while a thread is still processing an 
exchange, that thread sees `isRunAllowed() == true` and goes on polling. This 
happens when a route policy stops and starts the consumer 
(`RoutePolicySupport.stopConsumer` / `startConsumer`, which 
`HazelcastRoutePolicy`, `MasterRoutePolicy` and `DnsActivationPolicy` use on a 
leadership change; `HazelcastRoutePolicy.onResume` does a stop and a start in a 
row). A stop and start through the route controller is not affected, as the 
route creates a new consumer.
   
   Three effects:
   
   - More polling threads than `concurrentConsumers`. `doStart` creates a latch 
of `concurrentConsumers`, but `setupTasks` starts only `max(concurrentConsumers 
- taskCount, 1)` new tasks. With `concurrentConsumers=2` and both threads busy 
at the stop, 3 threads poll after the start (3 exchanges are processed at the 
same time), and the latch counts 2. A stop and start right after a start, 
before the new threads ran, gives up to twice the threads.
   - A graceful shutdown that does not wait. With 3 live threads and a latch of 
2, the 2 idle threads end on `shutdownPending` and release the latch, so 
`prepareShutdown` returns while the third thread still processes an exchange. 
The shutdown strategy then stops the consumer (`shutdownNow` interrupts that 
thread) and prepares the route services for shutdown.
   - A thread that spins. When the processor does not clear the interrupt of 
the stop (a blocking call that cannot be interrupted, CPU work), the thread 
goes on after the start with its interrupt flag set: every `queue.poll` throws 
`InterruptedException` at once and the catch sets the flag again. The thread 
uses a whole CPU, never takes an exchange again, and never reaches the "empty 
poll and shutdown pending" exit, so every later graceful shutdown waits for its 
full timeout although nothing is pending or in flight.
   
   CAMEL-22390 was about the same loop when user code interrupts a SEDA thread 
(closed: users should not interrupt Camel threads). Here the interrupt comes 
from Camel's own stop.
   
   The fix makes each start of the consumer own its polling threads:
   
   - Each start submits its own polling tasks (`concurrentConsumers` of them, 
at least one). A task polls only while the start that submitted it is the 
current one, so a thread of an earlier start ends after the exchange it is 
processing, and its interrupt flag no longer matters. A task that only begins 
to run after the next start (the stop and start right after a start) ends at 
once.
   - Each task counts down the latch of its own start, so the latch of the 
current start counts exactly its threads.
   - `prepareShutdown` also waits (within the same shutdown timeout) for the 
threads of earlier starts that are still processing an exchange.
   - `taskCount` and the `max(concurrentConsumers - taskCount, 1)` arithmetic 
are gone. `ThreadPerTaskSedaConsumer` used to replace the latch after 
`setupTasks` had submitted its tasks; it now says through the new 
`getPollingTasks()` that it runs one coordinator thread (before, with 
`concurrentConsumers` set, `setupTasks` queued that many coordinators on its 
single thread executor, and only the first one ran until the shutdown).
   - `doRun()` (protected) now takes the start of the task: `doRun(int start)`. 
No subclass in Camel overrides it.
   
   `doStop` still does not wait for the threads, so a stop through a route 
policy is not slower than before.
   
   Model: found with a TLA+ model of the polling threads, the shutdown 
strategy, stop and start of the same instance, suspend and resume 
(`SedaShutdown.tla`, adapted with a `Fixed` flag). On main it finds more 
threads than `concurrentConsumers` (`PollersMatch`), a graceful shutdown that 
stops the consumer while a thread holds an exchange (`NoStopWhileProcessing`), 
and a thread that fails two polls on its interrupt flag (`NoSpin`, 
`NoStaleInterrupt`). With the fix all hold, together with `NoLoss`, a latch 
that counts exactly the live threads of its start (`LatchesMatch`), and 
`LatchReleasesFixed` (2 consumers, 2 messages, 2 starts, 2 stops, a suspend and 
a shutdown timeout: 5,186,013 distinct states, everystate and TLC agree). A 
vacuity check shows that a thread of an earlier start still holding an exchange 
after the start is reachable with the fix.
   
   Tests: new `SedaConsumerRestartWhileProcessingTest` 
(`concurrentConsumers=2`, the consumer stopped and started with `ServiceHelper` 
while the processor holds the threads and ignores the interrupt):
   
   - after the restart, the threads of the first start end, and with 3 
exchanges at most 2 are processed at the same time;
   - after the restart, `prepareShutdown` waits for the exchange being 
processed;
   - `prepareShutdown` waits for a thread of the first start that still 
processes the exchange it polled before the stop;
   - a thread that keeps the interrupt flag of the stop ends after its 
exchange, the next 10 messages are processed, and `prepareShutdown` with 
nothing pending returns at once instead of waiting for the 10 second timeout.
   
   On main all 4 fail: the first with 3 exchanges processed at the same time, 
the two `prepareShutdown` tests because it returns while the exchange is still 
processed, the last because the old thread never ends (it spins). With the 
change the SEDA tests of camel-core pass (154 run, 0 failures), and so do the 
cluster, route policy and throttling tests that stop and start consumers (168 
run, 0 failures). `SedaConsumerRestartWhileProcessingTest` with 
`ThreadPerTaskSedaConsumerTest`, `ThreadPerTaskSedaConsumerStopTest`, 
`SedaConsumerSuspendResumeTest` and `SedaSuspendedRouteWithPendingStopTest` 20 
times in one JVM: 240 run, 0 failures.
   
   Not changed:
   
   - A stop through a route policy still interrupts the exchanges in process, 
as before (only the next start no longer keeps their threads).
   - `ThreadPerTaskSedaConsumer` creates a new concurrency semaphore at each 
start, and tasks dispatched by the previous start release their permit into the 
new one. That is a separate issue in the virtual thread consumer, which I can 
file as a follow-up.
   
   # 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-27413](https://issues.apache.org/jira/browse/CAMEL-27413).
   
   # 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 the affected modules, including the formatter and 
import-sort plugins. 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 commits carry 
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]

Reply via email to