# Segfault when reading from serverside tcp connection

**URL:** <https://ziggit.dev/t/segfault-when-reading-from-serverside-tcp-connection/17618>\
**Category:** Help\
**Tags:** language\
**Created:** [September 15, 2026, 4:11pm UTC](https://ziggit.dev/t/segfault-when-reading-from-serverside-tcp-connection/17618 "2026-09-15T16:11:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![digital](https://ziggit.dev/user_avatar/ziggit.dev/digital/32/10163_2.png) [@digital](https://ziggit.dev/u/digital)\
**Post date:** [September 15, 2026, 4:11pm UTC](https://ziggit.dev/t/segfault-when-reading-from-serverside-tcp-connection/17618/1 "2026-09-15T16:11:48Z")

</div>

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.

This 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.

For 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:

```zig
const std = @import("std");
const Io = std.Io;
const dprint = std.debug.print;

pub fn main(init: std.process.Init) !void {
    // In order to do I/O operations need an `Io` instance.
    const io = init.io;

    const address = try std.Io.net.IpAddress.parse("::1", 1312);

    dprint("addr: {}\n", .{address});

    var listener = try address.listen(io, .{
        .reuse_address = true,
    });
    defer listener.deinit(io);

    var buffer: [1024]u8 = undefined;

    while (true) {
        const stream = try listener.accept(io);
        dprint("accepted connection from: {}\n", .{stream.socket.address});
        const reader = stream.reader(io, &buffer);
        var ri = reader.interface;

        if (ri.fillMore()) {
            dprint("possibly filled the buffer", .{});
        } else |err| {
            dprint("failed to fill the buffer because: {any}", .{err});
        }

        // if (ri.takeDelimiterInclusive('\n')) |line| {
        // dprint("line: {s}", .{line});
        // } else |err| {
        // dprint("error: {}", .{err});
        // }

        try stream.shutdown(io, .both);
    }
}

```

When run this code produces this output and segfaults when getting a connection (from netcat):

```plaintext
addr: .{ .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 } } }
accepted 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 } } }
Segmentation fault at address 0x79
/home/digital/zig-x86_64-linux-0.17.0-dev.1786+75044cb04/lib/std/Io.zig:554:29: 0x103e393 in operate (std.zig)
    return io.vtable.operate(io.userdata, operation);
                            ^
/home/digital/zig-x86_64-linux-0.17.0-dev.1786+75044cb04/lib/std/Io/net.zig:1275:31: 0x11f3118 in read (std.zig)
        return (try io.operate(.{ .net_read = .{
                              ^
/home/digital/zig-x86_64-linux-0.17.0-dev.1786+75044cb04/lib/std/Io/net.zig:1329:36: 0x11f2b85 in readVec (std.zig)
            const n = r.stream.read(io, dest) catch |err| {
                                   ^
/home/digital/zig-x86_64-linux-0.17.0-dev.1786+75044cb04/lib/std/Io/Reader.zig:1138:29: 0x108422b in fillMore (std.zig)
    _ = try r.vtable.readVec(r, &bufs);
                            ^
/home/digital/projects/comfy-aggregator/src/main.zig:26:24: 0x11e532f in main (main.zig)
        if (ri.fillMore()) {
                       ^
/home/digital/zig-x86_64-linux-0.17.0-dev.1786+75044cb04/lib/std/start.zig:797:30: 0x11e5f98 in callMain (std.zig)
    return wrapMain(root.main(.{
                             ^
/home/digital/zig-x86_64-linux-0.17.0-dev.1786+75044cb04/lib/std/start.zig:218:5: 0x11e4ca1 in _start (std.zig)
    asm volatile (switch (native_arch) {
    ^
fish: Job 1, './zig-out/bin/comfy_aggregator …' terminated by signal SIGABRT (Abort)

```

This one is using `zig 0.17.0-dev.1786+75044cb04` (because that’s what ziglings asked for).

Please help this one by teaching it how to find a solution for its problem or explain why this is failing.

---

<div class="post-metadata">

**Author:** ![xeondev](https://ziggit.dev/user_avatar/ziggit.dev/xeondev/32/9912_2.png) [@xeondev](https://ziggit.dev/u/xeondev)\
**Post date:** [September 15, 2026, 4:34pm UTC](https://ziggit.dev/t/segfault-when-reading-from-serverside-tcp-connection/17618/2 "2026-09-15T16:34:33Z")

</div>

Classic `@fieldParentPtr` failure.

```diff
- var ri = reader.interface;
+ const ri = &reader.interface;

```

You have to take a pointer to an `interface` field rather than copy its value when working with `Io.Reader`/`Io.Writer` implementors.

---

<div class="post-metadata">

**Author:** ![digital](https://ziggit.dev/user_avatar/ziggit.dev/digital/32/10163_2.png) [@digital](https://ziggit.dev/u/digital)\
**Post date:** [September 16, 2026, 1:40pm UTC](https://ziggit.dev/t/segfault-when-reading-from-serverside-tcp-connection/17618/3 "2026-09-16T13:40:26Z")

</div>

thank you, that solves it. is there anything specific this one could have noticed to realize that’s the problem?

---

<div class="post-metadata">

**Author:** ![lalinsky](https://ziggit.dev/user_avatar/ziggit.dev/lalinsky/32/3720_2.png) [@lalinsky](https://ziggit.dev/u/lalinsky)\
**Post date:** [September 16, 2026, 1:48pm UTC](https://ziggit.dev/t/segfault-when-reading-from-serverside-tcp-connection/17618/4 "2026-09-16T13:48:33Z")

</div>

The only way to know this is the problem would be to run a debugger, and find out what exactly is the code doing. Segmentation fault at low address like 0x79 is always suspicious, because it most likely represents a null pointer reference somewhere in the code. Running through valgrind is also a good option, it will likely point directly to the first invalid read.

---

<div class="post-metadata">

**Author:** ![Chaz](https://ziggit.dev/letter_avatar_proxy/v4/letter/c/4491bb/32.png) [@Chaz](https://ziggit.dev/u/Chaz)\
**Post date:** [September 16, 2026, 3:08pm UTC](https://ziggit.dev/t/segfault-when-reading-from-serverside-tcp-connection/17618/5 "2026-09-16T15:08:44Z")

</div>

I wonder. If you wrote the full type signature of the line that caused the problem it would be:

> [@](#):
>
> `var ri: std.Io.Reader = reader.interface;`

It’s fine to have a `*std.Io.Reader` but any `std.Io.Reader` is inherently going to break when it calls @fieldParentPtr().

Is there a way to make a linter check this? Even when implicitly typed?
