allthingssecurity opened a new pull request, #30756: URL: https://github.com/apache/camel/pull/30756
# Description [CAMEL-27412](https://issues.apache.org/jira/browse/CAMEL-27412) With a recoverable aggregation repository, `AggregateProcessor.RecoverTask.run` ended with: ```java } finally { recoveryInProgress.set(false); inProgressCompleteExchangesForRecoveryTask.clear(); } ``` CAMEL-26642 made a recover check start only when no other check runs (`recoveryInProgress.compareAndSet(false, true)`), because a route stop does not wait for a running check. But the flag went down before the shared snapshot of the exchanges in progress was cleared. When the route was stopped and started while the old check was running, the recover task of the new start could start in that gap: ``` old check: ends its loop (route stopped), finally: recoveryInProgress = false new check: compareAndSet wins, snapshot.clear(), snapshot = {in progress, unconfirmed} = {X} old check: finally: snapshot.clear() -> the snapshot of the new check is empty new check: scan -> {X}, X not in the snapshot -> recover X and send it ``` So an exchange still being processed was processed a second time (with `parallelProcessing`, by two threads at the same time), and an exchange that was processed and whose confirm returned false (kept as unconfirmed) was sent again. The change swaps the two statements: the old check clears the snapshot and only then lets a new check start. Each check still clears the snapshot first thing after it wins the compareAndSet, so an id that a completion adds between the `clear()` and `set(false)` (`onSubmitCompletion` adds to the snapshot while `recoveryInProgress` is true) is dropped by the next check before it reads the snapshot. It needs a route restart while a check runs, and the old check must end right when the first check of the new start (1 s after the start) takes its snapshot, which is more likely when a scan or recover pass is slow. Found with a TLA+ model of the recover task, the completions and a route restart. On main the model finds an exchange processed by two threads at once and a processed exchange recovered again (Ids {1,2}, one restart). With the change all the properties hold up to 2 ids, 2 restarts, 2 failures and 2 failed confirms (everystate and TLC: 4,656,066 distinct states), and every recovered exchange is eventually confirmed (liveness, 204,287 states). Tests: `AggregateRecoverRestartSnapshotClearTest` and `AggregateRecoverRestartSnapshotClearParallelTest` (`parallelProcessing`), with an in-memory recoverable repository (as in `AggregateRecoverRestartTest`). There is no hook between the two statements, so the test replaces the snapshot set (a private field, by reflection) with a set that behaves the same and can hold the old check inside the `clear()` of its finally block. While it is held, the test restarts the route, waits until the recover task of the new start has run once (through a recording `ThreadPoolFactory`, as in `AggregateRecoverRestartTest`), then lets the old check end. No sleeps. - an exchange processed with a failed confirm must not be recovered or sent again; - an exchange still being processed must not be recovered, and is processed once (with `parallelProcessing`, never by two threads at once). On main all 4 tests fail (the exchange is recovered: `recovered=1`). With the change the camel-core processor tests (`org.apache.camel.processor.**`) pass: 3361 run, 1 failure, 17 skipped. The failure is `BacklogTracerCalledRouteTimestampTest` (a timestamp comparison), which fails the same way on other branches and does not touch the aggregator. The new tests with the existing recovery tests (`AggregateRecoverRestartTest` of CAMEL-26642, `AggregateRecoverInProgressTest`, `AggregateRecoverableConfirmFailureTest`) 20 times in one JVM: 320 run, 0 failures. # 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-27412](https://issues.apache.org/jira/browse/CAMEL-27412). # 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]
