fix: don't call create_task in ClientSession.__del__ without a running loop - #7621
Open
noron12234 wants to merge 1 commit into
Open
fix: don't call create_task in ClientSession.__del__ without a running loop#7621noron12234 wants to merge 1 commit into
noron12234 wants to merge 1 commit into
Conversation
__del__ runs during garbage collection, which can happen with no running
event loop — a sync context, or interpreter shutdown. asyncio.create_task()
raises RuntimeError there, and exceptions raised in __del__ are swallowed
and printed rather than propagated, so the session was silently never
closed and the connection leaked. It also emits a "coroutine was never
awaited" warning.
>>> s = ClientSession(); del s # no running loop
RuntimeError: no running event loop
Now checks for a running loop first and falls back to the ResourceWarning
aiohttp itself emits in this exact situation, matching upstream behaviour.
When there is a loop, the close task is also retained: the loop only keeps
weak references, so a discarded close() can be collected before it finishes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The problem
ClientSession.__del__closes the session by scheduling a task:__del__runs during garbage collection, and that can happen with no running event loop — a sync context, or interpreter shutdown.asyncio.create_task()raisesRuntimeErrorthere:Exceptions raised inside
__del__are swallowed by Python and printed to stderr rather than propagated, so this does not crash anything — the session simply never closes and the connection leaks, with a stray warning as the only sign.There is a second, smaller issue on the path that does work: when a loop is running, the task is discarded, and the loop only keeps a weak reference, so the close can be collected before it finishes.
What aiohttp itself does
Worth noting because this class subclasses
aiohttp.ClientSession— upstream's__del__never schedules anything (aiohttp/client.py:427):It warns and hands off to the exception handler, precisely because
__del__cannot rely on a loop being available. The override here departs from that.The change
Check for a running loop first; fall back to the same
ResourceWarningupstream emits. When a loop is available, keep the close task referenced until it completes.Verification
Both paths, with the patched
__del__:RuntimeError: no running event loop+coroutine … was never awaitedResourceWarning: Unclosed client session— same as aiohttpNo test added: the no-loop path is straightforward to test, but the loop path is a garbage-collection race and a test for it would be flaky by construction. Happy to add a test for the
RuntimeErrorcase alone if you'd like one.Found with an AST scan for
create_task/ensure_futureresults discarded as bare expression statements. This was the only hit underopenbb_platform/.