Repository navigation
[Mocha] Fix transports not stopping when the host shuts down - #10479
Open
tobias-tengler wants to merge 1 commit into
Open
tobias-tengler wants to merge 1 commit into
tobias-tengler wants to merge 1 commit into
Conversation
Contributor
Patch coverage100.0% of changed lines covered (30/30)
Project coverage: 58.6% (314103/536388 lines) |
Contributor
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Shutdown failure handling and shared-batch cancellation can leave endpoints active or misclassify cancellation as message failure.
Review effort: Balanced
Findings: 3
Open (5)
What changed in this PR
Fixes Mocha shutdown behavior by stopping transports, propagating cancellation to handlers, and cleaning up resources.
Changes:
- Adds runtime and transport shutdown lifecycle handling.
- Propagates cancellation through receive, retry, and batch pipelines.
- Adds shutdown, retry, and batching tests.
| File | Description |
|---|---|
Mocha.Transport.RabbitMQ.Tests/Behaviors/ShutdownTests.cs |
Tests cancellation and message requeue. |
Mocha.Transport.Postgres.Tests/Behaviors/ShutdownTests.cs |
Tests lease preservation during shutdown. |
Mocha.Tests/Runtime/MessagingRuntimeShutdownTests.cs |
Tests endpoint and batch cancellation. |
Mocha.Tests/Middlewares/Consume/Retry/RetryExecutorTests.cs |
Tests cancellation-aware retries. |
Mocha.Tests/Consumers/Batching/BatchCollectorTests.cs |
Tests buffered-entry removal. |
Mocha/Transport/MessagingTransport.cs |
Stops endpoints before transport cleanup. |
Mocha/Runtime/MessagingRuntimeHostedService.cs |
Stops runtime with the host. |
Mocha/Runtime/MessagingRuntime.cs |
Adds runtime shutdown and transport disposal. |
Mocha/Middlewares/ReceiveContext.cs |
Assigns receive cancellation tokens. |
Mocha/Middlewares/Receive/Redelivery/ReceiveRedeliveryMiddleware.cs |
Skips redelivery during cancellation. |
Mocha/Middlewares/Receive/ReceiveFaultMiddleware.cs |
Skips fault handling during cancellation. |
Mocha/Middlewares/Receive/ReceiveDeadLetterMiddleware.cs |
Skips dead-letter handling during cancellation. |
Mocha/Middlewares/Consume/Retry/RetryExecutor.cs |
Stops retries after cancellation. |
Mocha/Endpoints/ReceiveEndpoint.cs |
Propagates cancelled processing. |
Mocha/Consumers/Implementations/BatchConsumer.cs |
Links batch and endpoint cancellation. |
Mocha/Consumers/Batching/BatchCollector.cs |
Removes cancelled buffered entries. |
Mocha.Transport.RabbitMQ/Connection/RabbitMQConsumerManager.cs |
Cancels handlers before disconnecting. |
Mocha.Transport.Postgres/PostgresMessagingTransport.cs |
Defers cleanup until endpoints stop. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
tobias-tengler
marked this pull request as draft
October 6, 2026 14:33
tobias-tengler
force-pushed
the
mocha/shutdown-minimal
branch
2 times, most recently
from
October 7, 2026 16:26
cafcc7e to
d341a3f
Compare
tobias-tengler
force-pushed
the
mocha/shutdown-minimal
branch
from
October 7, 2026 19:59
d341a3f to
750e258
Compare
tobias-tengler
added this pull request to stack #10505
October 7, 2026 20:00
tobias-tengler
force-pushed
the
mocha/shutdown-minimal
branch
2 times, most recently
from
October 8, 2026 07:05
6167eff to
8bc09cf
Compare
tobias-tengler
marked this pull request as ready for review
October 8, 2026 07:07
tobias-tengler
force-pushed
the
mocha/shutdown-minimal
branch
from
October 8, 2026 07:08
8bc09cf to
7bb10f2
Compare
A stopped runtime cannot be started again.
tobias-tengler
force-pushed
the
mocha/shutdown-minimal
branch
from
October 8, 2026 13:30
aae6435 to
13d6f71
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 the host shut down,
MessagingRuntimeHostedService.StopAsynconly disposed the consumers. The transports kept receiving and heartbeating, even after the service provider was disposed. Messages picked up after shutdown failed on the disposed consumers.This PR adds
MessagingRuntime.StopAsync, which stops the started transports, and the hosted service calls it instead of disposing the runtime. Disposing the runtime now stops and disposes its transports. A stopped runtime cannot be started again, soStartAsyncthrows afterStopAsync. The PostgreSQL transport stops its background tasks in a newOnAfterStopAsynchook, so its consumer stays registered until the endpoints have stopped.