allthingssecurity opened a new pull request, #30700: URL: https://github.com/apache/camel/pull/30700
# Description [CAMEL-27382](https://issues.apache.org/jira/browse/CAMEL-27382) With a done file with a fixed name (`doneFileName=ready`), a poll that leaves files of the directory for the next poll (beyond `maxMessagesPerPoll`, or not started) keeps the done file (CAMEL-25728), also when its last file completes on another thread while the next poll runs (CAMEL-25960). Two cases still deleted the kept done file while the last file of that poll was routing on another thread (a `seda` hand-over, an async route): - **Stopping the consumer.** `doStop` called `GenericFileKeptDoneFiles.clear()`, which emptied the current set in place. Since CAMEL-25960 the files of a poll refer to that same set object, so the last file of the poll, completing after the stop, found the done file no longer kept and deleted it. After a restart the files the poll left had no done file and were never consumed. Nothing was logged. Example: `maxMessagesPerPoll=1`, `a.txt`, `b.txt`, `ready`; the poll hands `b.txt` to `seda:work`, the file route is stopped (it has no exchange in flight, so it stops at once), `b.txt` completes and deletes `ready`, and `a.txt` stays. - **A file still in progress, with `recursive=true`.** The listing skips a file that is in progress, so the next complete listing found no file of that file's directory and deleted its kept done file as "gone" (CAMEL-25728) while the file was still routing. When that file then failed and was rolled back, it stayed without its done file for good, although the documented rule is that the last file of a poll that fails keeps its done file. Example: `recursive=true&maxMessagesPerPoll=1`, `d1/a.txt` and `d2/b.txt` with their done files; the first poll takes `d1/a.txt` (async) and keeps both done files, the second poll lists only `d2/b.txt` and deletes `d1/ready`, then `d1/a.txt` fails. Both are regressions of the kept done files (CAMEL-25728, CAMEL-25960). Change (`GenericFileKeptDoneFiles`, `GenericFileConsumer`): - `clear()` points `kept` to a new set instead of emptying it, as `startBatch` already does for a complete listing. The files of the last poll keep the set of their poll, so they still find their done file kept. The consumer forgets the kept done files when it stops, as before. - `isValidFile` records the done file of a file it skips because it is in progress (only when kept done files are in use). `startBatch` does not return such a done file as gone: it stays kept, and the next complete listing decides again. Once the file completed, a listing that finds no file of its directory deletes the done file as gone, as before; a file that failed is listed again with its done file. A done file whose directory has files listed is decided by the files of the poll, as before. The FTP, SFTP, SMB and Azure Files consumers share `GenericFileConsumer`, so they get the same change. Dynamic done file names (`${file:name}.done`), `noop=true` and a custom processor (`pollEnrich`) do not use the kept done files and are not affected. The docs already say the done file is kept until the files left are consumed, so they are not changed. Model: found with a TLA+ model of the kept done files (polls, the files of each poll routing on other threads, failures, read locks, stop/start, a producer that waits for the done file to go, one or two directories, eager and non-eager limits). I extended it with the second change (`CarryInProgress`) next to the first (`ClearInPlace`). On main it finds the stopped consumer that leaves a file without its done file (`NoStuck`, 66 to 1047 distinct states depending on the scope) and the gone delete of a directory whose file is still routing (`GoneSafe`, 13 states; `LastFailedKeepsDone` after that file fails, 40 states). With both changes all of these hold, at scopes up to 4 files, 2 directories, 5 polls, a failure, a read lock miss and a stop (everystate, up to 142,895 distinct states; TLC confirms the same counts on 5 configurations). Each change alone leaves the other case failing. One violation remains in both versions, with an async route, a failure and a producer: two last files of one directory in flight both delete the done file, and the second delete can remove the producer's next done file. That is older than the kept done files and is not changed here (see below). Tests: - New `FileConsumerDoneFileNameKeptWhileRoutingTest` (camel-core, package-private): - the consumer is stopped while the last file of the poll waits on a seda route: once that file completed, the done file must still be there, and after the restart the file left must be consumed and the done file deleted; - `recursive=true&maxMessagesPerPoll=1`, the first file routes on another thread while the next poll consumes the file of the other directory: the done file of the routing file must still be there; that file then fails, and must be consumed again and its done file deleted. - `GenericFileKeptDoneFilesTest` (camel-file): `clear()` leaves a batch of the last poll its kept done file; a done file of a file in progress is not gone, and is gone at the next listing that finds no file of it in progress. On main both new camel-core tests fail (the done file is gone in each case, with the main versions of the two classes compiled in front). With the change: camel-file 49 tests and the file tests of camel-core (`org.apache.camel.component.file.**`) 554 run, 12 skipped, 0 failures. The new test with `FileConsumerDoneFileNameAsyncLastFileTest` and `FileConsumerDoneFileNameFilesLeftTest` 20 times in one JVM: 340 run, 0 failures. The camel-ftp done-file tests were not run locally (CI runs them). Formatter and import-sort checks pass. Not changed: - With an async route, two last files of one directory can be in flight at the same time (a failed file is listed again while the last file of the previous poll still routes). Both delete the done file, and a producer that writes its next files once the done file is gone can lose its new done file to the second delete. This is older than the kept done files, needs no stop or recursion, and a fix needs some identity of the done file per batch; I can open a separate JIRA. - A done file whose directory has files listed is decided by the files of the poll, as before, also while a file of an earlier poll in that directory still routes: when the last file of the later poll completes first, it deletes the done file, and if the earlier file then fails it has no done file (the same happens without the kept done files). Keeping such a done file as well leaves it behind when the consumer stops before the next complete listing (the model finds that), so it is not changed here. - With `recursive=true`, a poll deletes only the done file of its last file's directory; the done files of the other directories it consumed stay (CAMEL-25728 left this as before). A separate PR (a poll deletes the done file of each directory it consumed) fixes that; the two change different parts of `GenericFileConsumer` and merge cleanly in either order. # 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-27382](https://issues.apache.org/jira/browse/CAMEL-27382). # 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]
