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

[python,aiohttp] Don't create persistent aiohttp.ClientSession in __init__ #20292

Merged
merged 1 commit into from
Dec 15, 2024

Conversation

dcermak
Copy link
Contributor

@dcermak dcermak commented Dec 11, 2024

aiohttp's ClientSession & TCPConnector used to obtain an event loop in init (via asyncio.get_event_loop). However, as of aio-libs/aiohttp#8512 both classes now obtain the running event loop and won't potentially create one. This makes it impossible to create ClientSession and TCPConnector objects outside of coroutines, as get_running_loop must be called from a coroutine.

Thus we defer the creation of a ClientSession into the actual request and cache it for later usage. Thereby we pay only a very small price on the first request, but subsequent requests will not be any more expensive.

This PR should fix a long standing issue in py-gitea-opensuse-org: https://github.com/dcermak/py-gitea-opensuse-org/actions/runs/12208693521/job/34062321907?pr=48 which broke with a recent release of aiohttp. I have tested this PR to regenererate py-gitea-opensuse-org and with this PR, the tests pass

PR checklist

  • Read the contribution guidelines.

  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.

  • Run the following to build the project and update samples:

    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)

  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

cc @cbornet @tomplus @krjakbrjak @fa0311 @multani

@dcermak dcermak force-pushed the python-aiohttp-no-event-loop branch from fbf31cf to 7d33482 Compare December 11, 2024 13:17
…nit__

aiohttp's `ClientSession` & `TCPConnector` used to obtain an event loop in
__init__ (via `asyncio.get_event_loop`). However, as of aio-libs/aiohttp#8512 both
classes now obtain the running event loop and won't potentially create one. This
makes it impossible to create `ClientSession` and `TCPConnector` objects outside
of coroutines, as `get_running_loop` must be called from a coroutine.

Thus we defer the creation of a `ClientSession` into the actual request and
cache it for later usage. Thereby we pay only a very small price on the first
request, but subsequent requests will not be any more expensive.
@dcermak dcermak force-pushed the python-aiohttp-no-event-loop branch from 7d33482 to 152f83a Compare December 11, 2024 13:33
@wing328
Copy link
Member

wing328 commented Dec 15, 2024

given that you've tested it in your use cases to confirm the fix, we can give it a try.

thanks for the PR

@wing328 wing328 merged commit cdfab4e into OpenAPITools:master Dec 15, 2024
26 checks passed
@wing328 wing328 added this to the 7.11.0 milestone Dec 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants