Skip to content

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

Closed

Description

@Nico-Posada

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

Activity

  1. sobolevn commented on Oct 28, 2024

    @sobolevn
    Member

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

  2. added a commit that references this issue on Oct 31, 2024
  3. added 2 commits that reference this issue on Oct 31, 2024
  4. added 2 commits that reference this issue on Oct 31, 2024
  5. moved this from Todo to Done in asyncioon Oct 31, 2024
  6. added a commit that references this issue on Dec 8, 2024
  7. added a commit that references this issue on Jan 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

3.12only security fixes3.13only security fixes3.14bugs and security fixeseasyextension-modulesC modules in the Modules dirtopic-asynciotype-bugAn unexpected behavior, bug, or error

Projects

  • Status
    Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions