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

[release/6.0] Fix cancel in PipeReader.ReadAtLeastAsync #66870

Merged
merged 2 commits into from
Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -347,15 +347,15 @@ internal ValueTask<FlushResult> FlushAsync(CancellationToken cancellationToken)
ValueTask<FlushResult> result;
lock (SyncObj)
{
PrepareFlush(out completionData, out result, cancellationToken);
PrepareFlushUnsynchronized(out completionData, out result, cancellationToken);
}

TrySchedule(ReaderScheduler, completionData);

return result;
}

private void PrepareFlush(out CompletionData completionData, out ValueTask<FlushResult> result, CancellationToken cancellationToken)
private void PrepareFlushUnsynchronized(out CompletionData completionData, out ValueTask<FlushResult> result, CancellationToken cancellationToken)
{
var completeReader = CommitUnsynchronized();

Expand Down Expand Up @@ -691,6 +691,9 @@ internal ValueTask<ReadResult> ReadAtLeastAsync(int minimumBytes, CancellationTo

// We also need to flip the reading state off
_operationState.EndRead();

// Begin read again to wire up cancellation token
_readerAwaitable.BeginOperation(token, s_signalReaderAwaitable, this);
}

// If the writer is currently paused and we are about the wait for more data then this would deadlock.
Expand Down Expand Up @@ -1057,7 +1060,7 @@ internal ValueTask<FlushResult> WriteAsync(ReadOnlyMemory<byte> source, Cancella
WriteMultiSegment(source.Span);
}

PrepareFlush(out completionData, out result, cancellationToken);
PrepareFlushUnsynchronized(out completionData, out result, cancellationToken);
}

TrySchedule(ReaderScheduler, completionData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,5 +162,36 @@ public async Task WriteAndCancellingPendingReadBeforeReadAtLeastAsync()
Assert.True(result.IsCanceled);
PipeReader.AdvanceTo(buffer.End);
}

[Fact]
public Task ReadAtLeastAsyncCancelableWhenWaitingForMoreData()
{
CancellationTokenSource cts = new CancellationTokenSource();
ValueTask<ReadResult> task = PipeReader.ReadAtLeastAsync(1, cts.Token);
cts.Cancel();
return Assert.ThrowsAsync<OperationCanceledException>(async () => await task);
}

[Fact]
public async Task ReadAtLeastAsyncCancelableAfterReadingSome()
{
CancellationTokenSource cts = new CancellationTokenSource();
await Pipe.WriteAsync(new byte[10], default);
ValueTask<ReadResult> task = PipeReader.ReadAtLeastAsync(11, cts.Token);
cts.Cancel();
await Assert.ThrowsAsync<OperationCanceledException>(async () => await task);
}

[Fact]
public async Task ReadAtLeastAsyncCancelableAfterReadingSomeAndWritingAfterStartingRead()
{
CancellationTokenSource cts = new CancellationTokenSource();
await Pipe.WriteAsync(new byte[10], default);
ValueTask<ReadResult> task = PipeReader.ReadAtLeastAsync(12, cts.Token);
// Write, but not enough to unblock ReadAtLeastAsync
await Pipe.WriteAsync(new byte[1], default);
cts.Cancel();
await Assert.ThrowsAnyAsync<OperationCanceledException>(async () => await task);
}
}
}