This repository was archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Fix ReadAsync not returning when cancellation token fires during FlushAsync #31212
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
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 |
|---|---|---|
|
|
@@ -325,6 +325,81 @@ public async Task FlushAsyncThrowsIfPassedCanceledCancellationTokenAndPipeIsAble | |
| Assert.True(task.IsCompleted); | ||
| Assert.True(task.Result.IsCompleted); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ReadAsyncCompletesIfFlushAsyncCanceledMidFlush() | ||
| { | ||
| // This test tries to get pipe into a state where ReadAsync is being awaited | ||
| // and FlushAsync is cancelled while the method is running | ||
| Pipe = new Pipe(); | ||
| var resetEvent = new ManualResetEvent(false); | ||
|
|
||
| var cancellationTokenSource = new CancellationTokenSource(); | ||
| var writer = Task.Run(async () => | ||
| { | ||
| var cancellations = 0; | ||
| while (cancellations < 20) | ||
| { | ||
| try | ||
| { | ||
| // We want reader to be awaiting | ||
| resetEvent.WaitOne(); | ||
| Pipe.Writer.WriteEmpty(1); | ||
|
|
||
| // We want the token to be cancelled during FlushAsync call | ||
| // check it it's already cancelled and try a new one | ||
| if (cancellationTokenSource.Token.IsCancellationRequested) | ||
| { | ||
| cancellationTokenSource = new CancellationTokenSource(); | ||
| continue; | ||
| } | ||
|
|
||
| await Pipe.Writer.FlushAsync(cancellationTokenSource.Token); | ||
| } | ||
| catch (OperationCanceledException) | ||
| { | ||
| cancellations ++; | ||
| } | ||
| } | ||
|
|
||
| Pipe.Writer.Complete(); | ||
| return; | ||
| }); | ||
|
|
||
| var reader = Task.Run(async () => | ||
| { | ||
| while (true) | ||
| { | ||
| var readTask = Pipe.Reader.ReadAsync(); | ||
|
|
||
| // Signal writer to initiate a flush | ||
| if (!readTask.IsCompleted) | ||
| { | ||
| resetEvent.Set(); | ||
| } | ||
|
|
||
| var result = await readTask; | ||
| resetEvent.Reset(); | ||
|
|
||
| if (result.Buffer.IsEmpty) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| Pipe.Reader.AdvanceTo(result.Buffer.End); | ||
| } | ||
| }); | ||
|
|
||
| var canceller = Task.Run(() => | ||
| { | ||
| while (!writer.IsCompleted) | ||
| { | ||
| cancellationTokenSource.Cancel(); | ||
| } | ||
| }); | ||
|
|
||
| Assert.True(Task.WaitAll(new [] { writer, reader, canceller }, TimeSpan.FromSeconds(30)), "Reader was not completed in reasonable time"); | ||
|
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. Isn't potentially spending 30 seconds on a single unit test too long? Does the test still work as intended if we change this to < 5 seconds?
Author
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. Only if fails. I don't want to introduce the test that would be flaky on slower machines. In the successful case, it passes quickly. |
||
| } | ||
| } | ||
|
|
||
| public static class TestWriterExtensions | ||
|
|
||
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.
Any particular reason for 20?
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.
No, just a random number.