Repository navigation
Closed
Labels
bugObserved behavior contradicts documented or intended behaviorstandard libraryThis issue involves writing Zig code for the standard library.
Milestone
Description
Zig Version
0.16.0-dev.70+73a0b5441
Steps to Reproduce and Observed Behavior
- create file
test.zigwith the following contents:
const std = @import("std");
// An unlimited stream of 0xaa bytes.
pub const AAStream = struct {
reader: std.Io.Reader,
pub fn init(
buffer: []u8,
) AAStream {
return AAStream{
.reader = .{
.vtable = &.{
.stream = AAStream.streamHexAA,
},
.buffer = buffer,
.seek = 0,
.end = 0,
},
};
}
fn streamHexAA(io_reader: *std.Io.Reader, w: *std.Io.Writer, limit: std.Io.Limit) std.Io.Reader.StreamError!usize {
_ = io_reader;
// Uncomment this line to see that this function is repeatedly called with limit 0.
// defer std.debug.print("stream function finished. called with limit: {?}\n", .{limit.toInt()});
const n_bytes_remaining = limit.minInt(2);
for (0..n_bytes_remaining) |_| {
try w.writeByte(0xaa);
}
return n_bytes_remaining;
}
};
test {
var aa_buffer: [1024]u8 = undefined;
var aa_stream = AAStream.init(&aa_buffer);
// apply a limit of 2 bytes
var buffer: [1024]u8 = undefined;
var limited = std.Io.Reader.limited(
&aa_stream.reader,
.limited(2),
&buffer,
);
const reader = &limited.interface;
// trigger EndOfStream
var out_buffer: [8]u8 = undefined;
try std.testing.expectError(error.EndOfStream, reader.readSliceAll(&out_buffer));
}- observe test hanging by running
zig test test.zig
Expected Behavior
Test passes.
Metadata
Metadata
Assignees
Labels
bugObserved behavior contradicts documented or intended behaviorstandard libraryThis issue involves writing Zig code for the standard library.
Activity
eestrada commentedon Sep 5, 2025
I think I may have hit this bug when I attempted to use a
Reader.Limitedinstance and then stream to a writer usingstreamRemaining. I ran it through a debugger and found that it just kept returning zero instead oferror.EndOfStream, so it stayed in an endless loop attempting to stream more bytes and just reporting that it streamed zero bites every time after it had streamed up to its limit.Here is what I did to work around it for the time being: skip using a
Reader.Limitedinstance altogether and just use thestreamExactfunction on the original reader instead. This may not work for all circumstances, but it was acceptable for mine.This was for zig stable version 0.15.1
Revert "Fix infinite loop in Reader.Limited"
andrewrk commentedon Oct 7, 2025
Fix reverted in b824ca8
whatisaphone commentedon Oct 9, 2025
I ran OP's code after #25512 (on 529aa9f) and it no longer hangs. This can be closed!
andrewrk commentedon Oct 9, 2025
Thanks for checking!