{"slug": "segfault-when-reading-from-serverside-tcp-connection", "title": "Segfault when reading from serverside tcp connection", "summary": "A Zig developer reports a segmentation fault at address 0x79 while reading from an accepted TCP connection using Zig 0.17.0-dev.1786+75044cb04, with the crash traced through std.Io.zig:554 in io.vtable.operate during a call to Reader.fillMore. The developer's first Zig project, a sensor-measurement collection server binding to ::1 port 1312, segfaults immediately after netcat connects and the code calls ri.fillMore(). The stack trace points to std/Io/net.zig:1275 in read and std/Io/Reader.zig:1138 in fillMore, and the developer is asking for help diagnosing the failure.", "body_md": "heyo everyone, this one is coming to zig in hopes of having fun with computers and other beings, learning cool programming stuff, and being not (less) bothered by llms and ai bs.\n\nThis one did some ziglings exercises to get into the language and has now started on its first zig project, a collection server for sensor measurements. The sensors will most likely talk with the server using json inside http inside (mutual) tls.\n\nFor now this one’s goal is to bind to a socket, to accept connections and read from them. This one is running into issues trying to read from a connection:\n\n``` js\nconst std = @import(\"std\");\nconst Io = std.Io;\nconst dprint = std.debug.print;\n\npub fn main(init: std.process.Init) !void {\n    // In order to do I/O operations need an `Io` instance.\n    const io = init.io;\n\n    const address = try std.Io.net.IpAddress.parse(\"::1\", 1312);\n\n    dprint(\"addr: {}\\n\", .{address});\n\n    var listener = try address.listen(io, .{\n        .reuse_address = true,\n    });\n    defer listener.deinit(io);\n\n    var buffer: [1024]u8 = undefined;\n\n    while (true) {\n        const stream = try listener.accept(io);\n        dprint(\"accepted connection from: {}\\n\", .{stream.socket.address});\n        const reader = stream.reader(io, &buffer);\n        var ri = reader.interface;\n\n        if (ri.fillMore()) {\n            dprint(\"possibly filled the buffer\", .{});\n        } else |err| {\n            dprint(\"failed to fill the buffer because: {any}\", .{err});\n        }\n\n        // if (ri.takeDelimiterInclusive('\\n')) |line| {\n        //     dprint(\"line: {s}\", .{line});\n        // } else |err| {\n        //     dprint(\"error: {}\", .{err});\n        // }\n\n        try stream.shutdown(io, .both);\n    }\n}\n```\n\nWhen run this code produces this output and segfaults when getting a connection (from netcat):\n\n```\naddr: .{ .ip6 = .{ .port = 1312, .bytes = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, .flow = 0, .interface = .{ .index = 0 } } }\naccepted connection from: .{ .ip6 = .{ .port = 40616, .bytes = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }, .flow = 0, .interface = .{ .index = 0 } } }\nSegmentation fault at address 0x79\n/home/digital/zig-x86_64-linux-0.17.0-dev.1786+75044cb04/lib/std/Io.zig:554:29: 0x103e393 in operate (std.zig)\n    return io.vtable.operate(io.userdata, operation);\n                            ^\n/home/digital/zig-x86_64-linux-0.17.0-dev.1786+75044cb04/lib/std/Io/net.zig:1275:31: 0x11f3118 in read (std.zig)\n        return (try io.operate(.{ .net_read = .{\n                              ^\n/home/digital/zig-x86_64-linux-0.17.0-dev.1786+75044cb04/lib/std/Io/net.zig:1329:36: 0x11f2b85 in readVec (std.zig)\n            const n = r.stream.read(io, dest) catch |err| {\n                                   ^\n/home/digital/zig-x86_64-linux-0.17.0-dev.1786+75044cb04/lib/std/Io/Reader.zig:1138:29: 0x108422b in fillMore (std.zig)\n    _ = try r.vtable.readVec(r, &bufs);\n                            ^\n/home/digital/projects/comfy-aggregator/src/main.zig:26:24: 0x11e532f in main (main.zig)\n        if (ri.fillMore()) {\n                       ^\n/home/digital/zig-x86_64-linux-0.17.0-dev.1786+75044cb04/lib/std/start.zig:797:30: 0x11e5f98 in callMain (std.zig)\n    return wrapMain(root.main(.{\n                             ^\n/home/digital/zig-x86_64-linux-0.17.0-dev.1786+75044cb04/lib/std/start.zig:218:5: 0x11e4ca1 in _start (std.zig)\n    asm volatile (switch (native_arch) {\n    ^\nfish: Job 1, './zig-out/bin/comfy_aggregator …' terminated by signal SIGABRT (Abort)\n```\n\nThis one is using `zig 0.17.0-dev.1786+75044cb04` (because that’s what ziglings asked for).\n\nPlease help this one by teaching it how to find a solution for its problem or explain why this is failing.", "url": "https://wpnews.pro/news/segfault-when-reading-from-serverside-tcp-connection", "canonical_source": "https://ziggit.dev/t/segfault-when-reading-from-serverside-tcp-connection/17618#post_1", "published_at": "2026-09-15 16:11:48+00:00", "updated_at": "2026-09-15 16:20:08.940590+00:00", "lang": "en", "topics": ["developer-tools"], "entities": ["Zig", "std.Io", "std.Io.net", "std.Io.Reader", "netcat", "ziglings"], "alternates": {"html": "https://wpnews.pro/news/segfault-when-reading-from-serverside-tcp-connection", "markdown": "https://wpnews.pro/news/segfault-when-reading-from-serverside-tcp-connection.md", "text": "https://wpnews.pro/news/segfault-when-reading-from-serverside-tcp-connection.txt", "jsonld": "https://wpnews.pro/news/segfault-when-reading-from-serverside-tcp-connection.jsonld"}}