ClientSession teardown runs after event loop closes
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 598
- Forks
- 168
- Avg merge
- 1d 4h
- Merged PRs (30d)
- 86
Description
Description
I often see an error in the logs about trying to close a session after the event loop is already closed:
[pulp] | Error in atexit._run_exitfuncs:
[pulp] | Traceback (most recent call last):
[pulp] | File "/src/pulpcore/pulpcore/download/factory.py", line 90, in _session_cleanup
[pulp] | asyncio.get_event_loop().run_until_complete(self._session.close())
[pulp] | File "/usr/lib64/python3.9/asyncio/base_events.py", line 622, in run_until_complete
[pulp] | self._check_closed()
[pulp] | File "/usr/lib64/python3.9/asyncio/base_events.py", line 515, in _check_closed
[pulp] | raise RuntimeError('Event loop is closed')
[pulp] | RuntimeError: Event loop is closed
[pulp] | sys:1: RuntimeWarning: coroutine 'ClientSession.close' was never awaited
[pulp] | RuntimeWarning: Enable tracemalloc to get the object allocation traceback
[pulp] | pulp [None]: asyncio:ERROR: Unclosed client session
[pulp] | client_session: <aiohttp.client.ClientSession object at 0x7fcf40486eb0>
I'm not sure if that brings any immediate negative impact for the application, but I would say that is at least risky to not handle sessions lifetime correctly.
Expectation
I expect that we correctly handle the lifetime of our ClientSessions.
Reproduce
I don't have a reproducer right now.
It often happens when doing things on the content app code.
More context
What I know:
- In the context of the content app, the
ClientSessionsare created, shared and reused for a given Remote instance (via itsdownload_factory'singleton' property). - At the logs say, currently we are trying to cleanup the session on the atexit hook, when the interpreter is existing (too late)
- "The session should be closed before the loop closing." - from aiohttp maintainer
What I dont know:
- If we can just safely ignore this forever
- How to correctly handle these sessions lifetimes
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in pulpcore/download/factory.py at _session_cleanup around line 90, then inspect the Remote download_factory property in pulpcore/app/models/repository.py around line 581. Trace how ClientSessions are created, reused, and cleaned up relative to the event loop. Done means session cleanup occurs before loop shutdown without atexit errors, unclosed-session warnings, or unawaited-close warnings.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend, networking
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100