Skip to content

Commit 947247f

Browse files
authored
Fix spurious "Future exception was never retrieved" warnings for connection lost errors (#11100)
1 parent 4351c13 commit 947247f

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

CHANGES/11100.bugfix.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fixed spurious "Future exception was never retrieved" warnings for connection lost errors when the connector is not closed -- by :user:`bdraco`.
2+
3+
When connections are lost, the exception is now marked as retrieved since it is always propagated through other means, preventing unnecessary warnings in logs.

aiohttp/client_proto.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ def connection_lost(self, exc: Optional[BaseException]) -> None:
9898
),
9999
original_connection_error,
100100
)
101+
# Mark the exception as retrieved to prevent
102+
# "Future exception was never retrieved" warnings
103+
# The exception is always passed on through
104+
# other means, so this is safe
105+
with suppress(Exception):
106+
self.closed.exception()
101107

102108
if self._payload_parser is not None:
103109
with suppress(Exception): # FIXME: log this somehow?

tests/test_client_proto.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,3 +252,22 @@ async def test_connection_lost_sets_transport_to_none(
252252
proto.connection_lost(OSError())
253253

254254
assert proto.transport is None
255+
256+
257+
async def test_connection_lost_exception_is_marked_retrieved(
258+
loop: asyncio.AbstractEventLoop,
259+
) -> None:
260+
"""Test that connection_lost properly handles exceptions without warnings."""
261+
proto = ResponseHandler(loop=loop)
262+
proto.connection_made(mock.Mock())
263+
264+
# Simulate an SSL shutdown timeout error
265+
ssl_error = TimeoutError("SSL shutdown timed out")
266+
proto.connection_lost(ssl_error)
267+
268+
# Verify the exception was set on the closed future
269+
assert proto.closed.done()
270+
exc = proto.closed.exception()
271+
assert exc is not None
272+
assert "Connection lost: SSL shutdown timed out" in str(exc)
273+
assert exc.__cause__ is ssl_error

0 commit comments

Comments
 (0)