Skip to content

Commit

Permalink
Fix #8253
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexey Nikitin committed Apr 7, 2024
1 parent 28f1fd8 commit 052ccb6
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 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
18 changes: 9 additions & 9 deletions aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,15 @@ def __init__(

if connector is None:
connector = TCPConnector()

if timeout is sentinel or timeout is None:
timeout = DEFAULT_TIMEOUT
if not isinstance(timeout, ClientTimeout):
self._connector = None
raise ValueError(
f"timeout parameter cannot be of {type(timeout)} type, "
"please use 'timeout=ClientTimeout(...)'",
)
self._timeout = timeout
# Initialize these three attrs before raising any exception,
# they are used in __del__
self._connector: Optional[BaseConnector] = connector
Expand All @@ -281,14 +289,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
10 changes: 10 additions & 0 deletions tests/test_client_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -841,3 +841,13 @@ 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(loop):
loop.set_debug(False)
logs = []
loop.set_exception_handler(lambda loop, ctx: logs.append(ctx))
with pytest.raises(ValueError, match="timeout parameter cannot be .*"):
ClientSession(timeout=1)
# should not have "Unclosed client session" warning
assert not logs

0 comments on commit 052ccb6

Please sign in to comment.