Skip to content

Commit

Permalink
Patchback/backports/3.9/5fd29467fb63efdfae1ace280cec36b1f8139567/pr 8…
Browse files Browse the repository at this point in the history
…290 (#8311)
  • Loading branch information
NewGlad authored Apr 16, 2024
1 parent f21c6f2 commit 0415a4c
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 35 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
73 changes: 38 additions & 35 deletions aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,44 @@ 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 timeout is sentinel or timeout is None:
self._timeout = DEFAULT_TIMEOUT
if read_timeout is not sentinel:
warnings.warn(
"read_timeout is deprecated, " "use timeout argument instead",
DeprecationWarning,
stacklevel=2,
)
self._timeout = attr.evolve(self._timeout, total=read_timeout)
if conn_timeout is not None:
self._timeout = attr.evolve(self._timeout, connect=conn_timeout)
warnings.warn(
"conn_timeout is deprecated, " "use timeout argument instead",
DeprecationWarning,
stacklevel=2,
)
else:
if not isinstance(timeout, ClientTimeout):
raise ValueError(
f"timeout parameter cannot be of {type(timeout)} type, "
"please use 'timeout=ClientTimeout(...)'",
)
self._timeout = timeout
if read_timeout is not sentinel:
raise ValueError(
"read_timeout and timeout parameters "
"conflict, please setup "
"timeout.read"
)
if conn_timeout is not None:
raise ValueError(
"conn_timeout and timeout parameters "
"conflict, please setup "
"timeout.connect"
)
if loop is None:
if connector is not None:
loop = connector._loop
Expand Down Expand Up @@ -271,41 +309,6 @@ def __init__(
self._default_auth = auth
self._version = version
self._json_serialize = json_serialize
if timeout is sentinel or timeout is None:
self._timeout = DEFAULT_TIMEOUT
if read_timeout is not sentinel:
warnings.warn(
"read_timeout is deprecated, " "use timeout argument instead",
DeprecationWarning,
stacklevel=2,
)
self._timeout = attr.evolve(self._timeout, total=read_timeout)
if conn_timeout is not None:
self._timeout = attr.evolve(self._timeout, connect=conn_timeout)
warnings.warn(
"conn_timeout is deprecated, " "use timeout argument instead",
DeprecationWarning,
stacklevel=2,
)
else:
if not isinstance(timeout, ClientTimeout):
raise ValueError(
f"timeout parameter cannot be of {type(timeout)} type, "
"please use 'timeout=ClientTimeout(...)'",
)
self._timeout = timeout
if read_timeout is not sentinel:
raise ValueError(
"read_timeout and timeout parameters "
"conflict, please setup "
"timeout.read"
)
if conn_timeout is not None:
raise ValueError(
"conn_timeout and timeout parameters "
"conflict, please setup "
"timeout.connect"
)
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 @@ -885,3 +885,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 0415a4c

Please sign in to comment.