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

   # Description
   
   [CAMEL-27335](https://issues.apache.org/jira/browse/CAMEL-27335)
   
   With a correlation manager that extends `TimeoutCorrelationManagerSupport` 
(the documented base class for custom correlation managers), a request that is 
still waiting for its reply when the producer stops is never completed:
   
   - `NettyProducer.doStop` closes the channels. For each one, 
`ClientChannelHandler.channelInactive` asks the manager for the state with 
`getState(ctx, channel, (Throwable) null)`, and this manager returns `null`;
   - the manager is stopped next. `DefaultTimeoutMap.doStop` clears the map 
without evicting the entries, and the worker pool is shut down.
   
   The callback of the pending exchange is never called: a synchronous caller 
blocks, and the exchange stays in flight. This happens on a forced shutdown 
with requests in flight, or when a producer is stopped on its own (for example 
a dynamic producer evicted from the producer cache). With the default 
correlation manager the close of the channel completes the exchange ("No 
response received from remote server").
   
   The reply managers of camel-jms, camel-sjms and camel-rocketmq already drain 
their timeout map on stop (`drainAndEvictAll`, added for this case); the netty 
manager did not.
   
   The model found a second, narrower way to lose a request at the stop: the 
purge task of the map can remove an expired entry just before the stop and 
submit its timeout task after the worker pool was shut down. The pool's 
`CallerRuns` policy drops the tasks of a shut-down pool (and after the stop the 
pool field is `null`), so that exchange was not completed either.
   
   This change (`TimeoutCorrelationManagerSupport`):
   
   - the manager's timeout map is a small subclass of `DefaultTimeoutMap` that 
evicts every entry (`drainAndEvictAll`) before its own stop. Each pending 
request then completes as a timeout (`ExchangeTimedOutException`, or the 
`getTimeoutResponse` body), on the worker pool, before the pool is shut down 
(`shutdown` lets the queued tasks run);
   - an eviction that finds the worker pool shut down or `null` (or rejected) 
also runs its task in the calling thread. The task completes the exchange at 
most once (`NettyCamelState.markDone`, CAMEL-25128), so a task that the pool 
accepted and that also runs here completes it once.
   
   Model: found with a TLA+ model of the netty producer (pool, correlation 
manager with its timeout map and purge task, reply, write failure, request 
timeout, stop). I split the stop and the purge into their steps and added the 
fix. On main a request is pending after the stop. With the drain only, the 
purge race above still loses a request. With the change, no request is pending 
after the stop, no eviction is dropped, and every exchange completes at most 
once (reply, timeout, write failure and the stop racing): 2 exchanges, 3 
channels, 2 server closes, 2 write failures and the stop: 3,902,220 distinct 
states; 3 exchanges, 2 channels: 1,389,522 (TLC and everystate agree).
   
   Tests: new `NettyTimeoutCorrelationManagerStopTest` (a server that reads the 
requests and never replies, a manager with a 60 s timeout):
   
   - one pending request, the producer stops: the exchange must complete within 
10 s, with `ExchangeTimedOutException`;
   - two pending requests on the shared channel (`producerPoolEnabled=false`): 
both must complete;
   - a worker pool that no longer takes tasks (shut down) and a request that 
times out after 300 ms: the request must complete with 
`ExchangeTimedOutException` (the eviction runs in the thread of the purge 
task); on main the rejection is swallowed by the timeout map and the request 
never completes;
   - the default correlation manager: completes with "No response received from 
remote server" (control, passes on main).
   
   On main the three fail; the control passes. With the change the unit tests 
of camel-netty pass: 169 run, 0 failures, 9 skipped (run before the 
rejected-pool test was added; that class alone: 4 of 4). The new class 20 times 
in one JVM: 80 run, 0 failures. The purge race at the stop itself is covered by 
the model, not by a test (it needs the purge task between its removal and its 
submit).
   
   Not changed: a request completed by the stop gets the same 
`ExchangeTimedOutException` as a timeout, as with the jms and sjms reply 
managers.
   
   # 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-27335](https://issues.apache.org/jira/browse/CAMEL-27335).
   
   # 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 commit 
carries 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