Skip to content
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

Improve cancellation in StreamPipeReader.ReadAtLeastAsync #53306

Merged
merged 4 commits into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/libraries/System.IO.Pipelines/src/System/IO/Pipelines/Pipe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,11 @@ internal ValueTask<ReadResult> ReadAtLeastAsync(int minimumBytes, CancellationTo
ThrowHelper.ThrowInvalidOperationException_NoReadingAllowed();
}

if (token.IsCancellationRequested)
{
return new ValueTask<ReadResult>(Task.FromCanceled<ReadResult>(token));
}

CompletionData completionData = default;
ValueTask<ReadResult> result;
lock (SyncObj)
Expand Down Expand Up @@ -715,6 +720,11 @@ internal ValueTask<ReadResult> ReadAsync(CancellationToken token)
ThrowHelper.ThrowInvalidOperationException_NoReadingAllowed();
}

if (token.IsCancellationRequested)
{
return new ValueTask<ReadResult>(Task.FromCanceled<ReadResult>(token));
}

ValueTask<ReadResult> result;
lock (SyncObj)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,10 @@ public override ValueTask<ReadResult> ReadAsync(CancellationToken cancellationTo
// TODO ReadyAsync needs to throw if there are overlapping reads.
ThrowIfCompleted();

cancellationToken.ThrowIfCancellationRequested();
if (cancellationToken.IsCancellationRequested)
{
return new ValueTask<ReadResult>(Task.FromCanceled<ReadResult>(cancellationToken));
}

// PERF: store InternalTokenSource locally to avoid querying it twice (which acquires a lock)
CancellationTokenSource tokenSource = InternalTokenSource;
Expand Down Expand Up @@ -273,7 +276,10 @@ protected override ValueTask<ReadResult> ReadAtLeastAsyncCore(int minimumSize, C
// TODO ReadyAsync needs to throw if there are overlapping reads.
ThrowIfCompleted();

cancellationToken.ThrowIfCancellationRequested();
if (cancellationToken.IsCancellationRequested)
{
return new ValueTask<ReadResult>(Task.FromCanceled<ReadResult>(cancellationToken));
}

// PERF: store InternalTokenSource locally to avoid querying it twice (which acquires a lock)
CancellationTokenSource tokenSource = InternalTokenSource;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public async Task CancelingBetweenReadsThrowsOperationCancelledException()
Pipe.Writer.WriteEmpty(10);
await Pipe.Writer.FlushAsync();

await Assert.ThrowsAsync<OperationCanceledException>(() => task);
await Assert.ThrowsAnyAsync<OperationCanceledException>(() => task);
davidfowl marked this conversation as resolved.
Show resolved Hide resolved
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ public async Task CanReadAtLeast(int bufferSize, bool bufferedRead)
[Fact]
public Task ReadAtLeastAsyncThrowsIfPassedCanceledCancellationToken()
{
return Assert.ThrowsAsync<OperationCanceledException>(async () => await PipeReader.ReadAtLeastAsync(0, new CancellationToken(true)));
ValueTask<ReadResult> task = PipeReader.ReadAtLeastAsync(0, new CancellationToken(canceled: true));
return Assert.ThrowsAnyAsync<OperationCanceledException>(async () => await task);
davidfowl marked this conversation as resolved.
Show resolved Hide resolved
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,12 +362,10 @@ public void ReadAsyncReturnsIsCancelOnCancelPendingReadBeforeGetResult()
}

[Fact]
public void ReadAsyncThrowsIfPassedCanceledCancellationToken()
public async Task ReadAsyncThrowsIfPassedCanceledCancellationToken()
{
var cancellationTokenSource = new CancellationTokenSource();
cancellationTokenSource.Cancel();

Assert.Throws<OperationCanceledException>(() => Pipe.Reader.ReadAsync(cancellationTokenSource.Token));
ValueTask<ReadResult> task = Pipe.Reader.ReadAsync(new CancellationToken(canceled: true));
await Assert.ThrowsAnyAsync<OperationCanceledException>(async () => await task);
}

[Fact]
Expand Down