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

   # Description
   
   [CAMEL-27355](https://issues.apache.org/jira/browse/CAMEL-27355)
   
   `RocketMQProducer` (InOut with `replyToTopic`) has two gaps around a stop 
that [CAMEL-27321](https://issues.apache.org/jira/browse/CAMEL-27321) (#30575) 
and [CAMEL-27339](https://issues.apache.org/jira/browse/CAMEL-27339) close in 
camel-jms and camel-sjms. Both PRs name camel-rocketmq as a follow-up.
   
   **A request registered during or after the stop is never completed.** The 
producer registers the handler of an InOut request in the `onSuccess` callback 
of the asynchronous send, so the registration can come at any time relative to 
the stop. `ReplyTimeoutMap.doStop` drains the map (each handler is completed 
with a `RejectedExecutionException`, as 
`RocketMQReplyManagerSupport.processReply` does for a stopping reply manager) 
and then `DefaultTimeoutMap.doStop` cancels the purge task and clears the map. 
Nothing in the map checked whether it was stopping:
   
   - a handler added between the drain and the clear was removed by the clear 
without an event;
   - a handler added after the stop stayed in the stopped map, where nothing 
evicts it (the stop of the producer runs `unInitReplyManager` before 
`mqProducer.shutdown()`, so a send whose result arrives in between registers 
after the reply manager stopped).
   
   The exchange never completed, and a synchronous caller waited for ever.
   
   **A reply manager created after the producer stopped.** `process` checks 
`isRunAllowed()`, converts the body and then calls `initReplyManager()`. A 
request that passed the check just before the stop found `started=false` after 
it, took the producer's lock (the lock `BaseService.stop` holds during 
`doStop`, so it waited for the stop to end) and created and started a new reply 
manager: a `DefaultMQPushConsumer` on the reply topic and a timeout checker 
thread. The send then failed with a `NullPointerException` (the stop set 
`mqProducer` to null). Nothing stopped that reply manager until the producer 
was started and stopped again; a producer that is not started again (a removed 
route, a stopped `ProducerTemplate`) leaked it for good.
   
   The change, the same as in camel-jms and camel-sjms:
   
   - `ReplyTimeoutMap` uses its own lock (passed to `DefaultTimeoutMap`, which 
already has that constructor) and a `closed` flag that `doStop` sets under that 
lock before the drain. A handler that is added once the map is closed is 
completed at once with its timeout task, as the drain would have done: the 
request has already been sent at that point (the registration is in 
`onSuccess`), so it cannot be refused as camel-jms refuses a new request. This 
is what camel-jms does for the handler moved to the JMSMessageID after the 
drain. `doStart` resets the flag.
   - `initReplyManager` checks `isRunAllowed()` again once it holds the 
producer's lock, and fails the request with `RejectedExecutionException` 
(completed by the `catch` of `process`, as for a producer that is not running) 
instead of creating a reply manager. The fast path (`started=true`) is 
unchanged.
   
   The third part of CAMEL-27321, the timeout tasks that a forced shutdown of 
the onTimeout executor drops, does not apply here: `ReplyTimeoutMap` completes 
the timed out handlers on the purge thread itself, and the drained ones on the 
stopping thread, without a separate executor. camel-rocketmq shares only 
`DefaultTimeoutMap` with that change, which there only adds a WARN log for a 
failing listener (`ReplyTimeoutMap` catches the exceptions of its listener 
itself), so this PR does not depend on #30575 or on the CAMEL-27339 PR, and 
merges cleanly with both.
   
   Found with a TLA+ model of the producer, its reply manager, the map and the 
stop (requests with the running check, `initReplyManager`, the asynchronous 
send and its failure, the registration, the reply, the purge, and the steps of 
the stop under the producer's lock). On main it finds a handler added after the 
drain and cleared without being completed, and a reply manager that runs after 
the producer stopped. With only one of the two changes the other violation 
remains. With both, no request is lost, every request is completed exactly 
once, and no reply manager runs after the stop (TLC and everystate agree: 1,037 
distinct states with 3 requests).
   
   Tests, without a broker:
   
   - new `RocketMQReplyManagerStopTest` (the reply manager without the consumer 
of the reply topic, as in `RocketMQReplyManagerSupportTest`): a request 
registered after the stop, and one registered from a listener of the map while 
the stop drains it, must be completed once with `RejectedExecutionException`; a 
request that waits when the stop starts is completed by the drain (control, 
passes on main);
   - new `RocketMQProducerStopReplyManagerTest` (a real producer, which starts 
without a name server; `createReplyManager` is overridden to count the reply 
managers, as the JMS tests do, because the push consumer of the real reply 
manager does not start without a name server: `RemotingConnectException: 
connect to null failed`): an InOut request is held in the conversion of its 
body (after the running check), the producer is stopped, and the request 
continues: no reply manager may be created, and the exchange must fail with 
`RejectedExecutionException`; a running producer creates its reply manager 
once, and its stop stops it (control).
   
   For that test `createReplyManager` is now `protected` instead of `private`, 
as in `JmsProducer`.
   
   On main (with only the `protected` test hook added to the producer) 3 of the 
5 fail: the two registrations of `RocketMQReplyManagerStopTest` are never 
completed (`expected: <1> but was: <0>`), and the late request creates a reply 
manager (`expected: <0> but was: <1>`); the two controls pass. With the change 
all pass. The module tests pass (no ITs): 16 run, 0 failures, 0 skipped. Both 
new classes 20 times in one JVM: 100 run, 0 failures.
   
   No upgrade guide entry: a request that reaches a stopping reply manager now 
fails at once with a `RejectedExecutionException` instead of hanging.
   
   Not changed:
   
   - The handler is registered after the send, so a reply that arrives before 
`onSuccess` runs is dropped ("Reply received for unknown messageKey") and the 
request times out. camel-jms registers before the send. Fixing it needs the 
registration before the send and a decision on who completes the exchange when 
the send then fails, so I would do it in a follow-up.
   
   # 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-27355](https://issues.apache.org/jira/browse/CAMEL-27355).
   
   # 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-rocketmq`, 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