Skip to content

cProfile hangs / segfaults with 3.13t #125165

@andrewkho

Description

@andrewkho

Bug report

Bug description:

I'm testing Python 3.13t and trying to run cProfile in a context manager, however it seems to hang and/or segfault.

Below script repros the issue.

With > python -Xgil=1 test_cProfile.py profile
it succeeds as expected, however disabling -Xgil=0 causes it to usually hang.

""" example script showing deadlock with python 3.13t and cProfile context manager
python -Xgil=0 test_cProfile.py # runs normally
python -Xgil=0 test_cProfile.py profile # hangs
"""
import cProfile
import sys
import time
import threading
import queue
def worker(wid, q, stop):
    while not stop.is_set():
        try:
            x = q.get(timeout=1.)
            print(wid, x)
        except queue.Empty:
            continue
def run():
    stop = threading.Event()
    workers = []
    queues = []
    num_workers=2
    for wid in range(num_workers):
        queues.append(queue.Queue())
        workers.append(threading.Thread(target=worker, args=(wid, queues[-1], stop)))
        workers[-1].start()
    
    for i in range(100):
        queues[i%num_workers].put(i)
    while any(q.qsize() for q in queues):
        print("Waiting...")
        time.sleep(0.1)
        continue
    print("stop.set()")
    stop.set()
    for wid in range(num_workers):
        print("Joining", wid)
        workers[wid].join()
    
    print("Done")
def main():
    if len(sys.argv) > 1 and sys.argv[1] == "profile":
        print("Profiling with cProfile")
        with cProfile.Profile() as pr:
            run()
    else:
        print("Running without cProfile")
        run()
if __name__ == "__main__":
    main()
 commented on this paste.```


### CPython versions tested on:

3.13

### Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.13bugs and security fixes3.14bugs and security fixestopic-free-threadingtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions