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

   [CAMEL-27010](https://issues.apache.org/jira/browse/CAMEL-27010)
   
   ### What was wrong
   
   `MongoAbstractConsumerThread.stop()`, called by `doStop()` of the tailable 
and the change streams consumers, closes the cursor and then waits for the 
consumer thread with `stoppedLatch.await()`, without a timeout. Closing the 
cursor does not end a read in progress on that thread: the 5.x driver's 
`CursorResourceManager.close()` only marks the cursor close-pending while the 
`getMore` of `hasNext()` runs. A read from a connection that stopped answering 
(traffic dropped without closing the connection) does not return, so:
   
   - `consumer.stop()` does not return;
   - stopping the route or the CamelContext waits for the whole graceful 
shutdown timeout, then the stop of the consumer fails with an 
`InterruptedException`, its thread pool is not shut down, and the fail-safe of 
the `ExecutorServiceManager` waits for it 2 x `shutdownAwaitTermination` more. 
With a 3 s shutdown timeout, `CamelContext.stop()` took 23 s.
   
   ### The fix
   
   - While the thread reads from MongoDB, the stop waits for it at most 
`ExecutorServiceManager.getShutdownAwaitTermination()` (10 s by default, the 
time Camel waits for a thread pool to shut down), then logs a WARN, interrupts 
the thread and leaves it behind; it ends once the read returns. An interrupt 
does not end a socket read of a platform thread, but it does end one on a 
virtual thread, and the driver's interruptible waits (such as server selection).
   - An exchange that the route is processing is waited for as before, and not 
interrupted: the graceful shutdown bounds that wait with its own timeout. 
`processExchange()` sets a `processing` flag for this.
   - The latch is created with the thread object instead of at the start of 
`run()`, so a stop before `run()` begins no longer meets a `null` latch.
   
   ### Tests
   
   `MongoDbConsumerStopTest`, with a mocked driver (no Docker), shutdown 
timeout 60 s and `shutdownAwaitTermination` 500 ms:
   
   - `tailableConsumerStopsWhileItsThreadIsBlockedReadingTheCursor` and 
`changeStreamsConsumerStopsWhileItsThreadIsBlockedReadingTheCursor`: the 
cursor's `hasNext()` is ended neither by `close()` nor by an interrupt. 
`stopRoute()` must return within 10 s, with one WARN, and the thread 
interrupted. On main both fail (the stop is still waiting after 10 s); with the 
fix both pass.
   - `stopWaitsForTheExchangeThatTheRouteProcesses`: a route that takes longer 
than the bound; the stop waits for it and the exchange completes (passes before 
and after).
   
   The module's other unit tests pass.
   
   ### For reviewers
   
   - The bound reuses `shutdownAwaitTermination` rather than adding an endpoint 
option: it is what `shutdownGraceful(executor)` waits in other consumers, and 
it is configurable on the CamelContext.
   - The consumer is not suspendable, so the shutdown strategy stops it before 
it waits for the in-flight exchanges: interrupting a thread that routes an 
exchange would cut the graceful shutdown of a slow route short, hence the 
`processing` check.
   - No upgrade guide entry: no option or default changes.
   
   _Claude Code on behalf of Croway_
   


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