-
-
Notifications
You must be signed in to change notification settings - Fork 97
fix(subscriptions): emit commit diagnostic on the worker thread to stop a _positions data race #552
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
alexeyzimarev
wants to merge
1
commit into
dev
Choose a base branch
from
fix/checkpoint-commit-positions-race
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+76
−2
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
65 changes: 65 additions & 0 deletions
65
src/Core/test/Eventuous.Tests.Subscriptions/CheckpointCommitHandlerConcurrencyTests.cs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| using Eventuous.Diagnostics; | ||
| using Eventuous.Subscriptions.Checkpoints; | ||
| using Shouldly; | ||
|
|
||
| namespace Eventuous.Tests.Subscriptions; | ||
|
|
||
| /// <summary> | ||
| /// Guards against the production wedge (AI-1329) where <see cref="CheckpointCommitHandler.Commit"/> | ||
| /// read the non-thread-safe <c>_positions</c> <see cref="SortedSet{T}"/> ON THE ACK CALLER'S THREAD | ||
| /// (to build the commit diagnostic's <c>FirstPending</c>) while the commit worker thread mutated it — | ||
| /// a data race that threw <see cref="NullReferenceException"/> out of the ack path and dropped the | ||
| /// subscription. The commit diagnostic must be emitted from the worker thread, where <c>_positions</c> | ||
| /// is owned, so it can never race the caller. | ||
| /// </summary> | ||
| public class CheckpointCommitHandlerConcurrencyTests { | ||
| /// <summary> | ||
| /// Captures the managed thread id on which the commit diagnostic is emitted. Attaching any | ||
| /// listener with the commit-handler diagnostic name also flips <c>Diagnostic.IsEnabled("Commit")</c> | ||
| /// to true — exactly what <c>AddEventuousSubscriptions()</c> does in a real app, and why production | ||
| /// hit this while the test suite (no listener) never had. | ||
| /// </summary> | ||
| sealed class ThreadCapturingListener(string subscriptionId, TaskCompletionSource<int> emittedOn) | ||
| : GenericListener(CheckpointCommitHandler.DiagnosticName), IDisposable { | ||
| // The commit DiagnosticListener is process-global, so filter to OUR handler's subscription id | ||
| // (CommitEvent.Id) — otherwise a concurrently-running test's handler could be captured. The | ||
| // event type is internal, so read Id reflectively. Capture the thread DiagnosticSource.Write | ||
| // ran on; it is synchronous, so this is the emitting thread. | ||
| protected override void OnEvent(KeyValuePair<string, object?> evt) { | ||
| var id = evt.Value?.GetType().GetProperty("Id")?.GetValue(evt.Value) as string; | ||
|
|
||
| if (id == subscriptionId) emittedOn.TrySetResult(Environment.CurrentManagedThreadId); | ||
| } | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task Commit_diagnostic_is_not_emitted_on_the_caller_thread(CancellationToken ct) { | ||
| var subscriptionId = $"test-commit-thread-{Guid.NewGuid():N}"; | ||
| var emittedOn = new TaskCompletionSource<int>(TaskCreationOptions.RunContinuationsAsynchronously); | ||
|
|
||
| using var listener = new ThreadCapturingListener(subscriptionId, emittedOn); | ||
|
|
||
| var store = new NoOpCheckpointStore(); | ||
|
|
||
| await using var handler = new CheckpointCommitHandler(subscriptionId, store, TimeSpan.FromMilliseconds(1), batchSize: 1); | ||
|
|
||
| // Call Commit from a DEDICATED thread. Its ManagedThreadId is never a thread-pool id, and the | ||
| // worker runs on the pool — so "emitted != caller" is deterministic (comparing two pool threads | ||
| // could collide under load). Before the fix Commit emits synchronously on this dedicated thread; | ||
| // after it, emission happens on the worker. | ||
| var callerThreadId = 0; | ||
| var caller = new Thread(() => { | ||
| callerThreadId = Environment.CurrentManagedThreadId; | ||
| handler.Commit(new CommitPosition(0, 0, DateTime.UtcNow), ct).AsTask().GetAwaiter().GetResult(); | ||
| }) { IsBackground = true }; | ||
|
Comment on lines
+51
to
+54
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 1. Blocking getresult() in test The new concurrency test blocks on handler.Commit(...).AsTask().GetAwaiter().GetResult() which violates the repo guidance to avoid blocking waits in async flows. Even though it’s test code, this can hide deadlocks/hangs and conflicts with the async/.NoContext() convention. Agent Prompt
|
||
| caller.Start(); | ||
| caller.Join(); | ||
|
|
||
| var emittedThreadId = await emittedOn.Task.WaitAsync(TimeSpan.FromSeconds(5), ct); | ||
|
|
||
| emittedThreadId.ShouldNotBe( | ||
| callerThreadId, | ||
| "the commit diagnostic reads the worker-owned _positions, so it must be emitted on the worker thread, never the ack caller thread" | ||
| ); | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2. Metrics can go stale
🐞 Bug◔ ObservabilityAgent Prompt
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools