Skip to content

[Mocha] Fix transports not stopping when the host shuts down - #10479

Merged
tobias-tengler merged 2 commits into
mainfrom
mocha/shutdown-minimal
Oct 13, 2026
Merged

tobias-tengler merged 2 commits into
mainfrom
mocha/shutdown-minimal

Conversation

@tobias-tengler

@tobias-tengler tobias-tengler commented Oct 4, 2026 •

Copy link
Copy Markdown
Member

When the host shut down, MessagingRuntimeHostedService.StopAsync only 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, so StartAsync throws after StopAsync. The PostgreSQL transport stops its background tasks in a new OnAfterStopAsync hook, so its consumer stays registered until the endpoints have stopped.

@tobias-tengler
tobias-tengler marked this pull request as ready for review October 4, 2026 18:18
Copilot AI balanced review requested due to automatic review settings October 4, 2026 18:18
@github-actions

github-actions Bot commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

Patch coverage

100.0% of changed lines covered (35/35)

File Covered Changed Patch %
src/Mocha/src/Mocha/Runtime/MessagingRuntime.cs 31 31 100.0% 🟢
src/Mocha/src/Mocha/Runtime/MessagingRuntimeHostedService.cs 1 1 100.0% 🟢
src/Mocha/src/Mocha/ThrowHelper.cs 1 1 100.0% 🟢
src/Mocha/src/Mocha/Transport/MessagingTransport.cs 2 2 100.0% 🟢

Project coverage: 58.6% (314277/536505 lines)

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 High severity · 1 Medium severity · 1 Low severity

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
tobias-tengler marked this pull request as draft October 6, 2026 14:33
@tobias-tengler
tobias-tengler force-pushed the mocha/shutdown-minimal branch 2 times, most recently from cafcc7e to d341a3f Compare October 7, 2026 16:26
@tobias-tengler
tobias-tengler removed the request for review from PascalSenn October 7, 2026 16:50
@tobias-tengler
tobias-tengler force-pushed the mocha/shutdown-minimal branch from d341a3f to 750e258 Compare October 7, 2026 19:59
@tobias-tengler tobias-tengler changed the title [Mocha] Fix transport shutdown and handler cancellation [Mocha] Fix hosted shutdown not stopping transports Oct 7, 2026
@tobias-tengler tobias-tengler changed the title [Mocha] Fix hosted shutdown not stopping transports [Mocha] Fix transports not stopping when the host shuts down Oct 7, 2026
@tobias-tengler
tobias-tengler force-pushed the mocha/shutdown-minimal branch 2 times, most recently from 6167eff to 8bc09cf Compare October 8, 2026 07:05
@tobias-tengler
tobias-tengler marked this pull request as ready for review October 8, 2026 07:07
@tobias-tengler
tobias-tengler force-pushed the mocha/shutdown-minimal branch 2 times, most recently from aae6435 to 13d6f71 Compare October 8, 2026 13:30
A stopped runtime cannot be started again.
…ox test

The runtime hosted service now stops the messaging runtime, and a stopped
runtime cannot be started again. The test restarted every hosted service,
so its restart phase threw. It now keeps the runtime running and restarts
only the outbox worker, which is what the test exercises.
@tobias-tengler
tobias-tengler removed this pull request from stack #10505 October 13, 2026 06:41
@tobias-tengler
tobias-tengler merged commit 59f4495 into main Oct 13, 2026
158 checks passed
@tobias-tengler
tobias-tengler deleted the mocha/shutdown-minimal branch October 13, 2026 06:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants