Skip to content

hang inside std.Io.Reader.discardRemaining #24932

Closed

Description

@Mus-42

Zig Version

0.15.1

Steps to Reproduce and Observed Behavior

zig run following code:

const std = @import("std");

pub fn main() !void {
    var fixed: std.Io.Reader = .fixed("abc");
    var limited = fixed.limited(@enumFromInt(1), &.{});

    // hangs forever
    _ = try limited.interface.discardRemaining();
}

Expected Behavior

should exit loop after discarding all remaining data

Activity

  1. added
    bug
    on Aug 21, 2025
  2. Mus-42 commented on Aug 21, 2025

    @Mus-42
    Author

    it happens because on after first iteration it calls discard(0)
    after first iteration of discardRemaining loop:

    • limited.remaining is set to 0
    • Reader.Limited.discard computes combined_limit as 0, then calls discard(0) on fixed
    • Io.Reader.discard(0) can't return error.EndOfStream (0 always triggers early return) so loop never exits

    https://github.com/ziglang/zig/blob/cab6d752e8f6f6d3f538075840acc108238feda3/lib/std/Io/Reader.zig#L266..L275

    https://github.com/ziglang/zig/blob/cab6d752e8f6f6d3f538075840acc108238feda3/lib/std/Io/Reader/Limited.zig#L52..L58

    i guess solution would be to return error.EndOfStream inside Reader.Limited.discard when remaining <= limit

  3. added
    standard library
    on Oct 9, 2025
  4. modified the milestones: urgent, 0.15.2 on Oct 9, 2025
  5. whatisaphone commented on Oct 9, 2025

    @whatisaphone
    Contributor

    I ran OP's code after #25512 (on 529aa9f) and it no longer hangs. This can be closed!

  6. andrewrk commented on Oct 9, 2025

    @andrewrk
    Member

    Thanks for checking!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugstandard library

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions