Skip to content

gc debug "collectables" print can deadlock in free threading #161366

Open

Description

@johng

Bug report

import gc
import sys
import threading

stop = threading.Event()

def writer():
    while not stop.is_set():
        sys.stderr.write("x\n")

t = threading.Thread(target=writer)
t.start()

gc.set_debug(gc.DEBUG_COLLECTABLE)
for _ in range(1000):
    a = []
    a.append(a)
    del a
    gc.collect()
gc.set_debug(0)

stop.set()
t.join()
print("done")

From initial investigation, during a stop the world for GC debug collectables is printed which can occur whilst the BufferedWriter lock is being held. Debug for uncollectables is instead called outside this critical section and does not have this deadlock pattern.

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions