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

   # Description
   
   [CAMEL-27414](https://issues.apache.org/jira/browse/CAMEL-27414)
   
   `ManagedRoutePolicy.onRemove` takes `lock`, which resolves to the policy's 
own lock (inherited from `BaseService`), not to the lock of 
`DefaultSupervisingRouteController`. So `removeRoute` (and a route reload, 
which removes the routes) releases the route without the controller lock, and 
the release can land inside two check-then-act steps that the controller does 
under its lock:
   
   - The completion of an exhausted restart task checks under the controller 
lock that the route is stopped and that the task is still its restart task, 
logs the WARN, and then puts the route into the exhausted routes. When 
`removeRoute` releases the route between the check and the put, the removed 
route is put into the exhausted routes, and nothing takes it out again: 
`hasUnhealthyRoutes()` stays true (with `unhealthyOnExhausted`, the default), 
so the route controller readiness check (`camel/route-controller`) stays DOWN 
until the JVM restarts, `getExhaustedRoutes()` lists a route that does not 
exist, and `getRestartingRouteState(id)` returns its dead task.
   - A manual `startRoute` that fails releases the route lock of the 
CamelContext and then installs a restart task for the route. When `removeRoute` 
removes the route in between, its release finds no task, and the start then 
installs a restart task for the removed route: the route is reported restarting 
(and the controller unhealthy, with `unhealthyOnRestarting`, the default) until 
the next attempt, one `backOffDelay` later. That attempt starts the route by 
id, which is a no-op for a removed route, or starts a route added again with 
the same id (a reload).
   
   Taking the controller lock in `onRemove` is not the fix: `removeRoute` holds 
the route lock of the CamelContext there, and a manual start or stop holds the 
controller lock while it waits for the route lock, so the two would deadlock 
(`DefaultSupervisingRouteControllerRemoveRouteDeadlockTest.testRemoveStoppedRouteWhileAnotherRouteIsBeingStopped`
 forces that order). The model shows the same.
   
   The fix keeps `onRemove` as it is and makes the two steps check afterwards 
that the route is still controlled. `onRemove` removes the route from the 
controlled routes before it releases it, so for each step either the release 
comes after it (and removes what the step added), or the check sees the route 
gone:
   
   - The exhaustion completion puts the route into the exhausted routes, then, 
if the route is no longer controlled, removes it again 
(`exhausted.remove(route, task)`) and skips the exhaustion event and the route 
error.
   - `RouteManager.start` installs the restart task, then, if the route is no 
longer controlled, releases it (removes and cancels the task).
   - A comment in `onRemove` says that its `lock` is the lock of the policy on 
purpose.
   
   Prior fixes in this class (CAMEL-25008, CAMEL-25010, CAMEL-26448, 
CAMEL-26885, CAMEL-27170) are kept: no lock is added and no lock order changes.
   
   Model: found with a TLA+ model of one supervised route, its restart tasks, a 
management thread (start, stop), `removeRoute` and the CamelContext stop, with 
the controller lock, the route lock and stale reads of the task status 
(`SupervisingRestart.tla`, adapted with a `Fixed` flag). On main it finds a 
removed route that stays exhausted once every thread is idle 
(`RemovedNotExhaustedQ`) and a removed route with a restart task 
(`RemovedNotRestarting`). With the fix both hold, together with 
`NoStartAfterManualStop`, `NotBothQuiescent`, `ExhaustedNotStartedUnmanaged`, 
`NoLockCycle` and no deadlock (2 restart tasks, 2 attempts, 3 manual 
operations, removeRoute and the CamelContext stop: 8,544,442 distinct states 
with everystate; the smaller scopes also with TLC, same counts). A vacuity 
check shows that an exhausted route is still reachable with the fix.
   
   Tests: new `DefaultSupervisingRouteControllerRemoveRouteRaceTest` 
(`@Isolated`: the first test adds an appender to the logger of the controller, 
and the supervising tests of other classes, which camel-core runs at the same 
time, log the same exhaustion WARN):
   
   - the scheduler thread is held in the exhaustion WARN (a log4j appender), 
after the checks under the controller lock and before the put; `removeRoute` 
completes meanwhile (it does not wait for the controller lock), then the 
completion goes on: the removed route must not be exhausted, must have no 
restart state, and the controller must not report unhealthy routes;
   - a manual `startRoute` fails and is held in its restarting-failure event, 
after the CamelContext released the route lock and before the restart task is 
installed; the route is removed meanwhile: the removed route must not be 
restarting, and the controller must not report unhealthy routes.
   
   On main both fail (the removed route is reported exhausted, and restarting). 
With the change the `org.apache.camel.impl.engine` tests of camel-core and 
`SupervisingRouteControllerSplitOnExceptionTest` pass (219 run, 0 failures, 1 
skipped), `DefaultSupervisingRouteControllerRemoveRouteDeadlockTest` among 
them. The new class with the other supervising race tests 
(`RemoveRouteDeadlock`, `ExhaustedRemove`, `ExhaustedStart`, 
`StartWhileRestarting`, `StopWhileRestarting`) 20 times in one JVM: 200 run, 0 
failures.
   
   Not changed:
   
   - For a moment, the exhaustion completion has put the route into the 
exhausted routes and not yet removed it from the restarting routes, so 
`getRestartingRoutes()` and `getExhaustedRoutes()` can both list it (they read 
without the lock). Removing it from the restarting routes first would instead 
let the controller report no unhealthy route for that moment, which is worse 
for a health check; settling both would need the getters to take the lock. I 
left it as it is.
   - If the start path itself throws an `Error` (for example 
`NoClassDefFoundError` from a consumer), the restart task stops without 
completing; that is a separate issue.
   
   # 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-27414](https://issues.apache.org/jira/browse/CAMEL-27414).
   
   # 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