diff --git a/src/Servers/Kestrel/Transport.NamedPipes/src/Internal/NamedPipeConnectionListener.cs b/src/Servers/Kestrel/Transport.NamedPipes/src/Internal/NamedPipeConnectionListener.cs index 693f8d4b23d6..5f05a46300ca 100644 --- a/src/Servers/Kestrel/Transport.NamedPipes/src/Internal/NamedPipeConnectionListener.cs +++ b/src/Servers/Kestrel/Transport.NamedPipes/src/Internal/NamedPipeConnectionListener.cs @@ -94,6 +94,15 @@ private async Task StartAsync(NamedPipeServerStream nextStream) } } } + catch (IOException ex) when (!_listeningToken.IsCancellationRequested) + { + // WaitForConnectionAsync can throw IOException when the pipe is broken. + NamedPipeLog.ConnectionListenerBrokenPipe(_log, ex); + + // Dispose existing pipe, create a new one and continue accepting. + nextStream.Dispose(); + nextStream = CreateServerStream(); + } catch (OperationCanceledException ex) when (_listeningToken.IsCancellationRequested) { // Cancelled the current token diff --git a/src/Servers/Kestrel/Transport.NamedPipes/src/Internal/NamedPipeLog.cs b/src/Servers/Kestrel/Transport.NamedPipes/src/Internal/NamedPipeLog.cs index 0adc04481e1f..e02ad8bb9a6d 100644 --- a/src/Servers/Kestrel/Transport.NamedPipes/src/Internal/NamedPipeLog.cs +++ b/src/Servers/Kestrel/Transport.NamedPipes/src/Internal/NamedPipeLog.cs @@ -77,4 +77,6 @@ public static void ConnectionDisconnect(ILogger logger, NamedPipeConnection conn } } + [LoggerMessage(8, LogLevel.Debug, "Named pipe listener received broken pipe while waiting for a connection.", EventName = "ConnectionListenerBrokenPipe")] + public static partial void ConnectionListenerBrokenPipe(ILogger logger, Exception ex); }