Fix synchronous cancellation race in Channels - #132230
Open
steveisok wants to merge 1 commit into
Open
Conversation
Use stable cancelability state initialized before cancellation registration, and add deterministic regression coverage for bounded and rendezvous channels.\n\nCo-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
|
Azure Pipelines: Successfully started running 3 pipeline(s). 13 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
|
Tagging subscribers to this area: @JulieLeeMSFT, @VSadov |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a race in System.Threading.Channels async operations where synchronous cancellation during token registration could cause completion to proceed without an atomic reservation, enabling double-completion and corrupting waiter/reader linked lists (with downstream assertion failures and potential item loss).
Changes:
- Make cancelability an immutable construction-time property (
_isCancelable) and use it for completion reservation decisions, avoiding reliance on_cancellationRegistration.Tokenwhen registration may be a default value. - Add test infrastructure to construct synchronously-canceled internal channel async operations and inspect/modify their linked-list state.
- Add deterministic regression tests for rendezvous waiter removal and bounded-channel direct handoff skipping canceled readers.
Show a summary per file
| File | Description |
|---|---|
| src/libraries/System.Threading.Channels/src/System/Threading/Channels/AsyncOperation.cs | Introduces _isCancelable and switches completion reservation/assert logic to use it, addressing the synchronous-cancellation registration race. |
| src/libraries/System.Threading.Channels/tests/TestBase.cs | Adds reflection-based helpers to create/cancel internal async operations and manipulate their list links/heads for deterministic regression coverage. |
| src/libraries/System.Threading.Channels/tests/RendezvousChannelTests.cs | Adds a regression test validating delayed removal of a synchronously-canceled waiter doesn’t corrupt the waiting-reader list. |
| src/libraries/System.Threading.Channels/tests/BoundedChannelTests.cs | Adds regression tests for synchronous cancellation during registration and for skipping canceled readers during direct handoff. |
Review details
- Files reviewed: 4/4 changed files
- Comments generated: 1
- Review effort level: Lite
Comment on lines
+37
to
+60
| protected static object CreateSynchronouslyCanceledAsyncOperation( | ||
| string typeName, | ||
| CancellationToken cancellationToken, | ||
| Type genericTypeArgument = null) | ||
| { | ||
| Type operationType = typeof(Channel<int>).Assembly.GetType(typeName, throwOnError: true); | ||
| if (genericTypeArgument is not null) | ||
| { | ||
| operationType = operationType.MakeGenericType(genericTypeArgument); | ||
| } | ||
|
|
||
| MethodInfo trySetCanceled = operationType.GetMethod("TrySetCanceled", BindingFlags.Instance | BindingFlags.Public); | ||
| var cancellationCallback = new Action<object, CancellationToken>((state, token) => | ||
| { | ||
| Assert.True((bool)trySetCanceled.Invoke(state, new object[] { token })); | ||
| }); | ||
|
|
||
| return Activator.CreateInstance( | ||
| operationType, | ||
| BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, | ||
| binder: null, | ||
| args: new object[] { true, cancellationToken, false, cancellationCallback }, | ||
| culture: null); | ||
| } |
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.
Fixes #129796
Fixes #132094
AsyncOperationused_cancellationRegistration.Tokento determine whether completion needed atomic reservation. If cancellation ran synchronously insideUnsafeRegister, the registration had not yet been assigned, so the callback observed a default non-cancelable token and completed without reserving the operation.A reader or waiter could then be reserved and completed a second time. This caused linked-list assertion failures in
RendezvousChanneland could silently lose items during aBoundedChanneldirect handoff.Use an immutable
_isCancelablevalue initialized before registration can invoke the callback. This preserves the non-cancelable fast path, pooling behavior, and registration cleanup without retaining the fullCancellationTokenon modern .NET.Adds deterministic coverage for:
The added field fits existing object padding;
BlockedReadAsyncOperation<int>remains 96 bytes.Validation
Note
This pull request description was generated with the assistance of GitHub Copilot.