Skip to content

std.os.accept: add error Interrupted and allow user to manually retry - #16166

Closed
nektro wants to merge 3 commits into
ziglang:masterfrom
nektro:os-accept
Closed

nektro wants to merge 3 commits into
ziglang:masterfrom
nektro:os-accept

Conversation

@nektro

@nektro nektro commented Jun 23, 2023

Copy link
Copy Markdown
Contributor

when a signal such as SIGINT or SIGTERM is received by a program under the default circumstances, it will cause the program to terminate immediately.

if however a listener for these is added using signal(2) or sigaction(2) then it is possible to run a function as a result of the signal instead and syscalls currently blocking will exit with EINTR.

however under status quo the logic used by std.os.accept() and thus std.http.Server and any others, this error is undetectable and unable to be handled. now in any other case this errno result is perfectly resumable, but the consumer of std.os.accept() is likely already running a loop of their own that they can then continue through when getting this new error. most notably, is that it allows for a safe and proper exit. consider the following pseudo code

var running = true;

fn main() !void {
    var server = Server{ ... }
    try server.listen(...);

   signal(SIGINT, handleSignal);
   signal(SIGTERM, handleSignal);

   while (running) {
      const conn = server.accept() catch continue;
      handleConnection(conn);
   }
}

fn handleSignal() void {
    running = false;
}

@andrewrk
andrewrk self-requested a review June 24, 2023 00:55
@marler8997

Copy link
Copy Markdown
Contributor

The pseudo-code looks strange to me. Setting a boolean to exit the loop without popping the listener socket is odd, as it means the loop won't exit until a new connection is attempted, at which point the connection will be accepted/handled but then the loop willl immediately exit right afterwards.

I believe the convention here would be to call shutdown on the listener socket inside handleSignal to get the accept function to return immediately. With this change I think the program behaves as you would expect without the caller needing to handle EINTR?

@nektro

nektro commented Aug 2, 2023 •

Copy link
Copy Markdown
Contributor Author

closing of the socked is handled by std.http.Server.deinit() and so closing it in the signal handler would be a file double-free (and panic in the defer with EBADF)

EINT is the code for Interrupted and already causes accept(2) to return

@marler8997

marler8997 commented Aug 2, 2023 •

Copy link
Copy Markdown
Contributor

closing of the socked is handled by std.http.Server.deinit() and so closing it in the signal handler would be a file double-free (and panic in the defer with EBADF)

Right, that's why you call shutdown instead of close.

@nektro

nektro commented Aug 2, 2023

Copy link
Copy Markdown
Contributor Author

doing so in the example I provided would require hosting the server instance out from main into global scope which I would rather try and avoid, but I'll give it a shot and see how it behaves and report back

@nektro

nektro commented Aug 14, 2023

Copy link
Copy Markdown
Contributor Author

oh i missed that because I had to call std.os.shutdown(server_global.socket.sockfd.?, .both); rather than std.net.StreamServer.shutdown(). trying this now

@nektro

nektro commented Aug 14, 2023

Copy link
Copy Markdown
Contributor Author

yep that worked

@nektro nektro closed this Aug 14, 2023
@nektro
nektro deleted the os-accept branch August 14, 2023 06:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants