Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing "is_alive" checks before acquiring the GIL in destructors. #894

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/nanobind/stl/function.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct pyfunc_wrapper {
}

~pyfunc_wrapper() {
if (f) {
if (f && is_alive()) {
gil_scoped_acquire acq;
Py_DECREF(f);
}
Expand Down
4 changes: 2 additions & 2 deletions src/error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ python_error::python_error() {
}

python_error::~python_error() {
if (m_value) {
if (m_value && detail::is_alive()) {
gil_scoped_acquire acq;
/* With GIL held */ {
// Clear error status in case the following executes Python code
Expand Down Expand Up @@ -73,7 +73,7 @@ python_error::python_error() {
}

python_error::~python_error() {
if (m_type) {
if (m_type && detail::is_alive()) {
gil_scoped_acquire acq;
/* With GIL held */ {
// Clear error status in case the following executes Python code
Expand Down
6 changes: 5 additions & 1 deletion src/nb_ndarray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,8 @@ static PyObject *dlpack_from_buffer_protocol(PyObject *o, bool ro) {
}

mt->deleter = [](managed_dltensor *mt2) {
if(! is_alive())
smurfix marked this conversation as resolved.
Show resolved Hide resolved
return;
gil_scoped_acquire guard;
Py_buffer *buf = (Py_buffer *) mt2->manager_ctx;
PyBuffer_Release(buf);
Expand Down Expand Up @@ -617,7 +619,7 @@ void ndarray_dec_ref(ndarray_handle *th) noexcept {

if (rc_value == 0) {
check(false, "ndarray_dec_ref(): reference count became negative!");
} else if (rc_value == 1) {
} else if (rc_value == 1 && is_alive()) {
gil_scoped_acquire guard;

Py_XDECREF(th->owner);
Expand Down Expand Up @@ -662,6 +664,8 @@ ndarray_handle *ndarray_create(void *value, size_t ndim, const size_t *shape_in,
scoped_pymalloc<int64_t> shape(ndim), strides(ndim);

auto deleter = [](managed_dltensor *mt) {
if (! is_alive())
smurfix marked this conversation as resolved.
Show resolved Hide resolved
return;
gil_scoped_acquire guard;
ndarray_handle *th = (ndarray_handle *) mt->manager_ctx;
ndarray_dec_ref(th);
Expand Down