Skip to content
This repository was archived by the owner on Apr 8, 2020. It is now read-only.

Commit b2c1062

Browse files
Make ConditionalProxy shut down the WebSocket proxy much faster when the app is shutting down
1 parent ae7ae65 commit b2c1062

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/Microsoft.AspNetCore.SpaServices.Extensions/Proxying/ConditionalProxy.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,26 @@ private static async Task PumpWebSocket(WebSocket source, WebSocket destination,
249249

250250
while (true)
251251
{
252-
var result = await source.ReceiveAsync(new ArraySegment<byte>(buffer), cancellationToken);
252+
// Because WebSocket.ReceiveAsync doesn't work well with CancellationToken (it doesn't
253+
// actually exit when the token notifies, at least not in the 'server' case), use
254+
// polling. The perf might not be ideal, but this is a dev-time feature only.
255+
var resultTask = source.ReceiveAsync(new ArraySegment<byte>(buffer), cancellationToken);
256+
while (true)
257+
{
258+
if (cancellationToken.IsCancellationRequested)
259+
{
260+
return;
261+
}
262+
263+
if (resultTask.IsCompleted)
264+
{
265+
break;
266+
}
267+
268+
await Task.Delay(250);
269+
}
253270

271+
var result = resultTask.Result; // We know it's completed already
254272
if (result.MessageType == WebSocketMessageType.Close)
255273
{
256274
if (destination.State == WebSocketState.Open || destination.State == WebSocketState.CloseReceived)

0 commit comments

Comments
 (0)