Skip to content

BUG: _lib: crash in uarray on interpreter exit with a free-threaded interpreter #21214

@rgommers

Description

@rgommers

From @albanD's report in #20669 (comment):

Running with pydebug build as of two weeks ago and using ua_function, 1.15.0dev0 crashes on exit with

(null):0: _Py_NegativeRefcount: Assertion failed: object has negative ref count
<object at 0x2000e12e2b0 is freed>
Fatal Python error: _PyObject_AssertFailed: _PyObject_AssertFailed
Python runtime state: finalizing (tstate=0x00000000009bab80)

From @colesbury comment that 3.13t sets all module as being immortal, the explanation is that

/* m_free= */ globals_free};
never gets called and so the static string from
py_ref ua_function;
outlives the python runtime while still having a strong reference.
When we get to static variable de-alloc, we end up calling the py_ref destructor
~py_ref() { Py_XDECREF(obj_); }
(without taking the gil btw) while the python runtime is already dead (and in this case the string already properly de-allocated by the runtime) and thus we detect it to have negative refcount.
Since the m_free not being called is not going to be fixed, I guess the right fix is to have the static variable have an owning reference to the py_obj but explicitly leak it (never call decref on it).

This would be the same treatment being applied to PyTorch's static variables in pytorch/pytorch#130341 and other places like https://github.com/pytorch/pytorch/blob/0b134c15cdf8cb4c2c7ffed1c2b6518de44382bf/torch/csrc/autograd/python_hook.cpp#L105

Metadata

Metadata

Assignees

No one assigned

    Labels

    defectA clear bug or issue that prevents SciPy from being installed or used as expectedfree-threadingItems related to supporting free-threaded (a.k.a. "no-GIL") builds of CPythonscipy._libuarrayItems related to the uarray backend

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions