Skip to content

Commit

Permalink
Fix for aio-libs#8253 "Unclosed client session" when initialization f…
Browse files Browse the repository at this point in the history
…ails (aio-libs#8290)
  • Loading branch information
NewGlad authored Apr 7, 2024
1 parent 28d026e commit 5fd2946
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 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
Almaz Salakhov
Expand Down
23 changes: 13 additions & 10 deletions aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ def __init__(
max_field_size: int = 8190,
fallback_charset_resolver: _CharsetResolver = lambda r, b: "utf-8",
) -> None:
# We initialise _connector to None immediately, as it's referenced in __del__()
# and could cause issues if an exception occurs during initialisation.
self._connector: Optional[BaseConnector] = None
if base_url is None or isinstance(base_url, URL):
self._base_url: Optional[URL] = base_url
else:
Expand All @@ -253,12 +256,20 @@ def __init__(

loop = asyncio.get_running_loop()

if timeout is sentinel or timeout is None:
timeout = DEFAULT_TIMEOUT
if not isinstance(timeout, ClientTimeout):
raise ValueError(
f"timeout parameter cannot be of {type(timeout)} type, "
"please use 'timeout=ClientTimeout(...)'",
)
self._timeout = timeout

if connector is None:
connector = TCPConnector()

# Initialize these three attrs before raising any exception,
# they are used in __del__
self._connector: Optional[BaseConnector] = connector
self._connector = connector
self._loop = loop
if loop.get_debug():
self._source_traceback: Optional[traceback.StackSummary] = (
Expand All @@ -281,14 +292,6 @@ def __init__(
self._default_auth = auth
self._version = version
self._json_serialize = json_serialize
if timeout is sentinel or timeout is None:
timeout = DEFAULT_TIMEOUT
if not isinstance(timeout, ClientTimeout):
raise ValueError(
f"timeout parameter cannot be of {type(timeout)} type, "
"please use 'timeout=ClientTimeout(...)'",
)
self._timeout = timeout
self._raise_for_status = raise_for_status
self._auto_decompress = auto_decompress
self._trust_env = trust_env
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 @@ -841,3 +841,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 5fd2946

Please sign in to comment.