-
Notifications
You must be signed in to change notification settings - Fork 195
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
[BUG]: nb::ndarray
arguments with nb::call_guard<nb::gil_scoped_release>
on Python 3.12
#377
Comments
Hum, it's not obvious to me why this would not work. And I just tried (different platform: macOS/Python 3.11) and had no issues. Could I ask you to compile in debug mode and report a backtrace? |
Everything works fine for me on 3.11, and for all previous python versions (>=3.8). It seems to be 3.12-only issue. I will try the debug build in a sec. |
Mysterious. I don't really see what changed in 3.12 that could explain this difference. Except perhaps something related to the stable ABI, in case you are using that? (What's the filename of the compiled extension? Does it include the string |
Yes, # Build stable ABI wheels for CPython 3.12+
wheel.py-api = "cp312" Disabling it makes no difference. Running the test alone produces no backtrace. Just |
I ran the test with LLDB. Here is the call stack:
|
Hmm. When I use a reference ( Not an expert here, but shouldn't we create the arguments before releasing the lock? if constexpr (CheckGuard && !std::is_same_v<typename Info::call_guard, void>) {
return func_create<ReturnRef, false>(
[func = (forward_t<Func>) func](Args... args) NB_INLINE_LAMBDA {
typename Info::call_guard::type g;
(void) g;
return func((forward_t<Args>) args...);
},
(Return(*)(Args...)) nullptr, is, extra...);
} The guard is created before the arguments are passed to the function. If the function takes the |
I think I have guess as to what is wrong. Normally, passing around a I think the issue is that nanobind is now so aggressive in move-constructing arguments that the entire |
Never mind the previous patch I posted, it was not a good solution to the problem. Could you please check if the following addresses the issue? diff --git a/src/nb_ndarray.cpp b/src/nb_ndarray.cpp
index 6e74699..ced2343 100644
--- a/src/nb_ndarray.cpp
+++ b/src/nb_ndarray.cpp
@@ -537,6 +537,8 @@ 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) {
+ gil_scoped_acquire guard;
+
Py_XDECREF(th->owner);
Py_XDECREF(th->self);
managed_dltensor *mt = th->ndarray; |
Yes, both solutions fix the issue. |
Is this ndarray-only problem though? |
nb::ndarray
arguments with nb::call_guard<nb::gil_scoped_acquire>
on Python 3.12nb::ndarray
arguments with nb::call_guard<nb::gil_scoped_release>
on Python 3.12
I Appreciate the quick response btw 🤗 |
It's a problem for all types that require CPython API calls. That would be any wrapper type (e.g. |
… part of libroom. Related to wjakob/nanobind#377
* CI: removes python 3.7, adds 3.11 and 3.12 * switches to "build" package to build the package in CI. * fixes bdist_wheel build * Replaces the brittle crossing-based point-in-polygon (2D) algorithm by Dan Sunday's robust winding based algorithm. * Fixes a bug with releasing the GIL and python 3.12 in the rir builder part of libroom. Related to wjakob/nanobind#377
I had the exact same problem but with pybind11. Removing the |
- nb::any is replaced by `nb::ssize_t(-1)` - Because of a change in `~ndarray`, we need to make sure that Python is initialized in our tests (see wjakob/nanobind#377)
- nb::any is replaced by `nb::ssize_t(-1)` - Because of a change in `~ndarray`, we need to make sure that Python is initialized in our tests (see wjakob/nanobind#377)
Problem description
Hi there, many thanks for this amazing project!
On Python 3.12, calling functions with
nb::ndarray
arguments andnb::call_guard<nb::gil_scoped_release>
causes segmentation fault. This problem does not occur with previous versions of Python.When
nb::call_guard<nb::gil_scoped_release>
is not used and insteadnb::gil_scoped_release
is used directly inside the function, this error does not occur.System:
Reproducible example code
Here is a modified version of
nanobind_example
:And a test:
The text was updated successfully, but these errors were encountered: