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

   # Description
   
   [CAMEL-27528](https://issues.apache.org/jira/browse/CAMEL-27528)
   
   `DisruptorReference.start()` and `shutdownDisruptor()` wait for each event 
handler of the disruptor to start (stop) with `awaitStarted(10, SECONDS)` / 
`awaitStopped(10, SECONDS)`. The wait sits in a loop that catches 
`InterruptedException`, logs "Interrupted while waiting for the startup to 
complete" (or "... the shutdown ...") at INFO, sets the interrupt flag again 
and waits again:
   
   ```java
   } catch (InterruptedException e) {
       LOGGER.info("Interrupted while waiting for the startup to complete");
       Thread.currentThread().interrupt();
   }
   ```
   
   `AbstractLifecycleAwareExchangeEventHandler.awaitStarted(timeout)` is 
`started || startedLatch.await(timeout, unit)`, and `CountDownLatch.await()` 
throws at once while the interrupt flag is set. So once the thread is 
interrupted while an event handler has not started (or not stopped) yet, every 
iteration throws at once: the thread spins at full CPU with the lock of the 
disruptor held, logs one INFO line per iteration, and the 10 s limit, meant to 
keep the application from hanging, never applies. It ends only when the event 
handler starts (stops), and never if it does not. A test run on main wrote 
about 2 GB of log within a minute this way: in the teardown of a test whose 
next start could not run its event handler, the CamelContext stop timed out and 
interrupted the shutdown task while it waited.
   
   ## The fix
   
   Both waits go through one method, `awaitEventHandlers(handlers, start)`, 
with the same 10 s limit per event handler and the same ERROR line when it 
passes. It never waits again with the interrupt flag set (the wait that throws 
`InterruptedException` clears the flag), logs one WARN line, and sets the flag 
again for the caller at the end. What an interrupt does then differs between 
the two waits:
   
   - the wait for the start goes on, without interruption, for the rest of the 
10 s limit (the deadline is fixed when the wait for an event handler begins, a 
new interrupt only costs one more call). Producers use the new Disruptor as 
soon as `start()` returns, and LMAX 3.4.4 `Disruptor.shutdown()` waits only for 
the event processors that run (`hasBacklog()` looks at the consumers whose 
`isRunning()` is true). A start that returned before its event processors ran 
would let the next reconfiguration halt that ring buffer at once, with the 
exchanges producers had published on it: InOnly exchanges accepted without 
error and never processed. An earlier version of this PR ended the start wait 
on interrupt, and a red-team probe lost all 5 exchanges sent between an 
interrupted reconfiguration and the next one. An event processor that runs 
normally ends this wait within microseconds; the 10 s apply only when it cannot 
start, where main also waits 10 s (no interrupt) or forever (interrupt);
   - the wait for the stop ends. `shutdownDisruptor()` calls it after 
`Disruptor.shutdown()`, which returned only once the running event processors 
had taken every exchange published on the ring buffer and were told to halt. An 
event handler that has not stopped yet has handled every exchange it took and 
is only shutting down, as after the 10 s limit on main, so nothing is lost by 
not waiting for it, and the thread that was interrupted, usually to stop 
something, does not wait up to 10 s per event handler.
   
   I did not make the interrupt fail the start: the callers (a route start or 
stop, the removal of an endpoint) do not expect it, and a start that throws 
leaves the producers waiting for the disruptor (CAMEL-27519, a separate PR).
   
   Not changed here: the busy wait of `Disruptor.shutdown()` (LMAX) for the 
backlog, which does not look at the interrupt flag.
   
   ## Tests
   
   `DisruptorInterruptedWaitTest` (package-private, JUnit assertions as in the 
module, Awaitility, no sleeps). It counts the "Interrupted while waiting" lines 
with a log4j appender on the `DisruptorReference` logger, which does not pass 
them to the log file during the test, so a loop cannot fill the disk:
   
   - `interruptedStartWaitsForTheEventHandlersBeforeTheDisruptorIsUsed`: the 
CamelContext gets a `ThreadPoolFactory` whose pools keep their tasks without 
running them while a flag is set (public API, no reflection), so the event 
handler of the next Disruptor does not start. One thread, interrupted, calls 
`reconfigure()`, then sends 5 InOnly exchanges and calls `reconfigure()` again. 
The test waits until the pool holds the event processor and the thread is in a 
timed wait (so it waits, without looping), checks it logged at most twice (the 
shutdown of the old event handler and the start of the new one), then lets the 
pool run the tasks: the thread ends, its interrupt flag was kept after the 
first `reconfigure()`, and the 5 exchanges are processed;
   - `interruptedWaitForEventHandlersThatDoNotStopEnds`: an interrupted thread 
waits for two event handlers that have started and do not stop: it returns 
within 5 s, keeps its flag, and logs once. The test stops the two event 
handlers in a `finally`, so if the wait looped again, it would end before the 
log appender is removed.
   
   On main both tests fail: the start test because the interrupted thread loops 
instead of waiting (it is never in a timed wait within 5 s; the counted lines 
stay out of the log file), the other because the new method is missing. With 
the previous version of this PR (an interrupt ended the start wait) the start 
test fails with 0 of the 5 exchanges processed, the stop test passes. With the 
method changed to wait again after an interrupt on the stop path, as main does, 
the stop test fails and the log file of the test run grows by 4 KB only, as the 
`finally` ends the loop. With the fix: the class 20 times in one JVM, 40/40; 
camel-disruptor 138 tests, 0 failures. Merged with CAMEL-27519 and with a 
separate PR that also changes `DisruptorReference` (no conflict, also none with 
current main): camel-disruptor 145 tests, 0 failures. On that merged code, the 
red-team probes that lost exchanges with the previous version lose none: 500 
pairs of an interrupted reconfiguration followed by a norma
 l one, with a producer sending throughout, 0 missing of 31,911; an interrupted 
reconfiguration in a tight loop for 8 s (487,143 reconfigurations, each keeping 
the flag), 0 missing of 9,434, no duplicate, never more than one exchange at a 
time in the single consumer.
   
   Backport: the same loops are on camel-4.22.x, camel-4.18.x and camel-4.14.x.
   
   _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