Skip to content

Make outbox, inbox, and scheduling workers idempotent on start#9912

Open
alisan3 wants to merge 5 commits into
ChilliCream:mainfrom
alisan3:ali/hosted-worker-idempodency
Open

Make outbox, inbox, and scheduling workers idempotent on start#9912
alisan3 wants to merge 5 commits into
ChilliCream:mainfrom
alisan3:ali/hosted-worker-idempodency

Conversation

@alisan3

@alisan3 alisan3 commented Jun 14, 2026

Copy link
Copy Markdown
Contributor

The Mocha message-bus hosted workers were not idempotent on start. StartAsync
checked _task for null and then assigned it as two separate steps, so:

  • A second StartAsync call threw InvalidOperationException instead of being a no-op.
  • Concurrent calls could both pass the guard and each create a background loop (and,
    for the Postgres workers, an NpgsqlDataSource), leaking the orphaned loop/connection
    that StopAsync could never dispose.

Changes

Made StartAsync/StopAsync idempotent and thread-safe in:

  • PostgresMessageBusOutboxWorker
  • ScheduledMessageWorker
  • MessageBusInboxWorker

Each now guards lifecycle state with a lock: a repeat StartAsync is a no-op (no throw),
and StopAsync atomically claims the task before disposing it (outside the lock) so a
single caller owns disposal and resources can't leak.

Tests

Added StartAsync_Should_NotThrow_When_CalledMultipleTimes for each worker, plus a new
SchedulingServiceRegistrationTests. All pass on net8.0/net9.0/net10.0.

@alisan3 alisan3 changed the title [Mocha] Make outbox, inbox, and scheduling workers idempotent on start Make outbox, inbox, and scheduling workers idempotent on start Jun 14, 2026
@alisan3 alisan3 marked this pull request as ready for review June 14, 2026 12:00
Copilot AI review requested due to automatic review settings June 14, 2026 12:00

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.

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 StartAsync can be called multiple times for inbox/outbox/scheduling workers.
  • Updated inbox/outbox/scheduling workers to make StartAsync a 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.

Comment thread src/Mocha/src/Mocha.Inbox/MessageBusInboxWorker.cs
@alisan3 alisan3 requested a review from Copilot June 15, 2026 08:17

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.

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 StartAsync documentation to explicitly state that repeated calls are a no-op (idempotent) so callers/tests aren’t relying on undocumented behavior.
using Microsoft.Extensions.Hosting;

@alisan3 alisan3 requested a review from Copilot June 15, 2026 08:34

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.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 6 comments.

Comment thread src/Mocha/src/Mocha.EntityFrameworkCore.Postgres/Outbox/MessageBusOutboxWorker.cs Outdated
@alisan3 alisan3 requested a review from Copilot June 15, 2026 09:03

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.

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 4 comments.

Comment thread src/Mocha/src/Mocha.Inbox/MessageBusInboxWorker.cs
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