Skip to content

Commit 374f717

Browse files
authored
Prevent WebSockets from throwing during graceful shutdown (#26914)
* Fix canceled ReadResult handling in Http1UpgradeMessageBody * Add UpgradeTests.DoesNotThrowGivenCanceledReadResult
1 parent 7151133 commit 374f717

File tree

2 files changed

+92
-4
lines changed

2 files changed

+92
-4
lines changed

src/Servers/Kestrel/Core/src/Internal/Http/Http1UpgradeMessageBody.cs

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http
1414
/// </summary>
1515
internal sealed class Http1UpgradeMessageBody : Http1MessageBody
1616
{
17+
private int _userCanceled;
18+
1719
public Http1UpgradeMessageBody(Http1Connection context)
1820
: base(context)
1921
{
@@ -26,13 +28,13 @@ public Http1UpgradeMessageBody(Http1Connection context)
2628
public override ValueTask<ReadResult> ReadAsync(CancellationToken cancellationToken = default)
2729
{
2830
ThrowIfCompleted();
29-
return _context.Input.ReadAsync(cancellationToken);
31+
return ReadAsyncInternal(cancellationToken);
3032
}
3133

3234
public override bool TryRead(out ReadResult result)
3335
{
3436
ThrowIfCompleted();
35-
return _context.Input.TryRead(out result);
37+
return TryReadInternal(out result);
3638
}
3739

3840
public override void AdvanceTo(SequencePosition consumed)
@@ -54,6 +56,7 @@ public override void Complete(Exception exception)
5456

5557
public override void CancelPendingRead()
5658
{
59+
Interlocked.Exchange(ref _userCanceled, 1);
5760
_context.Input.CancelPendingRead();
5861
}
5962

@@ -69,12 +72,49 @@ public override Task StopAsync()
6972

7073
public override bool TryReadInternal(out ReadResult readResult)
7174
{
72-
return _context.Input.TryRead(out readResult);
75+
// Ignore the canceled readResult unless it was canceled by the user.
76+
do
77+
{
78+
if (!_context.Input.TryRead(out readResult))
79+
{
80+
return false;
81+
}
82+
} while (readResult.IsCanceled && Interlocked.Exchange(ref _userCanceled, 0) == 0);
83+
84+
return true;
7385
}
7486

7587
public override ValueTask<ReadResult> ReadAsyncInternal(CancellationToken cancellationToken = default)
7688
{
77-
return _context.Input.ReadAsync(cancellationToken);
89+
ReadResult readResult;
90+
91+
// Ignore the canceled readResult unless it was canceled by the user.
92+
do
93+
{
94+
var readTask = _context.Input.ReadAsync(cancellationToken);
95+
96+
if (!readTask.IsCompletedSuccessfully)
97+
{
98+
return ReadAsyncInternalAwaited(readTask, cancellationToken);
99+
}
100+
101+
readResult = readTask.GetAwaiter().GetResult();
102+
} while (readResult.IsCanceled && Interlocked.Exchange(ref _userCanceled, 0) == 0);
103+
104+
return new ValueTask<ReadResult>(readResult);
105+
}
106+
107+
private async ValueTask<ReadResult> ReadAsyncInternalAwaited(ValueTask<ReadResult> readTask, CancellationToken cancellationToken = default)
108+
{
109+
var readResult = await readTask;
110+
111+
// Ignore the canceled readResult unless it was canceled by the user.
112+
while (readResult.IsCanceled && Interlocked.Exchange(ref _userCanceled, 0) == 0)
113+
{
114+
readResult = await _context.Input.ReadAsync(cancellationToken);
115+
}
116+
117+
return readResult;
78118
}
79119
}
80120
}

src/Servers/Kestrel/test/InMemory.FunctionalTests/UpgradeTests.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33

44
using System;
55
using System.IO;
6+
using System.IO.Pipelines;
67
using System.Threading.Tasks;
8+
using Microsoft.AspNetCore.Connections;
9+
using Microsoft.AspNetCore.Connections.Features;
710
using Microsoft.AspNetCore.Http.Features;
811
using Microsoft.AspNetCore.Server.Kestrel.Core;
912
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure;
@@ -343,5 +346,50 @@ await connection.Receive("HTTP/1.1 101 Switching Protocols",
343346
await appCompletedTcs.Task.DefaultTimeout();
344347
}
345348
}
349+
350+
[Fact]
351+
public async Task DoesNotThrowGivenCanceledReadResult()
352+
{
353+
var appCompletedTcs = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously);
354+
355+
await using var server = new TestServer(async context =>
356+
{
357+
try
358+
{
359+
var upgradeFeature = context.Features.Get<IHttpUpgradeFeature>();
360+
var duplexStream = await upgradeFeature.UpgradeAsync();
361+
362+
// Kestrel will call Transport.Input.CancelPendingRead() during shutdown so idle connections
363+
// can wake up and shutdown gracefully. We manually call CancelPendingRead() to simulate this and
364+
// ensure the Stream returned by UpgradeAsync doesn't throw in this case.
365+
// https://github.com/dotnet/aspnetcore/issues/26482
366+
var connectionTransportFeature = context.Features.Get<IConnectionTransportFeature>();
367+
connectionTransportFeature.Transport.Input.CancelPendingRead();
368+
369+
// Use ReadAsync() instead of CopyToAsync() for this test since IsCanceled is only checked in
370+
// HttpRequestStream.ReadAsync() and not HttpRequestStream.CopyToAsync()
371+
Assert.Equal(0, await duplexStream.ReadAsync(new byte[1]));
372+
appCompletedTcs.SetResult(null);
373+
}
374+
catch (Exception ex)
375+
{
376+
appCompletedTcs.SetException(ex);
377+
throw;
378+
}
379+
},
380+
new TestServiceContext(LoggerFactory));
381+
382+
using (var connection = server.CreateConnection())
383+
{
384+
await connection.SendEmptyGetWithUpgrade();
385+
await connection.Receive("HTTP/1.1 101 Switching Protocols",
386+
"Connection: Upgrade",
387+
$"Date: {server.Context.DateHeaderValue}",
388+
"",
389+
"");
390+
}
391+
392+
await appCompletedTcs.Task.DefaultTimeout();
393+
}
346394
}
347395
}

0 commit comments

Comments
 (0)