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

"Unclosed client session" when initialization fails #8253

Closed
1 task done
unights opened this issue Mar 28, 2024 · 2 comments · Fixed by #8290
Closed
1 task done

"Unclosed client session" when initialization fails #8253

unights opened this issue Mar 28, 2024 · 2 comments · Fixed by #8290
Labels

Comments

@unights
Copy link

unights commented Mar 28, 2024

Describe the bug

This is actually a wrong use case.

Some parameters will be checked after the connection is established.

So the connection cannot be closed if there are incorrect parameters.

# -*- coding: utf-8 -*-
import asyncio

from aiohttp import ClientSession


async def main():
    async with ClientSession(timeout=1):
        pass


if __name__ == '__main__':
    asyncio.run(main())

How about deferring connection creation ( here ) ?

To Reproduce

as above

Expected behavior

First check that the parameters are correct and then establish the connection.

Logs/tracebacks

Traceback (most recent call last):
  File "D:\proj\test.py", line 13, in <module>
    asyncio.run(main())
  File "C:\Users\user\Miniconda3\Lib\asyncio\runners.py", line 190, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "C:\Users\user\Miniconda3\Lib\asyncio\runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user\Miniconda3\Lib\asyncio\base_events.py", line 653, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "D:\proj\test.py", line 8, in main
    async with ClientSession(timeout=1):
               ^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\proj\.venv\Lib\site-packages\aiohttp\client.py", line 292, in __init__
    raise ValueError(
ValueError: timeout parameter cannot be of <class 'int'> type, please use 'timeout=ClientTimeout(...)'
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x000001D74DC83CD0>

Python Version

$ python --version
Python 3.11.6

aiohttp Version

$ python -m pip show aiohttp
Name: aiohttp
Version: 3.9.3                                       
Summary: Async http client/server framework (asyncio)
Home-page: https://github.com/aio-libs/aiohttp       
Author:                                              
Author-email:
License: Apache 2
Location: D:\proj\.venv\Lib\site-packages
Requires: aiosignal, attrs, frozenlist, multidict, yarl
Required-by:

multidict Version

$ python -m pip show multidict
Name: multidict
Version: 6.0.5
Summary: multidict implementation
Home-page: https://github.com/aio-libs/multidict
Author: Andrew Svetlov
Author-email: andrew.svetlov@gmail.com
License: Apache 2
Location: D:\proj\.venv\Lib\site-packages
Requires:
Required-by: aiohttp, yarl

yarl Version

$ python -m pip show yarl
Name: yarl
Version: 1.9.4
Summary: Yet another URL library
Home-page: https://github.com/aio-libs/yarl
Author: Andrew Svetlov
Author-email: andrew.svetlov@gmail.com
License: Apache-2.0
Location: D:\proj\.venv\Lib\site-packages
Requires: idna, multidict
Required-by: aiohttp

OS

Windows 10

Related component

Client

Additional context

No response

Code of Conduct

  • I agree to follow the aio-libs Code of Conduct
@unights unights added the bug label Mar 28, 2024
@Dreamsorcerer
Copy link
Member

Feel free to make a PR and move the check earlier.

NewGlad pushed a commit to NewGlad/aiohttp that referenced this issue Apr 4, 2024
NewGlad pushed a commit to NewGlad/aiohttp that referenced this issue Apr 4, 2024
NewGlad pushed a commit to NewGlad/aiohttp that referenced this issue Apr 4, 2024
NewGlad pushed a commit to NewGlad/aiohttp that referenced this issue Apr 4, 2024
NewGlad pushed a commit to NewGlad/aiohttp that referenced this issue Apr 5, 2024
NewGlad pushed a commit to NewGlad/aiohttp that referenced this issue Apr 5, 2024
NewGlad pushed a commit to NewGlad/aiohttp that referenced this issue Apr 7, 2024
@Dreamsorcerer
Copy link
Member

Note that this solution doesn't help if you pass a connector in as an argument.

NewGlad added a commit to NewGlad/aiohttp that referenced this issue Apr 8, 2024
NewGlad added a commit to NewGlad/aiohttp that referenced this issue Apr 8, 2024
NewGlad added a commit to NewGlad/aiohttp that referenced this issue Apr 8, 2024
NewGlad added a commit to NewGlad/aiohttp that referenced this issue Apr 8, 2024
NewGlad added a commit to NewGlad/aiohttp that referenced this issue Apr 8, 2024
NewGlad added a commit to NewGlad/aiohttp that referenced this issue Apr 8, 2024
NewGlad added a commit to NewGlad/aiohttp that referenced this issue Apr 9, 2024
NewGlad added a commit to NewGlad/aiohttp that referenced this issue Apr 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants