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

   ## Summary
   
   Fix a TOCTOU race condition in `GenericFileConsumer.isValidFile()` that 
allowed a file to be routed twice when the exchange was committed on a 
different thread.
   
   ## Root Cause
   
   The race unfolds in three steps:
   
   1. The poll thread lists the directory and finds a file that is not yet in 
the in-progress repository — the `contains()` check passes.
   2. A separate thread (e.g. from a `threads()` or `disruptor:` EIP) commits 
the exchange: `GenericFileOnCompletion` moves the file to `.camel` and removes 
it from the in-progress repository.
   3. The poll thread now calls `inProgressRepository.add()` — which 
**succeeds** because the entry was removed in step 2 — and routes the 
already-moved file a second time. The second commit then fails with *"Cannot 
rename file"*.
   
   This was first observed as a flaky failure in 
`FileDisruptorShutdownCompleteAllTasksTest` (CAMEL-25647) and tracked 
separately as CAMEL-25778.
   
   ## Fix
   
   After `inProgressRepository.add()` returns `true`, verify the file still 
exists on disk by calling `operations.existsFile(absoluteFilePath)`. If the 
file is gone (moved away by a concurrent commit), remove it from the 
in-progress repository and return `false` — skipping the file without routing 
it a second time.
   
   If `existsFile()` throws `GenericFileOperationFailedException` (e.g. I/O 
error), we log at DEBUG and continue: `GenericFileOnCompletion.onCompletion()` 
always removes the entry from the in-progress repository in its `finally` 
block, so there is no in-progress leak, and the worst outcome is the existing 
*"Cannot rename file"* warning on commit.
   
   ## Tests
   
   Added `FileConsumerAsyncCommitRaceTest` to `camel-core`: creates 50 files 
and routes them through an async `threads()` path with a fast poll interval (5 
ms) and `maxQueueSize(0)` (SynchronousQueue), which is the same topology that 
triggers the race. Asserts that no file name is counted more than once by the 
processor. The test is timing-sensitive by nature (race reproduction depends on 
thread interleaving) and serves as a regression guard.
   
   Fixes CAMEL-25778
   
   _Hermes Agent (Claude Sonnet 4.6) on behalf of Guillaume Nodet_
   


-- 
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