Skip to content

GC segfault with free threading #141965

@kevmo314

Description

@kevmo314

Crash report

What happened?

This code segfaults for me

import threading
import gc
import torch

def main():
    def worker(wid):
        for i in range(100):
            t = torch.zeros(1000)
            cond = threading.Condition()
            if i % 10 == 0:
                gc.collect()
        print(f"W{wid} done")
    threads = [threading.Thread(target=worker, args=(i,)) for i in range(40)]
    for t in threads: t.start()
    for t in threads: t.join()
    print("  PASSED")

if __name__ == "__main__":
    main()

I am unclear why. I haven't been able to reproduce it without pytorch and even stranger is that the threading.Condition() is not used. It leads me to think there's a race in the garbage collection process and the tensor + Condition happen to trigger some specific states but I haven't been able to characterize that yet.

CPython versions tested on:

3.14

Operating systems tested on:

Linux

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

Python 3.14.0 free-threading build (main, Nov 19 2025, 22:48:09) [Clang 21.1.4 ]

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.14bugs and security fixes3.15new features, bugs and security fixestopic-free-threadingtype-crashA hard crash of the interpreter, possibly with a core dump

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions