pyo3: `PanicException` doesn't inherit from base `Exception`
From https://github.com/pola-rs/polars/issues/5606
PanicException doesn’t inherit from the base python Exception, only from BaseException.
As used by libraries such as polars, PanicException is typically something comparable to ValueError, rather than something comparable to SystemExit or KeyboardInterrupt (built-ins which inherit from BaseException but not from Exception).
Using a library that relies on pyo3 in some application/service makes error handling problematic, as one most typically adds something like except Exception in order to handle errors gracefully while still responding to interrupt signals for example, but such code would not catch errors coming from pyo3-wrapped libraries.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 31 (20 by maintainers)
No, not in the majority of cases. As explained above, panics in Rust indicate logic errors and similarly unrecoverable failures that might have corrupted a library’s internal state and it usually is the best course of action to e.g. crash the service and let the supervisor layer restart it cleanly.
If the Python bindings of polars abuse panics to indicate invalid user input, then that is a bug in those bindings.
While I’ve been strongly in the BaseException camp before, I can see that this question is getting raised again and again, since panics-as-termination strongly goes against the Python feeling that “everything can be caught”.
And since PyO3 is mostly integrating Rust into Python, not the other way round, maybe it really is better to adapt to expectations?
Our argument that broken internal state should lead to termination is basically void, since after all it can be caught and will be caught anyway, just with more workarounds and grumbling.
Thanks for the discussion but I’m closing this again because I still believe inheriting from
BaseExceptionis the right thing to do.Discussion or solutions on making
PanicExceptioneasier to name/import/have-one-for-all-pyo3-modules is still welcome, but please make a new issue/PR for doing so.That is not my point. The server can go down whether or not you are catching
PanicException. For example:So the usability argument is moot. You still have to be ready for your server crashing. If anything, making it easy to catch
PanicExceptionis worse for usability, because it lulls you into the idea that you can just catch it and not have to worry about any of the above things.That’s not necessarily true; there have been many soundness bugs (including in the standard library) caused by code unwinding out of unsafe code without that unsafe code being ready for it. I have written such buggy unsafe code - it is a really difficult problem.
Moreover, safe code might not behave well after catching a panic. The panic might have poisoned a mutex or messed up an invariant somewhere, and any subsequent calls into that library might just panic again.
Personally I would not trust that if I caught a panic from an underlying rust library that it would continue to do what I want it to. There is really only one use case for catching panics; catching it, doing something (like logging) and resuming the panic.
As an aside, the “I have a web server that cannot crash no matter what” use case is not a good example. You have to account for your service going down either way. You cannot stop things like aborts/segfaults from bringing down your server.
You really should just let the panic do its thing and have a service for restarting your server if it goes down.
Agreed with what @birkenfeld has said above. A Rust panic really is intended to be treated as a (graceful) crash, not as a user-space error. It sounds from the polars thread that the maintainers there are willing to treat panics as bugs and fix them.
The point is that a panic isn’t a typical error condition (returning
Resultis). A Python user isn’t expected to interact with panics, the choice to not extend fromExceptionis deliberate so that panics are not accidentally caught.Will close this one, if there is significant pressure from the community we can revisit.
As a counter argument:
pyo3is used to create python libraries. Major python libraries (e.g. NumPy) never throwSystemExitanywhere in their code, regardless of how important an error is, or how exactly a user got into that error, or whether the error was caused by the user or by the library author.One would not expect to get such type of exceptions (e.g.
SystemExit,KeyboardInterrupt) out of a library, butpyo3leaves the possibility open to producing the currently-equivalentPanicExceptiondue to a combination of user error and library author error.A C++ library wrapped with cython for example would not throw any exception that’s not derived from
Exception, and one does not need to manually extend error catching conditions when using a cython-wrapped library, as one would have to do when including a pyo3-wrapped library.