← 输入框|github.com|刷新
Skip to content

Interpreter shutdown can deadlock when a daemon thread holds the warnings lock #159485

Open

Description

@ngoldbaum

Originally reported by @krassowski in #158973.

A daemon thread can be stopped while holding the warnings lock. During interpreter finalization, garbage collection triggers a ResourceWarning, deadlocking the finalizing thread:

import threading
import time
import warnings

ready = threading.Event()
warnings.simplefilter("always", ResourceWarning)

class WarnAtShutdown:
    def __init__(self):
        self.self_ref = self  # Defer __del__ until shutdown GC.

    def __del__(self):
        warnings.warn("late", ResourceWarning)

def showwarning(message, *args, **kwargs):
    if str(message) == "hold":
        ready.set()
        time.sleep(0.1)

warnings.showwarning = showwarning
threading.Thread(
    target=lambda: warnings.warn("hold", ResourceWarning),
    daemon=True,
).start()
assert ready.wait(4)
WarnAtShutdown()

Expected: the process exits normally. Actual: it hangs during interpreter shutdown.

  • CPython versions tested on: 3.14.8, 3.15.0.
  • Operating systems tested on: macOS.

Linked PRs

Activity

  1. self-assigned this
    on Oct 15, 2026
  2. added
    type-bug
    interpreter-core
    3.14
    3.15
    3.16
    on Oct 15, 2026
  3. added a commit that references this issue on Oct 15, 2026
  4. ngoldbaum commented on Oct 16, 2026

    @ngoldbaum
    ContributorAuthor

    I updated the reproducer script in the issue to avoid an unbounded wait (it now just does time.sleep(0.1) like test_threading does). This is more realistic and is fixed by @ZeroIntensity's InterpreterGuard approach. There's no need to guard against waiting forever in a callback like my original reproducer was arguing for. There's still a deadlock even without that and the InterpreterGuard fixes it.

  5. added 2 commits that reference this issue on Oct 16, 2026
  6. daneeller commented on Nov 22, 2026

    @daneeller

    Version-scope data point: the reproducer hangs on 3.14.5 Windows but exits normally on 3.13.14.

    I ran the script from the issue (with the original 0.5 s hold) under an external 20 s timeout on two GIL builds:

    • 3.13.14 (MSC v.1944, 64-bit Windows): printed the final message and exited normally, EXIT_CODE=0.
    • 3.14.5 (MSC v.1944, 64-bit Windows): printed reached end of module; interpreter should now finalize, then never exited; killed at 20 s, EXIT_CODE=124.

    The issue labels already list 3.14/3.15/3.16, so this is consistent — but the explicit 3.13 negative result may be useful when deciding where to backport the InterpreterGuard fix. It confirms the regression boundary is between 3.13 and 3.14 rather than being a long-standing issue.

    This is relevant in practice for applications with many daemon threads: a desktop app with 20+ daemon workers and an atexit shutdown hook will occasionally leave a process alive after the window closes, which is easy to misdiagnose as a UI-framework shutdown bug.

    No change requested — just adding the Windows data point and the 3.13 control result.

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

Metadata

Metadata

Assignees

Labels

3.143.153.16interpreter-coretype-bug

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions