allthingssecurity opened a new pull request, #30356: URL: https://github.com/apache/camel/pull/30356
# Description [CAMEL-27173](https://issues.apache.org/jira/browse/CAMEL-27173) `DefaultNettyCamelStateCorrelationManager` keeps the state (exchange and callback) of the last request of a channel until the channel is closed (`channelInactive`). After the reply, the channel goes back to the producer pool and the exchange routes on. When the server then resets the idle connection (an idle timeout that closes with SO_LINGER 0, a load balancer, a restart of the server), `ClientChannelHandler.exceptionCaught` gets that old state and sets the exception on the exchange, whose call has already completed: the exchange fails with "Connection reset by peer" when its current step returns, although the netty call succeeded (with redelivery, that step runs again). A message that the server sends on the idle channel replaces the message of that exchange the same way (`channelRead0`). `channelInactive` already checks that the state is not done; these two paths do not. This needs the exchange to run on while the event loop of the channel is free: after the call it routes on another thread (another netty call, `threads()`, an async delay, an async producer). With only synchronous steps after the call they run on that event loop before it reads the reset. The SPI javadoc of `NettyCamelStateCorrelationManager.getState` says the state should be removed once it is no longer needed; the default manager keeps it on purpose for the 100-continue path (`CamelNettyClientContinue`), where the real response follows on the same state, so the fix is in the handler and not in the manager. Fix (`ClientChannelHandler`): a done state is handled as no state. `exceptionCaught` closes the channel (it is not reused) and leaves the exchange alone; `channelRead0` ignores the message (DEBUG log). With `reuseChannel=true` the exchange keeps the channel between its calls: a reset in between no longer fails the step the exchange runs at that time; the channel is closed, so the next call of the exchange on it fails. Requests that wait for their reply are handled as before; the correlation managers with a timeout (`TimeoutCorrelationManagerSupport`) remove the state when they hand it out, so they do not see a done state. Model: `NettyIdleState.tla` (reply, reset, unsolicited message, the exchange routing on): "an exchange whose reply was received is never failed or changed afterwards by its idle channel" is violated on main (reply, then reset or unsolicited message: 4 states) and holds with the change. Tests: `NettyProducerIdleChannelResetTest`: a plain socket server replies, then resets the connection (SO_LINGER 0) while the exchange runs its next step; that step waits until the producer closed the channel. On main the exchange fails with the reset (`SocketException: Connection reset`); with the change it completes. camel-netty module tests: 163 run, 0 failures, 9 skipped. The new class 20 times in one JVM: 20 run, 0 failures. `camel-netty-http` gets the same change, as `HttpClientChannelHandler` extends `ClientChannelHandler` (it overrides neither `exceptionCaught` nor `channelRead0`): camel-netty-http module tests 383 run, 0 failures, 1 error, 10 skipped; the error was `NettyHttpBasicAuthConstraintRolesTest.userWithTheRoleIsAccepted` getting a 404 from the test's own server (not a client-side path), and that class passes alone and 10 times in one JVM (40 of 40). # 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-27173](https://issues.apache.org/jira/browse/CAMEL-27173). # 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 the touched modules with the formatter and import-sort plugins; no option, doc 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]
