Commit Graph

392 Commits

Author SHA1 Message Date
Jacob Young 753e71e2f5 std.Io.Threaded: implement and cleanup windows codepaths 2026-01-31 23:45:52 -08:00
Andrew Kelley cc442d24ab std.Io: move fileWriteStreaming to Operation
This serves as an example to contributors of how to move VTable
functions to becoming an Operation, thereby enabling Batch API and
timeouts.
2026-01-31 22:53:28 -08:00
Andrew Kelley 3abc96a601 std.Io: add test for batchAwaitAsync
and make it always work for all targets including WASI

This function guarantees no additional failure modes introduced.
2026-02-01 02:49:36 +01:00
Andrew Kelley 2b19134c86 Merge pull request 'std.Io: introduce batching and operations API, satisfying the "poll" use case' (#30743) from poll into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/30743
2026-01-31 12:07:31 +01:00
Andrew Kelley b6f4bb91c4 std.Io: add documentation to Batch 2026-01-30 22:03:33 -08:00
Andrew Kelley 25aef0dd87 std.Io.Threaded: rework file reading to observe nonblocking flag
- batchAwaitAsync does blocking reads with NtReadFile (no APC, no event)
  when the nonblocking flag is unset, but still takes advantage of
  APCs when nonblocking flag is set.
- batchAwaitConcurrent returns error.ConcurrencyUnavailable when it
  encounters a file_read_streaming operation on a file in blocking mode.
- fileReadStreaming avoids pointlessly checking sync cancelation status
  when nonblocking flag is set, uses an APC with a done flag, and waits
  on that value to change in NtDelayExecution before returning.
- fix incorrect use of NtCancelIoFile (ntdll function prototype was
  wrong, leading to misuse)
2026-01-30 22:03:14 -08:00
Andrew Kelley 39a6d5d1c5 std.Io.File: add non-blocking flag
On Windows, we need to know ahead of time whether a file was opened in
synchronous mode or asynchronous mode. There may be advantages to
tracking this state for POSIX operating systems as well.
2026-01-30 22:03:14 -08:00
Andrew Kelley 62c97b745d std.Io.Threaded: stop checking bytes read with END_OF_FILE 2026-01-30 22:03:14 -08:00
Jacob Young 2674acdb77 Io.Batch: implement alternate API 2026-01-30 22:03:14 -08:00
Andrew Kelley 866ee4f1c5 std.Io.Threaded: handle TIMEOUT from NtDelayExceution 2026-01-30 22:03:14 -08:00
Matthew Lugg c2679feaaa std.Io.Threaded: fix ntdll timeouts on Windows 2026-01-30 22:03:14 -08:00
Andrew Kelley 8f8aa8346a std.Io.Threaded: ntReadFileResult handles EOF + bytes available 2026-01-30 22:03:14 -08:00
Andrew Kelley 4dd7fe90a2 std.Io.Threaded: compress ntReadFile logic
Just use the ntstatus field rather than an additional enum
2026-01-30 22:03:14 -08:00
Andrew Kelley 3320e6a1ae std.Io.Threaded.batchWait better fix for any_done
It is legal to call batchWait with already completed operations in the
ring. In such case, we need to avoid waiting in the syscall. The
any_done flag was a poor way of tracking state we already have: whether
the completion queue is empty.

This problem affects the posix poll implementation as well.

Thanks again to jacobly for finding the problem.
2026-01-30 22:03:14 -08:00
Andrew Kelley d770e14e00 std.Io.Threaded.batchWaitWindows: eager result sets any_done true
Thanks jacobly for finding the bug
2026-01-30 22:03:14 -08:00
Andrew Kelley 7a13d57916 std.Io.Threaded: add missing check for pending status in batchCancel 2026-01-30 22:03:14 -08:00
Andrew Kelley 8a80b54640 std: remove error.BrokenPipe from file reads, add error.EndOfStream
and make reading file streaming allowed to return 0 byte reads.
According to Microsoft documentation, on Windows it is possible to get
0-byte reads from pipes when 0-byte writes are made.
2026-01-30 22:03:14 -08:00
Andrew Kelley fdf1ee973e std.Io.Threaded: move the NtDelayExecution later in batchWait
also guard against receiving SUCCESS with 0 byte read

ms docs say that pipes can do this if there is a 0 byte write
2026-01-30 22:03:14 -08:00
Andrew Kelley 2fb224cb84 std.Io.Threaded: fix bad use of AlertableSyscall
The defer would cause two problems:
1. keeping the state active during call to NtCancelIoFile
2. invalid state transition. after canceled is returned from
   checkCancel, new status is already canceled. calling finish after
   that is illegal.
2026-01-30 22:03:14 -08:00
Andrew Kelley 523aa213c9 std.Io.Threaded: batchWait and batchCancel for Windows 2026-01-30 22:03:14 -08:00
Andrew Kelley 37316a3cf6 std.Io.Threaded: resolve merge conflicts 2026-01-30 22:03:14 -08:00
Andrew Kelley 15ca46d1e7 std.Io.Threaded: fix compilation error on some systems 2026-01-30 22:03:14 -08:00
Andrew Kelley a4d438562d std.Io.Threaded: fix compilation failures on Windows
it's still broken as hell tho
2026-01-30 22:03:14 -08:00
Andrew Kelley 9134430387 std.Io.Threaded: fix batchWait impl 2026-01-30 22:03:14 -08:00
Andrew Kelley 68a34df025 std.Io.Threaded: fix error set 2026-01-30 22:03:14 -08:00
Andrew Kelley dd0153b91b std.Io.operate: fix bogus catch
this used to have a different error set. just goes to show you how
useful switching on error set is even when there is only 1 prong
2026-01-30 22:03:14 -08:00
Andrew Kelley a0c2645948 std.Io.Threaded: delete dead code 2026-01-30 22:03:14 -08:00
Jacob Young 8146ccfecc Io: add ring to Batch API 2026-01-30 22:03:14 -08:00
Andrew Kelley 0a379513af std.Io.Threaded: super broken Windows impl of batch
this is a cry for help
2026-01-30 22:03:14 -08:00
Andrew Kelley 642f329ac9 std.Io: exploring a different batch API proposal 2026-01-30 22:03:14 -08:00
Andrew Kelley e0d06b40e3 std.Io.Threaded: set poll_buffer_len to 32
reasoning is that polling with large amount of operations will be rarely
done with std.Io.Threaded. However this still provides the opportunity
to provide concurrency for any real world use cases that need it.
2026-01-30 22:03:13 -08:00
Andrew Kelley 6a7fe61d74 std.Io.Threaded.operate: handle poll buffer exceeded 2026-01-30 22:03:13 -08:00
Andrew Kelley 93f5c99149 std.Io.Threaded.operate: handle cancelation and poll errors 2026-01-30 22:03:13 -08:00
Andrew Kelley 05064e1281 std.Io: simplify operate function
- no timeout
- no n_wait
- infallible
2026-01-30 22:03:13 -08:00
Andrew Kelley 0a0ecc4fb1 std.Io: proof-of-concept "operations" API
This commit shows a proof-of-concept direction for std.Io.VTable to go,
which is to have general support for batching, timeouts, and
non-blocking.

I'm not sure if this is a good idea or not so I'm putting it up for
scrutiny.

This commit introduces `std.Io.operate`, `std.Io.Operation`, and
implements it experimentally for `FileReadStreaming`.

In `std.Io.Threaded`, the implementation is based on poll().

This commit shows how it can be used in `std.process.run` to collect
both stdout and stderr in a single-threaded program using
`std.Threaded.Io`.

It also demonstrates how to upgrade code that was previously using
`std.Io.poll` (*not* integrated with the interface!) using concurrency.
This may not be ideal since it makes the build runner no longer support
single-threaded mode. There is still a needed abstraction for
conveniently reading multiple File streams concurrently without
io.concurrent, but this commit demonstrates that such an API can be
built on top of the new `std.Io.operate` functionality.
2026-01-30 22:03:13 -08:00
Jacob Young 90890fcb5c Io.Threaded: fix UAF-induced crashes during asynchronous operations
When `NtReadFile` returns `SUCCESS`, the APC routine still runs when
next alertable, which was previously clobbering an out of scope `done`.
Instead of adding an extra syscall to the success path, avoid all APC
side effects, allowing instant completions to return immediately.
2026-01-30 22:03:13 -08:00
Andrew Kelley 9862518797 std.Io.Threaded: fix NtDelayExecution delay interval 2026-01-30 22:03:13 -08:00
Andrew Kelley 11b0a504df std.Io.Threaded: handle some more error codes from NtReadFile 2026-01-30 22:03:13 -08:00
Andrew Kelley 6d9e6e2c38 std.Io.Threaded: avoid extra fields of Thread
As mlugg pointed out those race when a thread finishes an operation just
after it is canceled and then that thread to picks up another task,
resulting in these fields being potentially overwritten.

This updates fileReadStreaming on Windows to handle being alerted, and
then manage its own cancelation of the file I/O.
2026-01-30 22:03:13 -08:00
Andrew Kelley a933d7a6f8 std.Io.Threaded: don't pass null to NtDelayExecution
Windows returns ACCESS_VIOLATION if you do that.
2026-01-30 22:03:13 -08:00
Andrew Kelley e705ad8302 std.Io.Threaded: implement APC cancelation
specifically the call to NtCancelIoFileEx
2026-01-30 22:03:13 -08:00
Andrew Kelley 1e3072ec46 std.Io.Threaded: introduce Thread.InterruptMethod
implements APC cancelation except for the actual call to NtCancelIoFileEx
2026-01-30 22:03:13 -08:00
Andrew Kelley 5580257596 std.Io.Threaded: add some temporary, choice panics 2026-01-30 22:03:13 -08:00
Andrew Kelley 8827488fcd std: back out the flags field of Io.File
For now, let us refrain from putting the sync mode into the Io.File
struct, and document that to do concurrent batch operations, any Windows
file handles must be in asynchronous mode. The consequences for
violating this requirement is neither illegal behavior, nor an error,
but that concurrency is lost. In other words, deadlock might occur. This
prevents the addition of flags field.

partial revert of 2faf14200f58ee72ec3a13e894d765f59e6483a9
2026-01-30 22:03:13 -08:00
Andrew Kelley cb7be96644 std.Io: give File a nonblocking bit on Windows
This tracks whether it is a file opened in synchronous mode, or
something that supports APC.

This will be needed in order to know whether concurrent batch operations
on the file should return error.ConcurrencyUnavailable, or use APC to
complete the batch.

This patch also switches to using NtCreateFile directly in
std.Io.Threaded for dirCreateFile, as well as NtReadFile for
fileReadStreaming, making it handle files opened in synchronous mode as
well as files opened in asynchronous mode.
2026-01-30 22:03:13 -08:00
Alex Rønne Petersen cbe38f771c std.Io.Threaded: consider EOPNOTSUPP to be programmer error in createFileMap()
Not doing so was hiding bugs (e.g. on s390x-linux).
2026-01-31 00:08:34 +01:00
Andrew Kelley 6a3226c43c std.Io: add net.Socket.createPair
and remove the following from std.posix:
- socketpair
- fcntl
2026-01-30 02:07:26 -08:00
rpkak b9819fce69 Io.Threaded: limit copy_file_range len to prevent EOVERFLOW 2026-01-30 08:19:50 +01:00
Andrew Kelley 377bb8f237 Merge pull request 'std: migrate getcwd to Io' (#31047) from getcwd into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/31047
2026-01-30 06:38:08 +01:00
Brandon Black ecb9ddf267 Threaded.sleepPosix: fix libc error handling
Confusingly, the POSIX spec for clock_nanosleep() says it returns
*positive* error values directly and does not touch `errno`. Not
detecting EINTR properly here was breaking the cancellation of
threads blocked in this call when linking libc.
2026-01-30 06:19:31 +01:00