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

fix: Replace bare static exception<T> with gil_safe_call_once_and_store. #4897

Merged
merged 1 commit into from
Oct 23, 2023
Merged
Changes from all commits
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
20 changes: 6 additions & 14 deletions include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "detail/init.h"
#include "attr.h"
#include "gil.h"
#include "gil_safe_call_once.h"
#include "options.h"
#include "typing.h"

Expand Down Expand Up @@ -2609,23 +2610,14 @@ class exception : public object {
};

PYBIND11_NAMESPACE_BEGIN(detail)
// Returns a reference to a function-local static exception object used in the simple
// register_exception approach below. (It would be simpler to have the static local variable
// directly in register_exception, but that makes clang <3.5 segfault - issue #1349).
template <typename CppException>
exception<CppException> &get_exception_object() {
static exception<CppException> ex;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this default constructor call into CPython (and release/reacquire the GIL)?

The deadlock scenario can only arise if there's some non-trivial lock interaction in the initializer of the static variable.

I suppose this change would also deal with some end-of-program lifetime situations, though, e.g. if this object gets destroyed too early or too late.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this default constructor call into CPython (and release/reacquire the GIL)?

No, it just copies nullptr to m_ptr (

PyObject *m_ptr = nullptr;
).

The deadlock scenario can only arise if there's some non-trivial lock interaction in the initializer of the static variable.

Yes, that was on my mind.

I suppose this change would also deal with some end-of-program lifetime situations, though, e.g. if this object gets destroyed too early or too late.

That was the only reason I got into here.

I considered simply changing this to use new (with a boilerplate "intentional leak" comment), but then decided: the universally safe solution

  1. is easy,
  2. inexpensive,
  3. expressive (call once is exactly what we want),
  4. reads nicely,
  5. future proof (in case the exception<T> implementation is changed),
  6. and a great pattern for people to remember and follow,

let's use it!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, makes sense! Using the new facility just for the lifetime is certainly also a good move.

return ex;
}

// Helper function for register_exception and register_local_exception
template <typename CppException>
exception<CppException> &
register_exception_impl(handle scope, const char *name, handle base, bool isLocal) {
auto &ex = detail::get_exception_object<CppException>();
if (!ex) {
ex = exception<CppException>(scope, name, base);
}
PYBIND11_CONSTINIT static gil_safe_call_once_and_store<exception<CppException>> exc_storage;
exc_storage.call_once_and_store_result(
[&]() { return exception<CppException>(scope, name, base); });

auto register_func
= isLocal ? &register_local_exception_translator : &register_exception_translator;
Expand All @@ -2637,10 +2629,10 @@ register_exception_impl(handle scope, const char *name, handle base, bool isLoca
try {
std::rethrow_exception(p);
} catch (const CppException &e) {
set_error(detail::get_exception_object<CppException>(), e.what());
set_error(exc_storage.get_stored(), e.what());
}
});
return ex;
return exc_storage.get_stored();
}

PYBIND11_NAMESPACE_END(detail)
Expand Down