Skip to content

Commit

Permalink
Fix for #8253 "Unclosed client session" when initialization fails (#8290
Browse files Browse the repository at this point in the history
)

(cherry picked from commit 5fd2946)
  • Loading branch information
NewGlad authored and Alexey Nikitin committed Apr 8, 2024
1 parent 7853b08 commit 0be34ad
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES/8253.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed "Unclosed client session" when initialization of ClientSession fails -- by :user:`NewGlad`.
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Alexander Shorin
Alexander Travov
Alexandru Mihai
Alexey Firsov
Alexey Nikitin
Alexey Popravka
Alexey Stepanov
Amin Etesamian
Expand Down
14 changes: 14 additions & 0 deletions aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,20 @@ def __init__(

if connector is None:
connector = TCPConnector(loop=loop)
loop = asyncio.get_running_loop()

if connector is None:
connector = TCPConnector()
# Initialize these three attrs before raising any exception,
# they are used in __del__
self._connector = connector
self._loop = loop
if loop.get_debug():
self._source_traceback: Optional[traceback.StackSummary] = (
traceback.extract_stack(sys._getframe(1))
)
else:
self._source_traceback = None

if connector._loop is not loop:
raise RuntimeError("Session and connector has to use same event loop")
Expand Down
5 changes: 5 additions & 0 deletions tests/test_client_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -885,3 +885,8 @@ async def test_build_url_returns_expected_url(
) -> None:
session = await create_session(base_url)
assert session._build_url(url) == expected_url


async def test_instantiation_with_invalid_timeout_value():
with pytest.raises(ValueError, match="timeout parameter cannot be .*"):
ClientSession(timeout=1)

0 comments on commit 0be34ad

Please sign in to comment.