Repository navigation
Serialize ExplicitProjectionCoordinator lifecycle calls so overlapping stops cannot race (#5842) - #5843
Merged
Conversation
…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>
This was referenced Nov 9, 2026
Closed
This was referenced Nov 17, 2026
This was referenced Nov 19, 2026
This was referenced Nov 26, 2026
Open
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.
Closes #5842. Companion to JasperFx/jasperfx#1148 (fix in JasperFx/jasperfx#1149).
Problem
JasperFx's
RunCommandcallshost.StopAsyncagain whenApplicationStoppingfires. So whenWebApplicationFactoryor Alba stops the host, twoIHost.StopAsynccalls are in flight and both reach the projection coordinator.ProjectionCoordinatorgets serialized lifecycle calls fromProjectionCoordinatorBaseonce the JasperFx fix ships, butExplicitProjectionCoordinatordoesn't derive from that base. With nothing serializing callers:_daemonssnapshot, ranStopAllAsyncon 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".StartAllAsyncon a daemon the other caller was disposing.DaemonForDatabaseand friends) created mid-stop was dropped from the cache without ever being stopped or disposed.Fix
SemaphoreSlim(1, 1)serializesStartAsyncandStopAsync.PauseAsyncandResumeAsyncalready route through them.StopAsyncdeliberately waits withCancellationToken.None, so a shutdown always completes its teardown.StopAsynctakes 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.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_undisposeda_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
ProjectionCoordinatoris 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