You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Bug: exception in noexcept what() when Python exception contains a surrogate character #4288
Originally posted by TheShiftedBit October 26, 2022
By default, Python produces errors when converting encoding strs with utf-8 if the str contains surrogate characters. This can be disabled by passing surrogatepass as a second argument to .encode(). Pybind11 has this same behavior with its str -> std::string conversion. However, the bug is this: if an exception message contains a surrogate character, calling .what() on an error_already_set with such an exception causes another exception to be thrown, but since .what() is noexcept, that exception cannot be caught and the program std::terminates.
I'm not sure what the correct behavior regarding surrogate characters is. Perhaps pybind11 should always use surrogatepass, perhaps not. However, even if that's not the right choice, it should probably use it during exception handling, or Python exceptions like this are extremely difficult to diagnose.
Without having looked very closely here, it seems similar to what we were running into with the absl::Status type, which also reports error messages. We settled on this:
That approach could be adopted in error_already_set.
Although that throws error_already_set, which we don't want to do when we're already inside error_already_set I think. I'm not sure how to best error out if there is really no way to recover the error message (some kind of system error or undefined behavior, presumably).
@TheShiftedBit Could be a bit more specific on what type of exception is being thrown here and possible provide an minimal compilable example? Better yet, open a PR that adds a unit test which fails under the current framework.
Now, it's a bit odd we have explicit static conversion to std::string here. I guess because it is faster / simpler. We do have support for casting to the relevant std::string. Wouldn't just changing the exception type string to that type work well? Now, not sure exactly where it is triggered in our stack, but I think it might be at this conversion here:
Okay, there is a big perf issue here at least. We reinterpret_cast<object> the value object to an a py::object which means when we call caststd::string() it calls the str_caster in cast.h instead of our static_cast converter pytypes.h. (I see why you did it so early, because we need to turn the function into a forward decl otherwise, still might be useful though as that caster is a lot faster since we don't have to do any lookups onto the heap / check overloads etc...
You asked for a reproducer, I posted it on the other issue: #4287 (comment)
I temporarily fixed it by adapting your what() implementation. I roughly do this:
auto value_str = py::reinterpret_steal<py::object>(PyObject_Str(ex.value().ptr()));
py::bytes bytes = value_str.attr("encode").call("utf-8", "backslashreplace");
std::string result = bytes;
(This one doesn't have the backtrace, just the message). In my case, I don't care about recovering from an exception, I just want to end the program with as much detail as possible; so using backslashreplace is a good choice for me. For a more general implementation, you probably want surrogatepass or something.
I've attached a zip containing a Dockerfile with the full reproducer. I pinned every relevant version number I could find, including pinning pybind11 to the latest release.
test_exceptions.py::test_error_already_set_message_with_unicode_surrogate terminate called after throwing an instance of 'pybind11::cast_error'
what(): Unable to cast Python instance of type <class 'str'> to C++ type 'std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >'
Fatal Python error: Aborted
Current thread 0x00007f15b3d4a740 (most recent call first):
File "/usr/local/google/home/rwgk/forked/pybind11/tests/test_exceptions.py", line 280 in test_error_already_set_message_with_unicode_surrogate
File "/usr/local/lib/python3.10/dist-packages/_pytest/python.py", line 192 in pytest_pyfunc_call
I think it's an easy fix, but I need to play a bit.
Activity
rwgk commentedon Oct 26, 2022
Without having looked very closely here, it seems similar to what we were running into with the
absl::Statustype, which also reports error messages. We settled on this:https://github.com/pybind/pybind11_abseil/blob/ef7a738c6472f661aa3a77c228ff0b456fee3f2f/pybind11_abseil/utils_pybind11_absl.cc
That approach could be adopted in
error_already_set.Although that throws
error_already_set, which we don't want to do when we're already insideerror_already_setI think. I'm not sure how to best error out if there is really no way to recover the error message (some kind of system error or undefined behavior, presumably).Skylion007 commentedon Oct 27, 2022
@TheShiftedBit Could be a bit more specific on what type of exception is being thrown here and possible provide an minimal compilable example? Better yet, open a PR that adds a unit test which fails under the current framework.
There are a couple of potential solutions. Apparently, one is just returning unicode characters directly in what() per this StackOverflow exchange: https://stackoverflow.com/q/3760731/2444240 . Another option is escaping / replacing the error. I am trying to figure if the exception is coming.
I think I narrowed it down to this line:pybind11/include/pybind11/pytypes.h
Line 1481 in fcb5554
Now, it's a bit odd we have explicit static conversion to std::string here. I guess because it is faster / simpler. We do have support for casting to the relevant std::string. Wouldn't just changing the exception type string to that type work well? Now, not sure exactly where it is triggered in our stack, but I think it might be at this conversion here:pybind11/include/pybind11/pytypes.h
Line 508 in fcb5554
The static_cast fails and throws an exception because of the surrogate char though because of behavior introduced in Python 3.4:https://python.readthedocs.io/en/stable/whatsnew/3.4.html .Okay, there is a big perf issue here at least. We
reinterpret_cast<object>the value object to an a py::object which means when we call caststd::string() it calls the str_caster in cast.h instead of our static_cast converter pytypes.h. (I see why you did it so early, because we need to turn the function into a forward decl otherwise, still might be useful though as that caster is a lot faster since we don't have to do any lookups onto the heap / check overloads etc...Skylion007 commentedon Oct 27, 2022
Something like this could work as a hotfix, but is still inefficient.
Skylion007 commentedon Oct 28, 2022
On MacOS at least: I'm not getting an exception, I am just junk characters as the output. I suspect this may be an issue on Windows though.
TheShiftedBit commentedon Oct 28, 2022
You asked for a reproducer, I posted it on the other issue: #4287 (comment)
I temporarily fixed it by adapting your
what()implementation. I roughly do this:(This one doesn't have the backtrace, just the message). In my case, I don't care about recovering from an exception, I just want to end the program with as much detail as possible; so using
backslashreplaceis a good choice for me. For a more general implementation, you probably wantsurrogatepassor something.Skylion007 commentedon Oct 29, 2022
@TheShiftedBit Your repro doesn't seem to actually reproduce the issue, :( #4295
TheShiftedBit commentedon Oct 29, 2022
I've attached a zip containing a Dockerfile with the full reproducer. I pinned every relevant version number I could find, including pinning pybind11 to the latest release.
Run with:
pybind11_repro.zip
rwgk commentedon Oct 30, 2022
This reproduces the error:
I think it's an easy fix, but I need to play a bit.
rwgk commentedon Oct 30, 2022
@henryiii This would be a good one to get out ASAP. It's a bit nasty that it dies with
terminate.Fix & test for issue pybind#4288 (unicode surrogate character in Pyth…
fix: unicode surrogate character in Python exception message. (#4297)
fix: unicode surrogate character in Python exception message. (#4297)
fix: unicode surrogate character in Python exception message. (#4297)