allthingssecurity opened a new pull request, #30696: URL: https://github.com/apache/camel/pull/30696
# Description [CAMEL-27354](https://issues.apache.org/jira/browse/CAMEL-27354) With `reuseChannel=true`, `NettyProducer` does not give the channel back to the producer pool when the request is done (it uses the original callback instead of `NettyProducerCallback`). It adds an on-completion to the exchange instead, which closes the channel if asked (`disconnect`, `CamelNettyCloseChannelWhenComplete`) and calls `releaseChannel` when the exchange is done. That on-completion is a `SynchronizationAdapter` that only overrode `onComplete`, so for a failed exchange (`onFailure`) nothing happened and the channel stayed borrowed for good: - a request that timed out: `ClientChannelHandler` closes the channel, but it is never invalidated, so the pool still counts it as active; - an exchange that failed later in the route, after the reply: the channel stays open and borrowed. With `producerPoolMaxTotal` set, the pool ran out after that many failed exchanges: the next requests waited `producerPoolMaxWait` (for ever by default) and failed with `NoSuchElementException: Timeout waiting for idle object`. With the default unbounded pool, the open channels of failed exchanges were never reused nor closed. The change: the on-completion overrides `onDone`, which `SynchronizationAdapter` calls for a completed and for a failed exchange. `releaseChannel` gives an open channel back to the pool and invalidates a closed one, so a timed out request frees its slot and the next borrow opens a new channel. This is what happens without `reuseChannel`, where `NettyProducerCallback` releases the channel whatever the outcome. This is a follow-up of CAMEL-27338 (a request that fails while it is set up gives its channel back), which leaves the `reuseChannel` case to this on-completion. With `reuseChannel` the on-completion is added before the request is set up, so a request that fails while it is set up (an invalid `CamelNettyRequestTimeout` header, for example) now gets its channel back through it as well. The two changes touch different parts of `processWithConnectedChannel` and merge cleanly in either order. Found with a TLA+ model of the producer pool with `reuseChannel` (exchanges that borrow, complete, fail after the reply, or time out and close the channel). On main, one failed exchange leaves a channel borrowed for good, and with `producerPoolMaxTotal=1` the next exchange never gets a channel. With the change no channel is left borrowed once the exchanges are done, the pool stays within its bound, and every exchange ends (TLC and everystate agree: 142 distinct states with 3 exchanges and a pool of 1, 222 with a pool of 2). Tests: new `NettyReuseChannelFailedExchangeTest`, `reuseChannel=true`, `producerPoolMaxTotal=1` and `producerPoolMaxWait=2000`: - an exchange fails in the route after the reply: the next exchange must get the channel and its reply; - a request times out (`requestTimeout=500`, the server answers later), which closes its channel: the next exchange must get a new channel and its reply; - a request fails before it is sent (an invalid `CamelNettyRequestTimeout` header): the next exchange must get the channel and its reply. On main all 3 fail (`NoSuchElementException: Timeout waiting for idle object` after 2 s). With the change they pass. The module tests: 169 run, 0 failures, 1 error, 9 skipped. The error is `NettyUseSharedWorkerThreadPoolManyRoutesTest` (`BindException: Address already in use` for one of the 60 ports it reserves before the routes bind them; it does not use a producer). It passed 10 times out of 10 with the change and 10 out of 10 with main's `NettyProducer`. The new class 20 times in one JVM: 60 run, 0 failures. Together with the test of CAMEL-27338 (`NettyProducerFailedSetupTest`) and the reuseChannel tests, on the merge of both changes, 3 times: 24 run, 0 failures. No upgrade guide entry: the channel of a failed exchange is now handled as the one of a completed exchange. # 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-27354](https://issues.apache.org/jira/browse/CAMEL-27354). # 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]
