Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unsafe .abort() call #7280

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/7280.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed an exception and possible memory leak in Python 3.11.1+ -- by :user:`Dreamsorcerer`
4 changes: 2 additions & 2 deletions aiohttp/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ def _cleanup_closed(self) -> None:
self._cleanup_closed_handle.cancel()

for transport in self._cleanup_closed_transports:
if transport is not None:
if transport is not None and not transport.is_closing():
transport.abort()

self._cleanup_closed_transports = []
Expand Down Expand Up @@ -409,7 +409,7 @@ def _close_immediately(self) -> List["asyncio.Future[None]"]:

# TODO (A.Yushovskiy, 24-May-2019) collect transp. closing futures
for transport in self._cleanup_closed_transports:
if transport is not None:
if transport is not None and not transport.is_closing():
transport.abort()

return waiters
Expand Down
13 changes: 8 additions & 5 deletions tests/test_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -1179,7 +1179,8 @@ async def test_cleanup_closed(loop: Any, mocker: Any) -> None:
mocker.spy(loop, "call_at")
conn = aiohttp.BaseConnector(enable_cleanup_closed=True)

tr = mock.Mock()
tr = mock.create_autospec(asyncio.Transport, spec_set=True, instance=True)
tr.is_closing.return_value = False
conn._cleanup_closed_handle = cleanup_closed_handle = mock.Mock()
conn._cleanup_closed_transports = [tr]
conn._cleanup_closed()
Expand All @@ -1189,10 +1190,11 @@ async def test_cleanup_closed(loop: Any, mocker: Any) -> None:
assert cleanup_closed_handle.cancel.called


async def test_cleanup_closed_disabled(loop: Any, mocker: Any) -> None:
async def test_cleanup_closed_disabled(loop: Any) -> None:
conn = aiohttp.BaseConnector(enable_cleanup_closed=False)

tr = mock.Mock()
tr = mock.create_autospec(asyncio.Transport, spec_set=True, instance=True)
tr.is_closing.return_value = False
conn._cleanup_closed_transports = [tr]
conn._cleanup_closed()
assert tr.abort.called
Expand Down Expand Up @@ -1343,14 +1345,15 @@ async def test_close_cancels_cleanup_handle(loop: Any) -> None:


async def test_close_abort_closed_transports(loop: Any) -> None:
tr = mock.Mock()
tr = mock.create_autospec(asyncio.Transport, spec_set=True, instance=True)
tr.is_closing.return_value = True

conn = aiohttp.BaseConnector()
conn._cleanup_closed_transports.append(tr)
await conn.close()

assert not conn._cleanup_closed_transports
assert tr.abort.called
assert not tr.abort.called
assert conn.closed


Expand Down