Serialize InFlightScriptStore reads on the per-task lock stripe#430
Merged
Conversation
TryGetTicketAsync queried the shared scoped DbContext without taking the per-task lock stripe the writes already use. A parallel batch runs every target's re-attach probe on the one DbContext the Hangfire worker owns for the task, so two concurrent probes (or a probe racing a record/clear) drove two operations onto the same context and EF threw "a second operation was started on this context instance" — an intermittent crash in multi-target deployments that use crash-resume re-attach. Gate the read with the same stripe as the writes so all of a task's DbContext access serialises onto one writer. Adds an integration regression test that resolves one store (one DbContext) and fires concurrent reads+writes: red before the fix with the exact EF error, green after.
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.
Summary
InFlightScriptStore.TryGetTicketAsync(the crash-resume re-attach probe) queried the shared scopedIRepository/DbContext without taking the per-serverTaskIdlock stripe that the writes (RecordDispatchedAsync/ClearAsync) already use.A second operation was started on this context instance. Intermittent crash in multi-target deployments using crash-resume re-attach.Why this surfaced now
The
KubernetesResumeCheckpointE2ETests/TentacleMixedModeE2ETests.MixedMode_SingleDeployment_BothTargetsExecuteE2E (two targets in parallel) hits this reattach path and exposed the race intermittently — it's a narrow timing window, so most CI runs win it. Pre-existing onmain; not introduced by any recent change.Non-breaking + generic
MutateAsync) is untouched; the fix mirrors its existing inline gate pattern.Test plan
InFlightScriptStoreTests.ConcurrentReadsAndWrites_OnOneSharedContext_DoNotThrow— resolves one store (one DbContext) and fires concurrent reads+writes. Verified red→green: without the fix it fails with the exact production error (A second operation was started on this context instance); with the fix it passes. (The other tests here use a freshRunscope per call and so never share a context — this one deliberately doesn't.)dotnet build— 0 errors.