fix: subinterpreter exception handling SEGFAULT#5795
Conversation
|
@b-pass could you please help reviewing this PR? |
That was the intent, yes.
The need to prevent allowing |
|
@b-pass I've updated the PR to remove all in-flight exception handling instead. |
|
Thanks @b-pass ! @justend29 Could you please review and maybe revise the PR description? (The Changes section seem to be out of date?) |
|
@henryiii I'm thinking the CIBW / iOS wheel macos-latest failure is unrelated. WDYT? |
|
Yes, see pypa/cibuildwheel#2556 (comment) |
Thanks @henryiii for confirming! |
|
@rwgk Done. Thanks for the diligence. |
Description
pybind11 version: b67d07e
The destructor of
subinterpreter_scoped_activatetests forstd::uncaught_exceptionsto throwstd::current_exception. These are two independent states that are unrelated to each other. As such, the destructor can easily SEGFAULT when throwing anullptrfromstd::current_exception(). The following example causes a SEGFAULT onstd::rethrow_exception, hereExplaining the issue:
The
subinterpreter_scoped_activateinsidescoped_subinterpreteris being destroyed from stack unwinding. Therefore,std::uncaught_exceptionsis 1, since this merely counts the number of in-flight exceptions. The current destructor would consequently try to throwstd::current_exception(). However,std::current_exception()returnsnullptr, as the destructor is not called from a catch block, wherestd::current_exception()returns the currently handled exception of the enclosing catch block - for which there is none.Changes
The original code intends to halt stack unwinding of any in-flight exception that happens to be a
py::error_already_set,mitigating the user error of letting
error_already_setexceptions escape an active subinterpreter. As halting stack unwinding without a surroundingtry-catchis not possible, the invalid checks were removed.Suggested changelog entry: