allthingssecurity opened a new pull request, #30913: URL: https://github.com/apache/camel/pull/30913
# Description [CAMEL-27518](https://issues.apache.org/jira/browse/CAMEL-27518) `TimeoutCorrelationManagerSupport.putState` put the state of a request in its timeout map under the correlation id of the request. When another request with the same correlation id still waited for its reply, its state was replaced and was in no map any more: neither its reply nor the timeout of the manager completed it, so its exchange waited forever (a synchronous caller blocked, the exchange stayed in flight). If the server answered that first request, its reply was set on the second exchange. The documentation of the `correlationManager` option and of the component page only asks for a correlation id in both the request and the reply; it says nothing about two requests that wait at the same time with the same id. Such ids are a mistake of the application, but the manager is documented to provide the timeout, and a request that is never completed is the worst outcome. The contract is now written down: a correlation id must not be used by two requests that wait for their reply at the same time. Change (`TimeoutCorrelationManagerSupport`, `NettyProducer`): - the timeout map of the manager refuses a correlation id that is held by a request that still waits for its reply: the new request fails with an `IllegalStateException` ("Correlation id: abc is used by another request that waits for its reply") and is not sent; the waiting request is left as it is and still gets its reply or its timeout. The check and the put are atomic: the map is created with its own lock, which the check and the put hold together (the purge task and the removal by a reply take the same lock). - a state that is already completed is replaced, as before. Such a state is left in the map when the write of its request failed, until it expires; its correlation id can be used again right away. A correlation id is also free again once its request got its reply or was completed with its timeout (both remove it from the map), or the producer stopped. The timeout is applied by the purge task of the map, which runs every `timeoutChecker` millis (1 second by default), so an id stays in use for up to that long after the timeout itself; the upgrade guide says so. - with `sync=false` the producer no longer puts its requests in a `TimeoutCorrelationManagerSupport`. No reply is waited for (the channel handler ignores what the server sends), so the state only stayed in the map until its timeout, where the eviction found it completed. With the new check, two such requests with the same id would be refused while the write of the first one was not complete. Other correlation managers still get the state with `sync=false`, as before. - the component page and the class javadoc state the rule; the 4.24 upgrade guide has an entry. Behaviour for users: a request whose correlation id is held by a waiting request now fails at once instead of hanging the other request. An application that reuses ids must give each request that waits for a reply an id of its own, or wait for the reply before it sends the next request with the same id. With `sync=false`, a custom `TimeoutCorrelationManagerSupport` that overrides `putState` is no longer called, and `getRequestCorrelationId` is not called either: a request without a correlation id (or whose id cannot be read) is now sent, where main failed it with an `IllegalArgumentException` ("CorrelationID is missing"). The upgrade guide entry says so too. Not changed: - a reply that arrives after its request timed out is still applied to a later request with the same id (for example a redelivery of the same message). That is correlation by id, and the request it lands on carries that id. - custom implementations of `NettyCamelStateCorrelationManager` that are not a `TimeoutCorrelationManagerSupport` decide themselves what a duplicate id does. - a manager bean shared by several producers, stopped by one of them: separate issue (CAMEL-27518, the first PR of this batch). Tests (new `NettyTimeoutCorrelationManagerDuplicateIdTest`, package-private, a plain socket server that replies only when the test says so): - `testSecondRequestWithTheIdOfAWaitingRequestFails`: `producerPoolEnabled=false`, `abc-first` waits; `abc-second` fails with `IllegalStateException`; the server replies and `abc-first` gets the reply. On main the second request replaced the first, and the first was never completed. - `testRequestsWithoutReplyAreNotPut`: `sync=false`, two requests with the same id are sent, and the manager's `putState` is not called. On main it was called twice. - `testRequestWithoutReplyIsSentWithoutCorrelationId`: `sync=false`, a request without a correlation id is sent and the server gets it. On main it failed with `IllegalArgumentException`. - `testPutRefusesTheIdOfAStateThatIsNotDone`: `putState` directly on a started manager; the second state with the same id is refused, and the reply still finds the first. - `testPutReplacesTheIdOfAStateThatIsDone`: a completed state (as after a failed write) is replaced by the next one with the same id. This one also passes on main: it is a control that the replace of a done state still works. Negative control: against the `NettyProducer` and `TimeoutCorrelationManagerSupport` of main, the first 4 tests fail and the control passes. With the change: the camel-netty unit tests pass (185 run, 0 failures, 9 skipped), and the new class passes 20 of 20 runs (100 tests). The waits are latches and a bounded queue poll, no sleeps. Merged locally with the other PR of this batch (both change `NettyProducer` and `TimeoutCorrelationManagerSupport`, and merge without a conflict), both new test classes and the existing correlation manager tests (stop, write failure, custom manager) pass 3 of 3 runs (69 tests). Found with a TLA+ model of the producer, the timeout map and the worker pool ("every request that was put is eventually completed" fails when two requests share a correlation id; the refusal makes it hold), and reproduced against the real producer. # 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-27518](https://issues.apache.org/jira/browse/CAMEL-27518). # 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 camel-netty and the core modules offline with the formatter and import-sort plugins and ran the tests listed above. I did not run the full root build. The component page changed, and its copy in the catalog was updated by hand to the same text; no option metadata changed.) # 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]
