Skip to content

Serialize ExplicitProjectionCoordinator lifecycle calls so overlapping stops cannot race (#5842) - #5843

Merged
jeremydmiller merged 1 commit into
masterfrom
fix/5842-explicit-coordinator-overlap
Nov 9, 2026
Merged

jeremydmiller merged 1 commit into
masterfrom
fix/5842-explicit-coordinator-overlap

Conversation

@jeremydmiller

Copy link
Copy Markdown
Member

Closes #5842. Companion to JasperFx/jasperfx#1148 (fix in JasperFx/jasperfx#1149).

Problem

JasperFx's RunCommand calls host.StopAsync again when ApplicationStopping fires. So when WebApplicationFactory or Alba stops the host, two IHost.StopAsync calls are in flight and both reach the projection coordinator. ProjectionCoordinator gets serialized lifecycle calls from ProjectionCoordinatorBase once the JasperFx fix ships, but ExplicitProjectionCoordinator doesn't derive from that base. With nothing serializing callers:

  • Overlapping stops both took the same _daemons snapshot, ran StopAllAsync on the same daemons at once and disposed each one twice. A caller reaching a daemon the other had already disposed logged "Error while trying to stop daemon agents".
  • A start racing a stop could call StartAllAsync on a daemon the other caller was disposing.
  • A daemon resolved during a stop leaked. The cache was cleared after the fan-out, so a daemon an accessor (DaemonForDatabase and friends) created mid-stop was dropped from the cache without ever being stopped or disposed.

Fix

  • A SemaphoreSlim(1, 1) serializes StartAsync and StopAsync. PauseAsync and ResumeAsync already route through them. StopAsync deliberately waits with CancellationToken.None, so a shutdown always completes its teardown.
  • StopAsync takes and clears the daemon cache in one step before stopping anything. The first stop tears the daemons down and overlapping stops find nothing left, and a daemon resolved mid-stop stays cached for the next stop.
  • An internal UseDaemon(...) test seam seeds the cache so the lifecycle can be tested against fake daemons without a database.

Tests

CoreTests/Bug_5842_explicit_coordinator_overlapping_lifecycle.cs, no database needed:

  • overlapping_stops_and_pauses_stop_and_dispose_each_daemon_once: 16 concurrent Stop/Pause callers over 100 rounds. Each daemon must be stopped and disposed exactly once, with no errors logged.
  • a_daemon_resolved_while_a_stop_is_in_flight_is_not_dropped_undisposed
  • a_start_overlapping_a_stop_never_starts_a_disposed_daemon: 16 concurrent Resume/Stop callers over 100 rounds.

All three fail on master, each on the race it targets, and pass with the fix. The existing coordinator tests also pass locally: 21 in CoreTests (solo mode, externally managed, Bug_4915, Bug_4874) and 8 in DaemonTests (Bug_5055_5056 double stop, pausing_and_resuming_the_daemon, Bug_3080, async_daemon_specifications).

Follow-up

ProjectionCoordinator is fixed by bumping to the JasperFx.Events release that includes JasperFx/jasperfx#1149. This PR doesn't depend on that release.

🤖 Generated with Claude Code

…g stops cannot race (#5842)

JasperFx's RunCommand stops the host again when ApplicationStopping fires, so a
WebApplicationFactory/Alba teardown has two IHost.StopAsync calls in flight.
ProjectionCoordinator gets serialized lifecycle calls from
ProjectionCoordinatorBase (JasperFx/jasperfx#1148), but
ExplicitProjectionCoordinator does not derive from it. Overlapping stops took
the same daemon snapshot, ran StopAllAsync on the same daemons at once and
disposed each one twice; a start racing a stop could start a daemon the other
caller was disposing.

A SemaphoreSlim now serializes StartAsync and StopAsync (Pause/Resume route
through them). StopAsync also takes and clears the daemon cache in one step
before the fan-out: clearing it afterwards dropped any daemon an accessor
resolved during the stop, which then leaked without being stopped or disposed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jeremydmiller
jeremydmiller merged commit cfa2569 into master Nov 9, 2026
32 checks passed
@jeremydmiller
jeremydmiller deleted the fix/5842-explicit-coordinator-overlap branch November 9, 2026 16:16
This was referenced Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ExplicitProjectionCoordinator: overlapping StopAsync/PauseAsync calls stop and dispose the same daemons concurrently

1 participant