Skip to content

Commit 9929f53

Browse files
authored
Merge pull request #173 from AArnott/fix170
Consistently complete with OCE on cancellation
2 parents ed908f4 + 8e5cadd commit 9929f53

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/Nerdbank.Streams/PipeExtensions.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,9 @@ public static PipeWriter UsePipeWriter(this Stream stream, PipeOptions? pipeOpti
111111
{
112112
try
113113
{
114-
while (!cancellationToken.IsCancellationRequested)
114+
while (true)
115115
{
116+
cancellationToken.ThrowIfCancellationRequested();
116117
ReadResult readResult = await pipe.Reader.ReadAsync(cancellationToken).ConfigureAwait(false);
117118
if (readResult.Buffer.Length > 0)
118119
{
@@ -233,11 +234,12 @@ public static PipeReader UsePipeReader(this WebSocket webSocket, int sizeHint =
233234
var pipe = new Pipe(pipeOptions ?? PipeOptions.Default);
234235
Task.Run(async delegate
235236
{
236-
while (!cancellationToken.IsCancellationRequested)
237+
while (true)
237238
{
238239
Memory<byte> memory = pipe.Writer.GetMemory(sizeHint);
239240
try
240241
{
242+
cancellationToken.ThrowIfCancellationRequested();
241243
var readResult = await webSocket.ReceiveAsync(memory, cancellationToken).ConfigureAwait(false);
242244
if (readResult.Count == 0)
243245
{
@@ -283,8 +285,9 @@ public static PipeWriter UsePipeWriter(this WebSocket webSocket, PipeOptions? pi
283285
{
284286
try
285287
{
286-
while (!cancellationToken.IsCancellationRequested)
288+
while (true)
287289
{
290+
cancellationToken.ThrowIfCancellationRequested();
288291
ReadResult readResult = await pipe.Reader.ReadAsync(cancellationToken).ConfigureAwait(false);
289292
if (readResult.Buffer.Length > 0)
290293
{
@@ -389,8 +392,9 @@ internal static Task LinkToAsync(this PipeReader reader, PipeWriter writer, bool
389392
{
390393
try
391394
{
392-
while (!cancellationToken.IsCancellationRequested)
395+
while (true)
393396
{
397+
cancellationToken.ThrowIfCancellationRequested();
394398
var result = await reader.ReadAsync(cancellationToken).ConfigureAwait(false);
395399
writer.Write(result.Buffer);
396400
reader.AdvanceTo(result.Buffer.End);

0 commit comments

Comments
 (0)