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

   # Description
   
   [CAMEL-27385](https://issues.apache.org/jira/browse/CAMEL-27385)
   
   `ScheduledPollConsumer.doStop` does not wait for a poll that is running: it 
cancels the tasks and shuts the executor down. CAMEL-25109 added the 
`generation` counter so that such a poll does not write the state of the 
consumer after the stop. That guard is check-then-act without a lock, and some 
writes have no guard at all. When the route is stopped and started while a poll 
is still running:
   
   - `forceConsumerAsReady()` has no generation check. Many components call it 
inside `poll()` once their remote call returns (aws2-sqs, aws2-s3, 
aws2-kinesis, minio, couchbase, the ftp consumers, ...). If the route is 
restarted during that call (an SQS long poll can take 20 s), the old poll marks 
the restarted consumer ready before it has polled. The readiness check then 
reports UP before that, and stays UP while the first poll of the restarted 
consumer hangs (unless `healthCheckMaxPollDuration` is set, CAMEL-26998); only 
a failed poll turns it DOWN, through the error counter.
   - With `repeatCount`, a run from before the stop that has decided to stop 
firing calls `scheduler.unscheduleTask()` after the restart. The scheduler 
object is reused, so it cancels the tasks of the restarted consumer: the 
consumer is Started but never polls again, and nothing reports it. Its 
`counter.incrementAndGet()` can also use up the repeat count of the restarted 
consumer.
   - A stop and start between the generation check and the writes of a poll 
(also in the greedy branch and in `onPolled`) leaves `firstPollDone`, the error 
counter and `lastErrorDetails` of the old poll in the restarted consumer.
   - The backoff block writes `backoffErrorBase` and `backoffCounter` without a 
generation check, so the error backoff of the restarted consumer can start 
later than configured.
   
   The change:
   
   - One private lock in the consumer (`stateLock`). `doStop` increments the 
generation and clears the state under it, as one step.
   - A run takes the lock, checks that the generation is still the one it read 
when it started, and only then does the backoff block, the repeat count and 
`unscheduleTask()`. A run that sees the consumer was stopped since it started 
returns there (before, it went on and found out after the poll). 
`unscheduleTask()` runs under the lock, so it can only cancel the tasks of the 
start the run belongs to. The three scheduler implementations in the repo 
(default, Spring, Quartz) only cancel their futures or unschedule the trigger 
there, and none waits for a running poll (Quartz with a JDBC job store makes 
one database call).
   - The greedy writes, `onPolled` and the final writes of a run check the 
generation and write under the same lock.
   - `doRun` keeps the generation of the poll in a thread local around 
`poll()`. `forceConsumerAsReady()` called from that poll checks the generation 
under the lock and does nothing when the consumer was stopped since the poll 
started. A call from another thread (not from a poll of this consumer, for 
example `GenericFilePollingConsumer` calling `poll(Exchange)` itself) works as 
before.
   - The lock is never held while polling, while the poll strategy runs, while 
an empty message is processed, or while the exception handler runs. 
Uncontended, it adds a few lock/unlock pairs per poll.
   
   No protected or public API changes; the javadoc of `forceConsumerAsReady()` 
says what it does for a poll of before a stop. Most of the diff is indentation 
(try/finally around the existing blocks); it reads best with whitespace ignored.
   
   Model: found with a TLA+ model of the scheduled runs against 
stop/start/suspend/resume of the same consumer (generation guard, backoff, 
greedy, `repeatCount`, `forceConsumerAsReady`). I added a switch for this fix 
and ran the same configurations on both versions (everystate, a subset 
confirmed with TLC; 2-3 runs, 1-2 restarts, 1 suspend, 1 failing poll). On main 
it finds a stale force-ready, a lost schedule with `repeatCount`, stale 
`firstPollDone` and error state in the restarted consumer, and a backoff base 
above the error counter. With the fix all of these hold, alone and together 
(every option at once, 3 runs and 2 restarts: 540,885 states), and the schedule 
is kept in every configuration as before. Probes that must fail still fail on 
the fix: a current poll still makes the consumer ready, still forces it ready, 
and a used up repeat count still cancels the schedule.
   
   Tests: new `ScheduledPollConsumerStaleRunTest`. The polls are run by the 
test on a thread (like `ScheduledPollConsumerEdgeCasesTest`) and held with 
latches at the step of the model trace:
   
   - `forceReadyOfPollFromBeforeStopIsIgnored`: the poll is held inside its 
remote call, the consumer is stopped and started, then the poll returns and 
calls `forceConsumerAsReady()`. The restarted consumer must not be ready; a 
poll of the restarted consumer then makes it ready.
   - `stateOfPollFromBeforeStopIsNotKept`: a failing poll (HTTP 503) is held 
after its generation check, and the consumer is stopped and started on another 
thread. `firstPollDone`, the error counter and `lastErrorDetails` of the 
restarted consumer must be clear.
   - `repeatCountOfRunFromBeforeStopDoesNotUnscheduleRestartedConsumer`: with 
`repeatCount=1`, the second run is held just before it calls 
`unscheduleTask()`, and the consumer is stopped and started on another thread. 
The restarted consumer must keep its scheduled task, and its counter must be 0.
   
   On main the 3 tests fail (the main version of `ScheduledPollConsumer` 
compiled in front of the classpath): the restarted consumer is ready, has 
`firstPollDone`, and has lost its scheduled task. With the fix they pass, also 
20 times in a row in one JVM together with the other 
`ScheduledPollConsumer*Test` classes (500 of 500; 
`ScheduledPollConsumerBackoffTest` left out of the repeat, as it keeps its 
counters in static fields). Module tests with the fix: camel-support (178, two 
surefire runs), camel-file (47), and camel-core `org.apache.camel.impl`, 
`support`, `component.file`, `component.scheduler` and `component.timer` 
(2232), no failures.
   
   There is no upgrade guide entry: nothing changes for a consumer that is not 
restarted while a poll runs.
   
   Not changed (separate JIRAs, found by the same model run):
   
   - A stopped `EventDrivenPollingConsumer` whose `receive` is still running 
suspends the stopped consumer in `afterPoll`, and after a restart that consumer 
never polls again (S4).
   - `doStop` does not wait for a running poll, so after a restart the old poll 
and the first poll of the restarted consumer can run at the same time with 
`concurrentConsumers=1` (S5, a known consequence of the design, see the comment 
on `generation`). This change only stops a run of before the stop that has not 
started its poll yet.
   - A run that is suspended between its `isSuspended()` check and 
`isPollAllowed()` does not poll, but counts as a successful poll and sets 
`firstPollDone` (S6).
   
   # 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-27385](https://issues.apache.org/jira/browse/CAMEL-27385).
   
   # 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