Avoid unobserved tasks in WebSocketsTransport (#12315)

This commit is contained in:
Brennan 2019-07-25 14:25:00 -07:00 committed by GitHub
parent 7209e0410c
commit 648bdf706e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 13 deletions

View File

@ -61,7 +61,7 @@ namespace Microsoft.AspNetCore.Http.Connections.Client.Internal
LoggerMessage.Define(LogLevel.Information, new EventId(16, "ClosingWebSocket"), "Closing WebSocket.");
private static readonly Action<ILogger, Exception> _closingWebSocketFailed =
LoggerMessage.Define(LogLevel.Information, new EventId(17, "ClosingWebSocketFailed"), "Closing webSocket failed.");
LoggerMessage.Define(LogLevel.Debug, new EventId(17, "ClosingWebSocketFailed"), "Closing webSocket failed.");
private static readonly Action<ILogger, Exception> _cancelMessage =
LoggerMessage.Define(LogLevel.Debug, new EventId(18, "CancelMessage"), "Canceled passing message to application.");

View File

@ -269,10 +269,6 @@ namespace Microsoft.AspNetCore.Http.Connections.Client.Internal
if (!_aborted)
{
_application.Output.Complete(ex);
// We re-throw here so we can communicate that there was an error when sending
// the close frame
throw;
}
}
finally
@ -347,8 +343,15 @@ namespace Microsoft.AspNetCore.Http.Connections.Client.Internal
{
if (WebSocketCanSend(socket))
{
// We're done sending, send the close frame to the client if the websocket is still open
await socket.CloseOutputAsync(error != null ? WebSocketCloseStatus.InternalServerError : WebSocketCloseStatus.NormalClosure, "", CancellationToken.None);
try
{
// We're done sending, send the close frame to the client if the websocket is still open
await socket.CloseOutputAsync(error != null ? WebSocketCloseStatus.InternalServerError : WebSocketCloseStatus.NormalClosure, "", CancellationToken.None);
}
catch (Exception ex)
{
Log.ClosingWebSocketFailed(_logger, ex);
}
}
_application.Input.Complete();

View File

@ -189,10 +189,6 @@ namespace Microsoft.AspNetCore.Http.Connections.Internal.Transports
if (!_aborted && !token.IsCancellationRequested)
{
_application.Output.Complete(ex);
// We re-throw here so we can communicate that there was an error when sending
// the close frame
throw;
}
}
finally
@ -270,8 +266,15 @@ namespace Microsoft.AspNetCore.Http.Connections.Internal.Transports
// Send the close frame before calling into user code
if (WebSocketCanSend(socket))
{
// We're done sending, send the close frame to the client if the websocket is still open
await socket.CloseOutputAsync(error != null ? WebSocketCloseStatus.InternalServerError : WebSocketCloseStatus.NormalClosure, "", CancellationToken.None);
try
{
// We're done sending, send the close frame to the client if the websocket is still open
await socket.CloseOutputAsync(error != null ? WebSocketCloseStatus.InternalServerError : WebSocketCloseStatus.NormalClosure, "", CancellationToken.None);
}
catch (Exception ex)
{
Log.ClosingWebSocketFailed(_logger, ex);
}
}
_application.Input.Complete();

View File

@ -53,6 +53,9 @@ namespace Microsoft.AspNetCore.Http.Connections.Internal.Transports
private static readonly Action<ILogger, Exception> _closedPrematurely =
LoggerMessage.Define(LogLevel.Debug, new EventId(14, "ClosedPrematurely"), "Socket connection closed prematurely.");
private static readonly Action<ILogger, Exception> _closingWebSocketFailed =
LoggerMessage.Define(LogLevel.Debug, new EventId(15, "ClosingWebSocketFailed"), "Closing webSocket failed.");
public static void SocketOpened(ILogger logger, string subProtocol)
{
_socketOpened(logger, subProtocol, null);
@ -122,6 +125,11 @@ namespace Microsoft.AspNetCore.Http.Connections.Internal.Transports
{
_closedPrematurely(logger, ex);
}
public static void ClosingWebSocketFailed(ILogger logger, Exception ex)
{
_closingWebSocketFailed(logger, ex);
}
}
}
}