allthingssecurity opened a new pull request, #30131: URL: https://github.com/apache/camel/pull/30131
# Description [CAMEL-26970](https://issues.apache.org/jira/browse/CAMEL-26970) Follow-up of CAMEL-23260. Found by the Service Bus DST world of CAMEL-26956 (opened in the same batch; this PR pairs with it): its default set accepts this case, and `-Ddst.servicebus.strictClose=true` fails 96 of 1000 seeds on main. Confirmed outside the DST harness against the SDK classes and with a plain unit test. CAMEL-23260 made `ServiceBusConsumer` `ShutdownAware` so that the exchanges in flight at a graceful stop can still settle their message. Its `deferShutdown` calls `client.stop()`, with the comment "stop accepting new messages but keep the connection open so that in-flight exchanges can still complete/abandon messages". With azure-messaging-servicebus 7.17 (javap of 7.17.16) that is not what `stop()` does: - `ServiceBusProcessorClient.stop()` calls `ServiceBusProcessor.stop()` when the processor runs on the V2 stack; - the V2 stack is the default for a non-session processor (`V2StackSupport.isNonSessionAsyncReceiveEnabled` is true unless `com.azure.messaging.servicebus.nonSession.asyncReceive.v2` opts out); - `ServiceBusProcessor.stop()` is `close()`: it disposes the `RollingMessagePump`, a `Mono.using` whose cleanup closes its `ServiceBusReceiverAsyncClient`; - a settlement through a disposed receiver fails: `updateDisposition` returns `IllegalStateException` "Cannot perform operation '...' on a disposed receiver". So an exchange that completed during the graceful stop could not complete its message (the exception was logged as "Exception occurred during onCompletion"), and Service Bus delivered the message again after its lock expired: the stop processed it twice. A failed exchange could not abandon or dead-letter its message either. It is the log of CAMEL-23260 itself ("Cannot perform operation 'abandoned' on a disposed receiver"). The Service Bus documentation: "Settling a message after you close the receiver throws an exception, and the message isn't settled." Change, in `ServiceBusConsumer`: - in peek-lock mode, with the processor client the consumer creates, `deferShutdown` no longer stops the client; it marks the consumer as stopping (a volatile flag); - a message the client delivers while the consumer is stopping is held in `processMessage`: not routed, not settled, and the call waits until the consumer stops. The client delivers the next message only when `processMessage` returns, so at most one message per concurrent call is held (`maxConcurrentCalls`, per session with sessions); - `doStop` (and `doShutdown`) close the client as before, then release the held calls. Service Bus delivers a held message again once its lock expires; its delivery count goes up by one, as it did on main for the exchanges in flight, which were also routed twice; - `processMessage` counts the exchange as pending before it checks the flag, so a graceful stop either waits for that exchange or the message is held; - in receive-and-delete mode a received message is deleted already and nothing needs settling, and the messages of a `processorClient` given by the user go to the callbacks it was built with, not through `processMessage`: in both cases `deferShutdown` still stops the client. Not changed: when a stop with `abortAfterTimeout` gives up and leaves the route running, the consumer stays in the stopping state until the route is stopped and started (on main its client stayed stopped); the up to one held message per call waits for that stop (the SDK renews its lock up to `maxAutoLockRenewDuration`). `onComplete` still calls `messageContext.complete()` without the `settle(...)` wrapper of abandon and dead-letter (a complete after a forced stop is logged by the unit of work); a follow-up JIRA if wanted. camel-azure-eventhubs has the same `deferShutdown` shape, but its checkpoints do not go through the receiver. Tests, in `ServiceBusConsumerTest` (the processor client mock behaves as the 7.17 V2 client: `stop()` and `close()` dispose the receiver, and a settlement after that throws the `IllegalStateException` above): - `gracefulStopKeepsTheClientOpenUntilTheExchangesInFlightAreSettled`: a message in flight, `deferShutdown`, the exchange completes: its message is completed, and the client is closed by the stop only; - `messageDeliveredDuringGracefulStopIsHeldUntilTheConsumerStops`: after `deferShutdown` the client delivers another message: the call waits, nothing is routed or settled, and the stop closes the client and releases the call; - `consumerStartedAgainAfterGracefulStopRoutesMessages`: after the stop, a start routes messages again; - `deferShutdownStopsClientInReceiveAndDeleteMode` (was `deferShutdownStopsClientAndReturnsTrue`, which asserted `stop()` for every mode) and `deferShutdownStopsUserProvidedClient`. Without the change the first two fail (`complete()` throws; the delivered message is routed and the call returns); with it the camel-azure-servicebus unit tests pass (79 run, 0 failures). Pairs with the world PR of CAMEL-26956, which opens in the same batch. The world models what the processor client does while a `processMessage` call runs (it renews the message lock, up to `maxAutoLockRenewDuration`), counts only routed deliveries as held by the consumer, and stops the route in the steps of `DefaultShutdownStrategy`, so it can run a consumer that holds a message in `processMessage` until it stops. With this change merged into it locally, the default set passes 1000 of 1000 seeds and `-Ddst.servicebus.strictClose=true` 5000 of 5000 (15 to 24 ms a seed, depending on the load of the machine), and the module's tests with the world pass (1079 run: 79, plus 1000 seeds; 0 failures). Best merged together with or after the world PR. The ITs need a Service Bus emulator and were not run locally (CI runs them). No documentation or upgrade guide change: a graceful stop now settles the messages of the exchanges in flight, as CAMEL-23260 meant it to. Backport: camel-4.18.x has the same `deferShutdown`. # 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-26970](https://issues.apache.org/jira/browse/CAMEL-26970). # 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-azure-servicebus and the core modules with the formatter and import-sort plugins and ran the camel-azure-servicebus unit tests. 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]
