Skip to content

Commit e521b0a

Browse files
committed
fix: suppress benign Windows socket cleanup errors in asyncio
1 parent 2c59435 commit e521b0a

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

main.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,29 @@ def main():
271271
log.info("Stopped")
272272

273273

274+
def _make_exception_handler(log):
275+
"""Return an asyncio exception handler that silences Windows WinError 10054
276+
noise from connection cleanup (ConnectionResetError in
277+
_ProactorBasePipeTransport._call_connection_lost), which is harmless but
278+
verbose on Python/Windows when a remote host force-closes a socket."""
279+
def handler(loop, context):
280+
exc = context.get("exception")
281+
cb = context.get("handle") or context.get("source_traceback", "")
282+
if (
283+
isinstance(exc, ConnectionResetError)
284+
and "_call_connection_lost" in str(cb)
285+
):
286+
return # suppress: benign Windows socket cleanup race
287+
log.error("[asyncio] %s", context.get("message", context))
288+
if exc:
289+
loop.default_exception_handler(context)
290+
return handler
291+
292+
274293
async def _run(config):
294+
loop = asyncio.get_running_loop()
295+
_log = logging.getLogger("asyncio")
296+
loop.set_exception_handler(_make_exception_handler(_log))
275297
server = ProxyServer(config)
276298
try:
277299
await server.start()

0 commit comments

Comments
 (0)