Skip to content

Commit 5ce6a82

Browse files
committed
Added exception translator specific mutex used with try_translate_exceptions
Fixes pybind#5346
1 parent 8a801bd commit 5ce6a82

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

include/pybind11/detail/exception_translation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ inline void try_translate_exceptions() {
5050
- delegate translation to the next translator by throwing a new type of exception.
5151
*/
5252

53-
bool handled = with_internals([&](internals &internals) {
53+
bool handled = with_internals_for_exception_translator([&](internals &internals) {
5454
auto &local_exception_translators = get_local_internals().registered_exception_translators;
5555
if (detail::apply_exception_translators(local_exception_translators)) {
5656
return true;

include/pybind11/detail/internals.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ static_assert(sizeof(instance_map_shard) % 64 == 0,
177177
struct internals {
178178
#ifdef Py_GIL_DISABLED
179179
pymutex mutex;
180+
pymutex exception_translator_mutex;
180181
#endif
181182
// std::type_index -> pybind11's type information
182183
type_map<type_info *> registered_types_cpp;
@@ -641,6 +642,15 @@ inline auto with_internals(const F &cb) -> decltype(cb(get_internals())) {
641642
return cb(internals);
642643
}
643644

645+
template <typename F>
646+
inline auto with_internals_for_exception_translator(const F &cb) -> decltype(cb(get_internals())) {
647+
auto &internals = get_internals();
648+
#ifdef Py_GIL_DISABLED
649+
std::unique_lock<pymutex> lock((internals).exception_translator_mutex);
650+
#endif
651+
return cb(internals);
652+
}
653+
644654
inline std::uint64_t mix64(std::uint64_t z) {
645655
// David Stafford's variant 13 of the MurmurHash3 finalizer popularized
646656
// by the SplitMix PRNG.

include/pybind11/pybind11.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2573,7 +2573,7 @@ void implicitly_convertible() {
25732573
}
25742574

25752575
inline void register_exception_translator(ExceptionTranslator &&translator) {
2576-
detail::with_internals([&](detail::internals &internals) {
2576+
detail::with_internals_for_exception_translator([&](detail::internals &internals) {
25772577
internals.registered_exception_translators.push_front(
25782578
std::forward<ExceptionTranslator>(translator));
25792579
});
@@ -2586,7 +2586,7 @@ inline void register_exception_translator(ExceptionTranslator &&translator) {
25862586
* the exception.
25872587
*/
25882588
inline void register_local_exception_translator(ExceptionTranslator &&translator) {
2589-
detail::with_internals([&](detail::internals &internals) {
2589+
detail::with_internals_for_exception_translator([&](detail::internals &internals) {
25902590
(void) internals;
25912591
detail::get_local_internals().registered_exception_translators.push_front(
25922592
std::forward<ExceptionTranslator>(translator));

0 commit comments

Comments
 (0)