Make outbox, inbox, and scheduling workers idempotent on start#9912
Open
alisan3 wants to merge 5 commits into
Open
Make outbox, inbox, and scheduling workers idempotent on start#9912alisan3 wants to merge 5 commits into
alisan3 wants to merge 5 commits into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds idempotent, thread-safe IHostedService start/stop behavior for Mocha background workers and expands test coverage to validate service registration and repeated StartAsync calls.
Changes:
- Added new DI registration tests for Postgres scheduling worker.
- Added tests ensuring
StartAsynccan be called multiple times for inbox/outbox/scheduling workers. - Updated inbox/outbox/scheduling workers to make
StartAsynca no-op when already started and to make stop/start transitions thread-safe via locking.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/SchedulingServiceRegistrationTests.cs | New tests for scheduling worker DI registration and repeated StartAsync. |
| src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/OutboxServiceRegistrationTests.cs | Adds repeated StartAsync test for Postgres outbox worker. |
| src/Mocha/test/Mocha.EntityFrameworkCore.Postgres.Tests/InboxServiceRegistrationTests.cs | Adds repeated StartAsync test for inbox worker. |
| src/Mocha/src/Mocha.Inbox/MessageBusInboxWorker.cs | Makes StartAsync idempotent and synchronizes start/stop via a lock. |
| src/Mocha/src/Mocha.EntityFrameworkCore.Postgres/Scheduling/ScheduledMessageWorker.cs | Makes StartAsync idempotent and synchronizes start/stop via a lock. |
| src/Mocha/src/Mocha.EntityFrameworkCore.Postgres/Outbox/MessageBusOutboxWorker.cs | Makes StartAsync idempotent and synchronizes start/stop via a lock. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Contributor
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
Comments suppressed due to low confidence (1)
src/Mocha/src/Mocha.Inbox/MessageBusInboxWorker.cs:1
- The XML docs no longer mention the previous behavior (throwing on a second start), but they also don’t describe the new behavior. Please update the
StartAsyncdocumentation to explicitly state that repeated calls are a no-op (idempotent) so callers/tests aren’t relying on undocumented behavior.
using Microsoft.Extensions.Hosting;
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.
The Mocha message-bus hosted workers were not idempotent on start.
StartAsyncchecked
_taskfor null and then assigned it as two separate steps, so:StartAsynccall threwInvalidOperationExceptioninstead of being a no-op.for the Postgres workers, an
NpgsqlDataSource), leaking the orphaned loop/connectionthat
StopAsynccould never dispose.Changes
Made
StartAsync/StopAsyncidempotent and thread-safe in:PostgresMessageBusOutboxWorkerScheduledMessageWorkerMessageBusInboxWorkerEach now guards lifecycle state with a lock: a repeat
StartAsyncis a no-op (no throw),and
StopAsyncatomically claims the task before disposing it (outside the lock) so asingle caller owns disposal and resources can't leak.
Tests
Added
StartAsync_Should_NotThrow_When_CalledMultipleTimesfor each worker, plus a newSchedulingServiceRegistrationTests. All pass on net8.0/net9.0/net10.0.