July 23, 2026, 10:18am 1
I would like to read from stdin, but only if the user actually wrote something.
I don’t really want to open another thread just for this so I tried a implementation from another ziggit post: Simple solution to read a byte with timeout
So at the end I wrote this code:
const resultOrError = io.operateTimeout(.{.file_read_streaming = .{
.data = &.{&readBuffer},
.file = .stdin(),
}}, .{ .duration = .{ .raw = .zero , .clock = .awake } }) catch |err| {
if (err == error.Timeout) return;
std.log.err("Error while reading from stdin: {t}", .{err});
return;
};
const result = resultOrError.file_read_streaming catch |err| {
std.log.err("Error while reading from stdin: {t}", .{err});
return;
};
if (result == 0) return;
... // do something with the input
This works on linux and I hoped that it would also work on windows, but when I tried it I got this error:
Error while reading from stdin: ConcurrencyUnavailable
This is because of this line in std/Io/Threaded.zig:
if (o.file.flags.nonblocking) {
...
} else {
if (concurrency) return error.ConcurrencyUnavailable;
...
I am not that deep into the everything going on with how windows etc handle the stdin, but is there a way for me to make this work on linux and on windows? Or can maybe someone point me to an issue I need to look out for, so I can just mark this as currently not possible on windows. (or maybe there is already a way on 0.17.0?)
(I do want to say that this is my first time trying to write such a program, so I may miss some implementation that would be better / would solve this)
Threaded uses synchronous IO on Windows. I don’t think it’s possible without writing your own platform specific code.
lalinsky July 23, 2026, 11:36am 3
Asynchronous (non-blocking) reading from stdin is a pretty hard problem to solve on Windows. The handle is not open as OVERLAPPED, and any other API might block. Honestly, the best solution is a separate thread that does blocking reads and posts results to an event/queue. It sucks, but I don’t know of a better solution.
0 Comments
Log in to join the conversation.No comments yet. Be the first to share your thoughts.