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

_wrap_loop: Prevent redundant AsyncioEventLoop instances #1372

Merged
merged 2 commits into from
Aug 19, 2024

Conversation

zmedico
Copy link
Member

@zmedico zmedico commented Aug 18, 2024

Ultimately the loop arguments that necessitate the _wrap_loop function can be removed, because our aim since bug 761538 should be to eliminate them. Meanwhile, we don't want _wrap_loop to return redundant AsyncioEventLoop instances if we can easily prevent it.

Therefore, use _safe_loop(create=False) to look up the AsyncioEventLoop instance associated with the current thread, and avoid creating redundant instances. This serves to guard against garbage collection of AsyncioEventLoop instances which may have _coroutine_exithandlers added by the atexit_register function since commit cb0c09d from bug 937740.

If _safe_loop(create=False) fails to associate a loop with the current thread, raise an AssertionError for portage internal API consumers. It's not known whether external API consumers will trigger this case, so if it happens then emit a UserWarning and return a temporary AsyncioEventLoop instance.

Fixes: #1368 cb0c09d ("Support coroutine exitfuncs for non-main loops")
Bug: https://bugs.gentoo.org/938127
Bug: https://bugs.gentoo.org/937740
Bug: https://bugs.gentoo.org/761538

@zmedico zmedico marked this pull request as draft August 18, 2024 03:52
@zmedico zmedico force-pushed the bug_938127_wrap_loop_gc_fix branch 2 times, most recently from 8cc14d1 to 6f4eea6 Compare August 18, 2024 04:12
@zmedico zmedico marked this pull request as ready for review August 18, 2024 04:15
@zmedico zmedico force-pushed the bug_938127_wrap_loop_gc_fix branch 3 times, most recently from 4c7602c to 5201eb4 Compare August 18, 2024 15:39
Ultimately the loop arguments that necessitate the _wrap_loop
function can be removed, because our aim since bug 761538 should
be to eliminate them. Meanwhile, we don't want _wrap_loop to return
redundant AsyncioEventLoop instances if we can easily prevent it.

Therefore, use _safe_loop(create=False) to look up the AsyncioEventLoop
instance associated with the current thread, and avoid creating
redundant instances. This serves to guard against garbage collection
of AsyncioEventLoop instances which may have _coroutine_exithandlers
added by the atexit_register function since commit cb0c09d from
bug 937740.

If _safe_loop(create=False) fails to associate a loop with the current
thread, raise an AssertionError for portage internal API consumers.
It's not known whether external API consumers will trigger this case,
so if it happens then emit a UserWarning and return a temporary
AsyncioEventLoop instance.

Fixes: cb0c09d ("Support coroutine exitfuncs for non-main loops")
Bug: https://bugs.gentoo.org/938127
Bug: https://bugs.gentoo.org/937740
Bug: https://bugs.gentoo.org/761538
Signed-off-by: Zac Medico <zmedico@gentoo.org>
This class originated from commit 142d08c and it is unused
since _PortageEventLoop was removed in commit 20204fd.

Fixes: 20204fd ("Remove unused _PortageEventLoop and _PortageChildWatcher")
Signed-off-by: Zac Medico <zmedico@gentoo.org>
@zmedico zmedico force-pushed the bug_938127_wrap_loop_gc_fix branch from 5201eb4 to a62faf9 Compare August 18, 2024 15:47
@gentoo-bot gentoo-bot merged commit a62faf9 into gentoo:master Aug 19, 2024
13 checks passed
@zmedico zmedico deleted the bug_938127_wrap_loop_gc_fix branch August 19, 2024 14:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants