Skip to content

handle impossible errors from the kernel with error.Unexpected instead of unreachable #6389

Closed

Description

@jorangreef

On Linux, e.g. in os.zig the std lib will often interrogate errno and map to Zig errors, or unreachable if the std lib wants to assert that the std lib implementation would never cause an EINVAL or EFAULT, e.g.:

EINVAL => unreachable,
EFAULT => unreachable,

However, while implementing #6356, I was about to follow this pattern but then I realized that the kernel often overloads errors in new kernel versions, which is particularly the case for the io_uring syscalls.

This means that we might think our std lib implementation cannot cause EINVAL, and then the kernel adds a new feature which could, leading to undefined behavior instead of a safe error.

In other words, we need to start going through the std lib and make this usage of unreachable an anti-pattern because there's no way we can assert what the kernel can or cannot be returning like this.

Activity

  1. changed the title [-]unsafe error handling in the std lib of overloaded kernel errors[/-] [+]std: unsafe error handling of overloaded kernel errors[/+] on Sep 21, 2020
  2. changed the title [-]std: unsafe error handling of overloaded kernel errors[/-] [+]std: unsafe handling of errors overloaded by the kernel[/+] on Sep 21, 2020
  3. added
    standard library
    on Sep 21, 2020
  4. jorangreef commented on Sep 21, 2020

    @jorangreef
    Author

    Either that, or we need an unreachable which doesn't become undefined behavior in fast builds, or another separate keyword. To be clear, I don't mean to suggest it's necessarily a good idea to add keywords...

  5. pfgithub commented on Sep 21, 2020

    @pfgithub
    Contributor

    Either that, or we need an unreachable which doesn't become undefined behavior in fast builds, or another separate keyword.

    I think @panic() does this

  6. Rocknest commented on Sep 21, 2020

    @Rocknest
    Contributor

    Do you have any example of a syscall that have changed its behaviour, except ioring stuff? If its specific to that feature, then do not use this pattern with these apis.

  7. jorangreef commented on Sep 21, 2020

    @jorangreef
    Author

    I haven't looked, but it's not about changing behavior so much as adding behavior, i.e. two error states (one old, one new) map to one error code, and Zig only checks one error state (because that was the only error state that existed at the time in the man pages) or assumes unreachable.

  8. rohlem commented on Sep 23, 2020

    @rohlem
    Contributor

    I don't know much about Linux; maybe I'm spouting nonsense, but couldn't the opposite, hypothetically, be true as well? If a syscall is "upgraded" to loosen its preconditions, then what was previously an error now succeeds. If the program relies on that logic, it breaks semantically (which is its own class of undefined behaviour).

    To me it seems like the actual solution would be to instead put an upper limit on the supported kernel version (so do a version check and @panic at run time if it is too high/new). While it's a shame supporting every new release would mean having to manually verify none of the syscall behaviour has changed, fwict that's the only reliable way to go about it.
    (Really there'd need to be a "compatibility profile" mode when these changes happen, like OpenGL has, though it's of course easier to just not change the interface. Assuming the latter, I too would be inclined to think this is an io_uring-specific growth pain / symptom, though again, I know nothing.)

  9. added
    proposal
    on Sep 23, 2020
  10. added this to the 0.8.0 milestone on Sep 23, 2020
  11. jorangreef commented on Sep 24, 2020

    @jorangreef
    Author

    but couldn't the opposite, hypothetically, be true as well? If a syscall is "upgraded" to loosen its preconditions, then what was previously an error now succeeds.

    Thanks @rohlem, for the interesting inverse example. That's certainly true, but I think at least it wouldn't lead to undefined behavior from a safety point of view? The program simply wouldn't receive an error from the kernel, so this kind of code branch wouldn't be reached.

    Unless, then again, it could be a safety issue if the program required the kernel to return an error, and fell back to unreachable if it did not.

    To me it seems like the actual solution would be to instead put an upper limit on the supported kernel version (so do a version check and @Panic at run time if it is too high/new). While it's a shame supporting every new release would mean having to manually verify none of the syscall behaviour has changed, fwict that's the only reliable way to go about it.

    Perhaps at first glance, but remember the kernel is free to backport features. Ultimately, checking kernel versions is a slippery slope and should be avoided in favor of relying on the error mechanism the kernel already exposes, albeit without using unreachable to assert what the kernel can/cannot return as an error.

  12. Rocknest commented on Sep 25, 2020

    @Rocknest
    Contributor

    Its expected that OSes have some backward compatibility, if the system has a breaking change thats not our problem (everything would break if for example read syscall suddenly changed). All documented errors should be handled and nothing more.

  13. modified the milestones: 0.8.0, 0.9.0 on May 19, 2021
  14. added
    accepted
    on Nov 23, 2021
  15. 33 remaining items

  16. alexrp commented on May 20, 2025

    @alexrp
    Member

    We discussed this in the meeting today and decided that we'll start by changing all => unreachable to => return error.Unexpected in std.posix and std.os.

    We'll then consider a follow-up enhancement: Add an std.Options field which enables panicking in code paths that, barring silly seccomp behavior and the like, always indicate programmer error.

  17. alexrp commented on Jun 4, 2025

    @alexrp
    Member

    We discussed this in the meeting today and decided that we'll start by changing all => unreachable to => return error.Unexpected in std.posix and std.os.

    Just noting that I'm postponing this until wrangle-writer-buffering is merged to avoid making Andrew's work on this branch even more of a slog than it already is.

  18. modified the milestones: urgent, 0.15.0 on Jun 26, 2025
  19. modified the milestones: 0.15.0, urgent on Jul 7, 2025
  20. assigned and unassigned on Jan 23, 2026
  21. removed their assignment
    on Jan 23, 2026
  22. alexrp commented on Apr 24, 2026

    @alexrp
    Member

    I believe this is basically done with std.Io.Threaded. Any further work can be follow-up issues.

  23. added
    enhancement
    and removed
    proposal
    accepted
    on Apr 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementstandard library

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions