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

Free-threading QSBR delayed free mem fails in high thread turnover environment #130794

Closed
tom-pytel opened this issue Mar 3, 2025 · 3 comments
Closed
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-free-threading type-crash A hard crash of the interpreter, possibly with a core dump

Comments

@tom-pytel
Copy link
Contributor

tom-pytel commented Mar 3, 2025

Crash report

What happened?

When cycling a lot of threads and modifying objects which use PyMem_FreeDelayed() in those threads the queues of memory blocks to free build up without ever being freed until the process runs out of memory, unless something else causes them to be freed. For example in the reproducer below commenting in the gc.collect() or the for loop will cause the delayed blocks to be freed correctly. Note that this is an extreme case.

Reproducer:

import gc
import threading


def fupmem(b, l):
    b.wait()
    l *= 2


def check(funcs, *args):
    barrier = threading.Barrier(len(funcs))
    thrds = []

    for func in funcs:
        thrd = threading.Thread(target=func, args=(barrier, *args))

        thrds.append(thrd)
        thrd.start()

    for thrd in thrds:
        thrd.join()


if __name__ == "__main__":
    count = 0

    while True:
        print(count := count + 1)

        check([fupmem] * 10, [None] * 256)

        # gc.collect()

        # for i in range(1000):
        #     l = []
        #     del l

CPython versions tested on:

3.14

Operating systems tested on:

Linux

Output from running 'python -VV' on the command line:

Python 3.14.0a5+ experimental free-threading build (heads/main:b3c18bfd828, Mar 3 2025, 09:13:46) [GCC 11.4.0]

Linked PRs

@tom-pytel tom-pytel added the type-crash A hard crash of the interpreter, possibly with a core dump label Mar 3, 2025
@tom-pytel
Copy link
Contributor Author

ping @colesbury

@colesbury
Copy link
Contributor

Thanks for the bug report. Typically, each thread periodically processes their queue of memory blocks to be freed. This happens every 254 calls to _PyMem_FreeDelayed(). The problems is that:

  1. Each thread only makes one call to _PyMem_FreeDelayed(), so they don't call _PyMem_ProcessDelayed.
  2. The thread then exits and "abandons" it's queue to the shared per-interpreter queue
  3. No thread processes the shared per-interpreter queue, because that happens in _PyMem_ProcessDelayed

The fix is relatively straightforward: when the thread exits and merges its queue into the shared per-interpreter queue, it should also take the opportunity to process the shared queue.

colesbury added a commit to colesbury/cpython that referenced this issue Mar 3, 2025
…yed.

This avoids a case where the interpreter's queue of memory to be freed
could grow rapidly if there are many short lived threads.
colesbury added a commit to colesbury/cpython that referenced this issue Mar 4, 2025
colesbury added a commit that referenced this issue Mar 4, 2025
…h-130808)

This avoids a case where the interpreter's queue of memory to be freed
could grow rapidly if there are many short lived threads.
colesbury added a commit to colesbury/cpython that referenced this issue Mar 4, 2025
…donDelayed. (pythongh-130808)

This avoids a case where the interpreter's queue of memory to be freed
could grow rapidly if there are many short lived threads.
(cherry picked from commit 2f6e0e9)

Co-authored-by: Sam Gross <colesbury@gmail.com>
colesbury added a commit that referenced this issue Mar 4, 2025
…ayed. (gh-130808) (#130857)

This avoids a case where the interpreter's queue of memory to be freed
could grow rapidly if there are many short lived threads.
(cherry picked from commit 2f6e0e9)
@kumaraditya303
Copy link
Contributor

Closing as it was fixed by #130808 and backported.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-free-threading type-crash A hard crash of the interpreter, possibly with a core dump
Projects
None yet
Development

No branches or pull requests

4 participants