Skip to content

Crash because of ownership assert in PyObject_ClearManagedDict on Free Threading #152254

Open

Description

@sobolevn

Crash report

Refs #151763
Original description: https://gist.github.com/devdanzin/99b84915b197ae0ade7face262b8af66

Repro:

from unittest.mock import MagicMock
from _testcapi import set_nomemory

set_nomemory(200)              # fail allocations from the 200th onward (lands in shutdown GC)
(MagicMock(), undefined_name)  # build a MagicMock (managed __dict__), then NameError abandons
                               # it into a traceback cycle -> survives to shutdown GC

I have a fix ready.

Linked PRs

Activity

  1. self-assigned this
    on Jun 26, 2026
  2. added
    type-crashA hard crash of the interpreter, possibly with a core dump
    on Jun 26, 2026
  3. changed the title [-]Crash because of ownership assert in `PyObject_ClearManagedDict`[/-] [+]Crash because of ownership assert in `PyObject_ClearManagedDict` on Free Threading[/+] on Nov 14, 2026
  4. vstinner commented on Nov 14, 2026

    @vstinner
    Member

    Ah, the reproducer requires Free Threading. Using Free Threading, I get:

    $ ./python repro.py 
    object address  : 0x2000181b890
    object refcount : 3
    object type     : 0x9ff7e0
    object type name: NameError
    object repr     : 
    lost sys.stderr
    python: Objects/dictobject.c:207: set_keys: Assertion `_Py_IsOwnedByCurrentThread((PyObject *)mp) || IS_DICT_SHARED(mp)' failed.
    Aborted                    (core dumped) ./python repro.py
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

interpreter-core(Objects, Python, Grammar, and Parser dirs)type-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