From b732b4fc72254149661231274d9ee199c0305dbf Mon Sep 17 00:00:00 2001 From: Elvis Pranskevichus Date: Fri, 18 Oct 2024 08:38:30 -0700 Subject: [PATCH] tests: Attempt to inhibit spurious ConnectionResetError on Windows (#1190) The proactor stuff on Windows raises a spurious `ConnectionResetError` while trying to shut down a socket in the `connection_lost` path. This is likely a Python bug (and nobody noticed because this is a background exception). Try to filter it out before complaining. --- asyncpg/_testbase/__init__.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/asyncpg/_testbase/__init__.py b/asyncpg/_testbase/__init__.py index 7aca834f..d4e0d43d 100644 --- a/asyncpg/_testbase/__init__.py +++ b/asyncpg/_testbase/__init__.py @@ -117,10 +117,22 @@ def setUp(self): self.__unhandled_exceptions = [] def tearDown(self): - if self.__unhandled_exceptions: + excs = [] + for exc in self.__unhandled_exceptions: + if isinstance(exc, ConnectionResetError): + texc = traceback.TracebackException.from_exception( + exc, lookup_lines=False) + if texc.stack[-1].name == "_call_connection_lost": + # On Windows calling socket.shutdown may raise + # ConnectionResetError, which happens in the + # finally block of _call_connection_lost. + continue + excs.append(exc) + + if excs: formatted = [] - for i, context in enumerate(self.__unhandled_exceptions): + for i, context in enumerate(excs): formatted.append(self._format_loop_exception(context, i + 1)) self.fail(