Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std.posix.kevent loop on EINTR #22253

Open
GuillaumeBibaut opened this issue Dec 16, 2024 · 2 comments
Open

std.posix.kevent loop on EINTR #22253

GuillaumeBibaut opened this issue Dec 16, 2024 · 2 comments
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@GuillaumeBibaut
Copy link

Zig Version

0.13.0

Steps to Reproduce and Observed Behavior

Hello,

I've been trying to have a discussion over what I'm thinking can be a problem while using the std.posix.kevent(...) function in the Zig Standard Library.
The reviewed code is the same in version 0.13.0 and master:
https://github.com/ziglang/zig/blob/master/lib/std/posix.zig#L4480

I'm about to start some C to Zig translation of some production C library to give us, our company, some future choice on whether we have to change our programing language. So I've been looking inside the Zig Standard Library to check what is done with kqueue and kevent and to know how much we'd have to adapt the C code.
The std.posix.kevent function code seems to be blocking with a while(true) loop and a continue on EINTR.
Our production C code is using this EINTR error, but if translating to Zig, we cannot do that check since that std.posix.kevent call is hiding that error inside the while(true) { ... continue .. } loop.
kqueue and kevent are functions part of kernel event notification mechanism, and they are made so that the developer using them are not blocked by using timeouts, or by using the error code to further process some other pieces of code not to block the greater scheme of these events.

I'm thinking this loop and continue are too low level at this point in the Zig Standard Library, and that it is not empowering the developer with enough choice.
I don't particularly know how to fix this, but it seems like an issue to have and while(true) at this point of the Zig code.

Some context:
I've been using FreeBSD, and some C library that's using kqueue and kevent. This library is part of some production code, and it's working totally fine. As a reference, we've been reading kqueue(2) manual to create that library that is proprietary code.
In this C library, we are using the kevent to check if we've got some TCP data to read from the sockets we're monitoring, and we are checking the kevent return to known how many sockets have got data awaiting to be read, and also we are checking errno to check if the error is not EINTR, and if it's not it has to stop and get out with an error propagated up. Our loops around these kevent calls are the farther from these calls so it gets us some time to do some other processing and not to be blocked.

Expected Behavior

To be able to catch the EINTR error up in the Zig code using std.posix.kevent maybe adding an error case in the KEventError, like Interrupted, so that the while(true) is removed from this function not to block the events mechanism.

@GuillaumeBibaut GuillaumeBibaut added the bug Observed behavior contradicts documented or intended behavior label Dec 16, 2024
@GuillaumeBibaut
Copy link
Author

The case I'm looking for might be just to use the std.c.kevent call instead to be able to take the appropriate actions in the context processing I've talked about.

@rohlem
Copy link
Contributor

rohlem commented Dec 16, 2024

Note that while std is compiler-provided, it's not special-cased in a lot of places.
If you know exactly what code you want to change / work around, you can always edit your copy of lib/std, or copy the necessary parts into your own project and modify them to better suit your needs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior
Projects
None yet
Development

No branches or pull requests

2 participants