Skip to content

Commit 28e9227

Browse files
authored
MAINT: Ensure raw dlpack deleter works when called without the GIL (numpy#22510)
* Ensure raw dlpack deleter works when called without the GIL * PR comments
1 parent b53ea93 commit 28e9227

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

numpy/core/src/multiarray/dlpack.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,24 @@
1414
static void
1515
array_dlpack_deleter(DLManagedTensor *self)
1616
{
17+
/*
18+
* Leak the pyobj if not initialized. This can happen if we are running
19+
* exit handlers that are destructing c++ objects with residual (owned)
20+
* PyObjects stored in them after the Python runtime has already been
21+
* terminated.
22+
*/
23+
if (!Py_IsInitialized()) {
24+
return;
25+
}
26+
27+
PyGILState_STATE state = PyGILState_Ensure();
28+
1729
PyArrayObject *array = (PyArrayObject *)self->manager_ctx;
1830
// This will also free the shape and strides as it's one allocation.
1931
PyMem_Free(self);
2032
Py_XDECREF(array);
33+
34+
PyGILState_Release(state);
2135
}
2236

2337
/* This is exactly as mandated by dlpack */

0 commit comments

Comments
 (0)