Skip to content

std.net.StreamServer.accept cannot handle SO_(SND|RCV)TIMEO sockopts #7142

Open
@stef

Description

@stef

i'm running into this line in my code: https://github.com/ziglang/zig/blob/master/lib/std/net.zig#L1701 - accept() getting a WouldBlock error, since i did not set std.io.is_async, but what i did was to do a setsockopt for SO_(SND|RCV)TIMEO which sets a timeout on the socket...

i don't really want to set the socket to nonblocking. i just want to regularly return if there is no incoming connection, but i'm ok to have a few seconds timeouts on that...

i lifted the code from 0.6.0 into my own sources and modified it to look like this:

pub fn accept(self: *net.StreamServer) !net.StreamServer.Connection {
    const accept_flags = os.SOCK_CLOEXEC;
    var accepted_addr: net.Address = undefined;
    var adr_len: os.socklen_t = @sizeOf(net.Address);
    if (os.accept4(self.sockfd.?, &accepted_addr.any, &adr_len, accept_flags)) |fd| {
        return net.StreamServer.Connection{
            .file = fs.File{
                .handle = fd,
                .io_mode = std.io.mode,
            },
            .address = accepted_addr,
        };
    } else |err| return err;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    standard libraryThis issue involves writing Zig code for the standard library.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions