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

Bug: Reference leak in _asyncio_Task___init___impl in _asynciomodule.c #126083

Closed
Nico-Posada opened this issue Oct 28, 2024 · 1 comment
Closed
Assignees
Labels
3.12 bugs and security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes easy extension-modules C modules in the Modules dir topic-asyncio type-bug An unexpected behavior, bug, or error

Comments

@Nico-Posada
Copy link
Contributor

Nico-Posada commented Oct 28, 2024

Bug report

Bug description:

I discovered this while working on #126080. If the func is given a context that isn't None, instead of doing Py_XSETREF it'll just do a regular assignment meaning that whatever was there before wont have its reference count decreased.

} else {
self->task_context = Py_NewRef(context);
}

import asyncio
import sys

# for convenience so I dont have to set up everything else for a Task obj
# and can just focus on the ref leak
class Break:
    def __str__(self):
        raise Exception("break")

async def coro():
    pass

task = asyncio.Task.__new__(asyncio.Task)
co = coro()

obj = object()
print("refcount before bug", sys.getrefcount(obj))

for _ in range(10000):
    try:
        task.__init__(co, context=obj, name=Break())
    except: pass

print("refcount after bug", sys.getrefcount(obj))

Output

refcount before bug 2
refcount after bug 10002

CPython versions tested on:

3.13

Operating systems tested on:

Linux

Linked PRs

@Nico-Posada Nico-Posada added the type-bug An unexpected behavior, bug, or error label Oct 28, 2024
@picnixz picnixz added topic-asyncio easy 3.12 bugs and security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes extension-modules C modules in the Modules dir labels Oct 28, 2024
@sobolevn
Copy link
Member

Yes, Py_XSETREF seems like a good solution here. @Nico-Posada, please send a PR :)

sobolevn pushed a commit that referenced this issue Oct 31, 2024
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Oct 31, 2024
…lizing with new non-`None` context (pythonGH-126103)

(cherry picked from commit d07dcce)

Co-authored-by: Nico-Posada <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Oct 31, 2024
…lizing with new non-`None` context (pythonGH-126103)

(cherry picked from commit d07dcce)

Co-authored-by: Nico-Posada <[email protected]>
sobolevn pushed a commit that referenced this issue Oct 31, 2024
…alizing with new non-`None` context (GH-126103) (#126229)

gh-126083: Fix a reference leak in `asyncio.Task` when reinitializing with new non-`None` context (GH-126103)
(cherry picked from commit d07dcce)

Co-authored-by: Nico-Posada <[email protected]>
sobolevn pushed a commit that referenced this issue Oct 31, 2024
…alizing with new non-`None` context (GH-126103) (#126230)

gh-126083: Fix a reference leak in `asyncio.Task` when reinitializing with new non-`None` context (GH-126103)
(cherry picked from commit d07dcce)

Co-authored-by: Nico-Posada <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 bugs and security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes easy extension-modules C modules in the Modules dir topic-asyncio type-bug An unexpected behavior, bug, or error
Projects
Status: Done
Development

No branches or pull requests

3 participants