Repository navigation
Map out-of-range raw OS errors to EINVAL on linux_raw - #1712
stepancheg wants to merge 1 commit into
Conversation
67bbf08 to
594e8c9
Compare
|
Applied suggestions. |
|
I think we should not care about kernel's bug at userspace. |
In practice, we already use rustix/src/backend/libc/termios/syscalls.rs Lines 378 to 383 in a1c13cc |
|
Panicking can result in outages like the one in Cloudflare https://blog.cloudflare.com/18-november-2025-outage/ |
|
To be honest, I'm not sure mapping this to |
|
I think panicking on an unexpected error is more appropriate than silently mapping to a different error. |
|
@stepancheg Could you elaborate on why this is an issue in your use case? |
|
There are no known issues, it is something I noticed while considering migration from nix to rustix (similar function in nix does not panic). Our project is large, many servers, a lot of code, much of it is low level (virtualization, FUSE, userspace block devices, etc). There are bugs in our project code (as in any large private project), yet we have strict no panicking policy: code is not allowed to panic unless invariant is local and can be easily verified. Crashing the server is expensive (in particular, graceful shut down of the service takes hours). We must not crash. So far we did not have any panics in production: some code path breaks, error is logged, diagnostics collected, maybe alert is raised, other operations continue. At least for our case, graceful handling of errors is strictly better than panicking: we see some strange behavior, we investigate at our own pace. Instead of waking up in the middle of the night to deal with cascading failure of the service. Possible alternative to this PR is having something like I would argue that it does not buy much: a shared library author should still do |
|
Just found: Linux kernel similarly does not panic on invalid errno. I guess their motivation is similar to mine: |
|
I suppose we could add a non-panicking pub const unsafe fn from_raw_os_error_unchecked(raw: i32) -> ErrnoHowever, this would shift the responsibility for ensuring that To be honest, if the possibility of a panic is the main concern, consumers should probably just validate the raw value before passing it to |
Right, the Linux kernel similarly maps out-of-range raw values to |
You are not suggesting for
Let me update the PR with |
|
Yeah, I was suggesting that |
`Errno::from_raw_os_error` asserted on values outside Linux's `1..4096` range. Return `EINVAL` instead. This also matches the libc backend, whose `from_raw_os_error` does not panic. Error handling usually runs when something is already broken, and it is not the default code path, so it tends to be less exercised. A bad `errno` can come from various places: an incorrectly implemented syscall, or a syscall wrapper that returns a stray value. Mapping those to `EINVAL` keeps the error observable without taking the process down. Assisted-by: Cursor:auto
594e8c9 to
5977c21
Compare
|
Updated the PR to |
Errno::from_raw_os_errorasserted on values outside Linux's1..4096range. ReturnErrno::INVALinstead. This also matches the libc backend, whosefrom_raw_os_errordoes not panic.Error handling usually runs when something is already broken, and it is not the default code path, so it tends to be less exercised. A bad
errnocan come from various places: an incorrectly implemented syscall, or a syscall wrapper that returns a stray value. Mapping those toEINVALkeeps the error observable without taking the process down.Assisted-by: Cursor:auto