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

   # Description
   
   [CAMEL-27091](https://issues.apache.org/jira/browse/CAMEL-27091)
   
   `GridFsConsumer.doStart` runs `run()` on a thread of its own executor. 
`run()` builds a `BackgroundTask` with an unlimited duration and calls 
`task.run(...)`, which schedules the polls on `taskExecutor` and waits on the 
latch of the task until the task ends. A poll never ends the task: 
`processCollection` returns `false` whatever it found, and a failed poll 
returns `false` too. Only an exception thrown out of a poll counted the latch 
down.
   
   `doStop` shuts `taskExecutor` down, which cancels the scheduled polls, and 
then shuts down the executor without interrupting it. Nothing releases the 
thread in `run()`: each stop of the consumer (a route stop and start, a route 
reload) leaves one thread waiting for ever, and the task stays in the task 
registry.
   
   Found with a TLA+ model of the thread in `run()`, the poll thread and the 
stop (TLC). On main the invariant "after the stop, no thread waits on a latch 
that nothing can count down" is violated in 25 states (start, polls, stop: the 
thread waits for ever), and no behaviour of main reaches a stopped consumer 
whose `run()` has returned. A first fix that keeps `task.run` and cancels the 
task in `doStop` holds that invariant but violates "a stop does not interrupt a 
poll": when the latch is released, `waitForTaskCompletion` cancels the 
scheduled future with interrupt, which interrupts a poll that is routing files. 
The fix below holds both, and "the task is not left in the task registry" (52 
distinct states); the witnesses (polls, a stop after polls, a stop before 
`run()`) are reachable.
   
   Fix, in `GridFsConsumer`:
   - `run()` schedules the polls with `task.schedule(...)` instead of running 
the task and waiting for it, and keeps the task in a volatile field; it does 
not schedule when the consumer is already stopping (the stop could not have 
seen the task);
   - `doStop` cancels the task without interrupt, so a poll that runs finishes 
the files it found, as before, and the task leaves the task registry.
   
   The polls themselves are unchanged: same thread, same delay, same budget.
   
   Tests: `GridFsConsumerTimestampTest.stopLeavesNoThreadWaitingForThePolls` 
(fake bucket of the existing tests): start the consumer, wait for two polls, 
stop it, and check that no thread of the consumer is left in `run()`. Without 
the change it fails after 20 seconds (the thread `mongodb-gridfs:fake` is still 
waiting); with the change the camel-mongodb-gridfs unit tests pass (51 run, 0 
failures), and `GridFsConsumerTimestampTest` passes 20 times in one JVM 
(920/920). The integration tests (container) were not run locally, CI runs 
them. Built with the cached MongoDB driver 5.13.0 (main uses 5.14.0, not in my 
offline repository).
   
   # 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-27091](https://issues.apache.org/jira/browse/CAMEL-27091).
   
   # 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 the core modules and camel-mongodb-gridfs with the formatter and 
import-sort plugins; no option, doc or generated file changes. 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]

Reply via email to