Skip to content

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

Open
tobias-tengler wants to merge 1 commit into
mainfrom
mocha/shutdown-minimal
Open

tobias-tengler wants to merge 1 commit 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 (30/30)

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

Project coverage: 58.6% (314103/536388 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.

Comment thread src/Mocha/src/Mocha/Consumers/Implementations/BatchConsumer.cs Outdated
Comment thread src/Mocha/src/Mocha/Runtime/MessagingRuntime.cs
Comment thread src/Mocha/src/Mocha/Transport/MessagingTransport.cs Outdated
Comment thread src/Mocha/src/Mocha/Endpoints/ReceiveEndpoint.cs Outdated
Comment thread src/Mocha/test/Mocha.Transport.Postgres.Tests/Behaviors/ShutdownTests.cs Outdated
@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 added this pull request to stack #10505 October 7, 2026 20:00
@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 from 8bc09cf to 7bb10f2 Compare October 8, 2026 07:08
A stopped runtime cannot be started again.
@tobias-tengler
tobias-tengler force-pushed the mocha/shutdown-minimal branch from aae6435 to 13d6f71 Compare October 8, 2026 13:30

This branch has not been deployed

No deployments
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.

2 participants