Kestrel accept loop permanently dies when ILogger throws — causes silent hang
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 6h
- Merged PRs (30d)
- 290
Description
The Kestrel connection accept loop in `ConnectionDispatcher` wraps the **entire** `while(true)` loop in a single `try/catch`. If any exception is thrown inside the loop body — including from `ILogger` calls — the accept loop exits permanently. The process remains alive but never accepts another connection.
The `ConnectionDispatcher.cs` source even has a comment acknowledging this
A transient failure in logging (or other non-critical operations inside the accept loop) should not permanently kill the connection listener. The accept loop should either:
1. Wrap per-iteration work in its own `try/catch` so transient failures don't exit the loop, OR
2. Implement retry logic with backoff before giving up, OR
3. Trigger a graceful server shutdown instead of silently hanging
A sample stack trace:
[CRIT] [Microsoft.AspNetCore.Server.Kestrel] The connection listener failed to accept any new connections.
System.AggregateException: An error occurred while writing to logger(s). (Access is denied.)
---> System.ComponentModel.Win32Exception (5): Access is denied.
at System.Diagnostics.EventLogInternal.InternalWriteEvent(UInt32 eventID, UInt16 category, EventLogEntryType type, String[] strings, Byte[] rawData, String currentMachineName)
at System.Diagnostics.EventLogInternal.WriteEvent(EventInstance instance, Byte[] data, Object[] values)
at Microsoft.Extensions.Logging.EventLog.WindowsEventLog.WriteEntry(String message, EventLogEntryType type, Int32 eventID, Int16 category)
at Microsoft.Extensions.Logging.EventLog.EventLogLogger.Log[TState](LogLevel logLevel, EventId eventId, TState state, Exception exception, Func`3 formatter)
at Microsoft.Extensions.Logging.Logger.g__LoggerLog|14_0[TState](LogLevel logLevel, EventId eventId, ILogger logger, Exception exception, Func`3 formatter, List`1& exceptions, TState& state)
--- End of inner exception stack trace ---
at Microsoft.Extensions.Logging.Logger.ThrowLoggingError(List`1 exceptions)
at Microsoft.Extensions.Logging.Logger.Log[TState](LogLevel logLevel, EventId eventId, TState state, Exception exception, Func`3 formatter)
at Microsoft.Extensions.Logging.LoggerMessage.<>c__DisplayClass10_0`1.g__Log|0(ILogger logger, T1 arg1, Exception exception)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.ConnectionDispatcher`1.<>c__DisplayClass10_0.<g__AcceptConnectionsAsync|0>d.MoveNext()
- Process stays alive with normal CPU/memory — no crash, no restart
- Port shows `LISTENING` in `netstat`
- The `CRIT` log entry `"The connection listener failed to accept any new connections"` appears exactly once
This is a **silent, permanent failure** that is extremely difficult to diagnose:
- No process crash → no automatic restart by the host (Service Fabric, IIS, etc.)
Contributor guide
Assessment
This issue has not been assessed yet.