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

   # Description
   
   [CAMEL-27338](https://issues.apache.org/jira/browse/CAMEL-27338)
   
   `NettyProducer.processWithConnectedChannel` sets up the request on the 
channel it borrowed from the producer pool before it writes it: the request 
timeout (from the `CamelNettyRequestTimeout` header, or `requestTimeout`) and 
the state of the correlation manager. When that throws, 
`ChannelConnectedListener` completes the exchange with the exception through 
`BodyReleaseCallback`, which does not release the channel: only the 
`NettyProducerCallback` that is created afterwards does. So the channel stays 
borrowed for ever. It happens with:
   
   - a `CamelNettyRequestTimeout` header that is not a number;
   - a request without a correlation id for a 
`TimeoutCorrelationManagerSupport` manager (`putState` throws 
`IllegalArgumentException: CorrelationID is missing`);
   - a custom pipeline without a `handler` (adding the timeout handler before 
it fails).
   
   With `producerPoolMaxTotal` set, the pool runs out after that many failed 
requests: every later request waits for a channel, for ever with the default 
`producerPoolMaxWait=-1`, or fails with `NoSuchElementException`.
   
   The change: the setup (up to and including `putState`) is in a `try`, and 
when it fails the borrowed channel is given back before the exception goes on 
to `ChannelConnectedListener` as before:
   
   - a channel of the producer pool is invalidated (`destroyObject` closes it). 
Returning it instead would leave the request timeout that `activateObject` 
added on the borrow armed on an idle channel (only a reply removes it), and the 
failed setup may have changed its pipeline;
   - the shared channel of `producerPoolEnabled=false` is released as usual 
(`returnObject` is a noop there, and the channel is used by the other requests);
   - with `reuseChannel=true` nothing changes: the channel is left to the 
on-completion that the first request of the exchange added (a channel that the 
exchange reuses was not borrowed by this request).
   
   The code after `putState` (the remote address and `writeBodyAsync`) reports 
a failure through the write future, so the state that `putState` registered is 
completed once as before, and the channel is not released twice.
   
   Found with a TLA+ model of the netty producer (pooled and shared channels, 
both correlation managers, request timeout, server close, write failure, 
producer stop). On main it finds the borrowed channel that is never given back 
(NoBorrowLeak) with both managers. With the fix all the invariants hold 
(callback at most once, no reply on the wrong exchange, never returned twice, 
never idle while in use, no borrow leak, nothing pending after stop), and the 
model is free of deadlocks (every exchange completes): default manager 33,375 
distinct states, timeout manager 85,366, 3 exchanges and 4 channels 4,025,090; 
the shared pool (control) gives the same 32,524 states on main and with the 
fix. Same counts with TLC and everystate.
   
   Tests: new `NettyProducerFailedSetupTest` (the module's `BaseNettyTest` 
server, `producerPoolMaxTotal=1`, `producerPoolMaxWait=2000`):
   
   - `testInvalidRequestTimeoutHeader`: a request with 
`CamelNettyRequestTimeout=not a number` fails, and the next request must get 
the reply;
   - `testMissingCorrelationId`: a request without a correlation id for a 
`TimeoutCorrelationManagerSupport` fails, and the next request must get the 
reply;
   - `testInvalidRequestTimeoutHeaderWithSharedChannel`: the same with 
`producerPoolEnabled=false` (control, passes on main).
   
   On main the first two fail: `The next request should get a channel from the 
pool ==> expected: <null> but was: <java.util.NoSuchElementException: Timeout 
waiting for idle object, maxWaitDuration=PT1.999989S>`; the control passes. 
With the change all three pass. The new class 20 times in one JVM: 60 run, 0 
failures.
   
   Module tests: `camel-netty`: 169 run, 0 failures, 9 skipped.
   
   Not changed, a separate bug for a follow-up: with `reuseChannel=true` that 
on-completion (a `SynchronizationAdapter`) overrides `onComplete` only, and 
`SynchronizationAdapter.onFailure` calls the no-op `onDone`, so an exchange 
that fails (also by a failed setup as above) never gives its channel back to 
the pool.
   
   This touches only `NettyProducer`; the other camel-netty fixes of this 
batch, in `ClientChannelHandler` (an exception on a channel without a request) 
and `TimeoutCorrelationManagerSupport` (its stop) are separate and merge 
cleanly with it.
   
   # 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-27338](https://issues.apache.org/jira/browse/CAMEL-27338).
   
   # 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 `components/camel-netty`, including the formatter and 
import-sort plugins. No option or generated file changes. 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