Repository navigation
[Mocha] Cancel message handlers and release interrupted messages on shutdown - #10504
Draft
tobias-tengler wants to merge 2 commits into
Draft
tobias-tengler wants to merge 2 commits into
tobias-tengler wants to merge 2 commits into
Conversation
tobias-tengler
added this pull request to stack #10505
October 7, 2026 20:00
tobias-tengler
marked this pull request as draft
October 7, 2026 20:00
tobias-tengler
force-pushed
the
mocha/shutdown-handler-cancellation
branch
2 times, most recently
from
October 7, 2026 20:06
171c463 to
2fa5296
Compare
Contributor
Patch coverage88.7% of changed lines covered (197/222) Uncovered changed lines (JSON){
"sha": "6b7eef39cad5cfca0d54862dd3eaffea551b0125",
"files": [
{ "path": "src/Mocha/src/Mocha.Transport.RabbitMQ/Middlewares/Receive/RabbitMQAcknowledgementMiddleware.cs", "ranges": [[36, 36]] },
{ "path": "src/Mocha/src/Mocha/Transport/MessagingTransport.cs", "ranges": [[251, 254], [272, 274]] },
{ "path": "src/Mocha/src/Mocha.Transport.Postgres/PostgresReceiveEndpoint.cs", "ranges": [[222, 222], [246, 249]] },
{ "path": "src/Mocha/src/Mocha/Consumers/Implementations/BatchConsumer.cs", "ranges": [[91, 93], [266, 267], [270, 274], [276, 276]] },
{ "path": "src/Mocha/src/Mocha/Consumers/Batching/BatchCollector.cs", "ranges": [[85, 85]] }
]
}Project coverage: 58.6% (314284/536564 lines) |
tobias-tengler
force-pushed
the
mocha/shutdown-handler-cancellation
branch
from
October 8, 2026 07:05
2fa5296 to
b84e810
Compare
tobias-tengler
force-pushed
the
mocha/shutdown-handler-cancellation
branch
3 times, most recently
from
October 8, 2026 08:39
f616eee to
57f2418
Compare
tobias-tengler
force-pushed
the
mocha/shutdown-handler-cancellation
branch
from
October 8, 2026 13:29
f16380f to
7a16145
Compare
… failures Delete or release handled Postgres messages with a token that stop does not cancel, and release the rest of a batch when polling is cancelled, so in-flight messages are not redelivered after the lock timeout. Keep stopping the remaining transports and endpoints when one fails, then rethrow the first failure.
tobias-tengler
force-pushed
the
mocha/shutdown-handler-cancellation
branch
from
October 8, 2026 13:30
7a16145 to
85ffdaf
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When a receive endpoint stopped, in-flight handlers were never cancelled because
ReceiveContextdiscarded the transport's cancellation token, so they kept running after shutdown began. Stopping also left PostgreSQL messages behind. A handler that finished after the stop began could not delete its message, because the delete used the already cancelled token, and messages that were leased but not finished stayed locked until the 5 minute lease timeout. Both were then redelivered and processed a second time. Finally, a transport or endpoint that failed to stop kept the remaining ones from stopping.This PR passes the endpoint's cancellation token to handlers, including batch handlers, and keeps shutdown cancellation from triggering retries, fault handling or error logs. RabbitMQ and Azure Service Bus settle messages with
CancellationToken.None, so an interrupted message is still nacked or abandoned. Batches are collected and processed per receive endpoint, so stopping one endpoint only cancels its own batches, andMaxConcurrentBatchesnow applies per endpoint.The PostgreSQL endpoint deletes or releases a handled message with
CancellationToken.None. When polling is cancelled, it releases the messages of the current batch that it still holds, so they are redelivered right away instead of after the lease timeout.MessagingRuntime.StopAsyncandMessagingTransport.StopAsynckeep stopping the remaining transports and endpoints when one fails, then rethrow the first failure. A transport that failed to stop stays started, so disposing the runtime tries it again before the consumers are disposed.