Commit Graph

259 Commits

Author SHA1 Message Date
Andrew Kelley 9009ab2495 std.Io.Threaded: make environ init non-optional
and argv0 on systems that need it too.

fixes surprising behavior for applications that forget to initialize the
environment field.
2026-01-04 00:27:08 -08:00
Andrew Kelley 69d07472a1 std lib tests passing on linux 2026-01-04 00:27:08 -08:00
Andrew Kelley 3e6d6150d9 std.process.Environ: fix compile errors on POSIX 2026-01-04 00:27:08 -08:00
Andrew Kelley 384bfc5f99 std.Progress: go through Io interface for parent IPC mechanism
and fix start code
2026-01-04 00:27:08 -08:00
Andrew Kelley 42988fc5f4 std.process.Environ.Block: enhance type safety 2026-01-04 00:27:08 -08:00
Andrew Kelley b85524d0c8 std.process.Environ: fix contains function 2026-01-04 00:27:08 -08:00
Andrew Kelley 32af0f6154 std: move child process APIs to std.Io
this gets the build runner compiling again on linux

this work is incomplete; it only moves code around so that environment
variables can be wrangled properly. a future commit will need to audit
the cancelation and error handling of this moved logic.
2026-01-04 00:27:08 -08:00
Andrew Kelley d6a1e73142 std: start wrangling environment variables and process args
this commit is unfinished. It marks a spot where I wanted to start
moving child process stuff below the std.Io.VTable
2026-01-04 00:27:07 -08:00
Matthew Lugg f306a9f84a std: rebase fixups and cancelation changes
This commit includes some API changes which I agreed with Andrew as a
follow-up to the recent `Io.Group` changes:

* `Io.Group.await` *does* propagate cancelation to group tasks; it then
  waits for them to complete, and *also* returns `error.Canceled`. The
  assertion that group tasks handle `error.Canceled` "correctly" means
  this behavior is loosely analagous to how awaiting a future works. The
  important thing is that the semantics of `Group.await` and
  `Future.await` are similar, and `error.Canceled` will always be
  visible to the caller (assuming correct API usage).

* `Io.Group.awaitUncancelable` is removed.

* `Future.await` calls `recancel` only if the "child" task (the future
  being awaited) did not acknowledge cancelation. If it did, then it is
  assumed that the future will propagate `error.Canceled` through
  `await` as needed.
2026-01-03 15:45:11 +00:00
Matthew Lugg b8a09bcbd9 std.Io.Group: tweak documentation and vtable API 2026-01-03 15:45:11 +00:00
Matthew Lugg 2c395e326f std.Io.Threaded: always use robust cancelation
As of this branch, the performance impact of robust cancelation is now
negligible (and in fact entirely unmeasurable in almost all cases), so
there is no good reason to not enable it in all cases. The performance
issues before were primarily down to a typo in the robust cancelation
logic which resulted in every canceled syscall potentially being sent
hundreds of signals in quick succession, because the delay between
signals started out at 1ns instead of 1us!
2026-01-03 15:45:10 +00:00
Matthew Lugg e57c557ad4 std.Io.Threaded: hugely improve Windows and NetBSD support
The most interesting thing here is the replacement of the pthread futex
implementation with an implementation based on thread park/unpark APIs.
Thread parking tends to be the primitive provided by systems which do
not have a futex primitive, such as NetBSD, so this implementation is
far more efficient than the pthread one. It is also useful on Windows,
where `RtlWaitOnAddress` is itself a userland implementation based on
thread park/unpark; we can implement it ourselves including support for
features which Windows' implementation lacks, such as cancelation and
waking a number of waiters with 1<n<infinity.

Compared to the pthread implementation, this thread-parking-based one
also supports full robust cancelation. Thread parking also turns out to
be useful for implementing `sleep`, so is now used for that on Windows
and NetBSD.

This commit also introduces proper cancelation support for most Windows
operations. The most notable omission right now is DNS lookups through
`GetAddrInfoEx`, just because they're a little more work due to having
a unique cancelation mechanism---but the machinery is all there, so I'll
finish gluing it together soon.

As of this commit, there are very few parts of `Io.Threaded` which do
not support full robust cancelation. The only ones which actually really
matter (because they could block for a prolonged period of time) are DNS
lookups on Windows (as discussed above) and futex waits on WASM.
2026-01-03 15:45:10 +00:00
Matthew Lugg a1d4120fd9 std.Io.Threaded: update to new internal syscall API 2026-01-03 15:45:10 +00:00
Matthew Lugg 0da5d5f150 std.Io.Threaded: rework cancelation and groups
The goal of this internal refactor is to fix some bugs in cancelation
and allow group tasks to clean up their own resources eagerly. The
latter will become a guarantee of the `std.Io` interface, which is
important so that groups can be used to "detach" tasks.

This commit changes the API which POSIX system calls use internally (the
functions formerly called `beginSyscall` etc), but does not update the
usage sites yet.
2026-01-03 15:45:07 +00:00
Andrew Kelley 1eb8fe7c61 Merge pull request 'bsd-futex' (#30626) from mikdusan/zig:bsd-futex into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/30626
Reviewed-by: Andrew Kelley <andrewrk@noreply.codeberg.org>
2025-12-30 20:24:00 +01:00
Andrew Kelley 2a02c7a0d5 std.Io.Group: async and concurrent support Cancelable results
Now, the return type of functions spawned with `Group.async` and
`Group.concurrent` may be anything that coerces to `Io.Cancelable!void`.

Before this commit, group tasks were the only exception to the rule
"error.Canceled should never be swallowed". Now, there is no exception,
and it is enforced with an assertion upon closure completion.

Finally, fixes a case of swallowing error.Canceled in the compiler,
solving a TODO.

There are three ways to handle `error.Canceled`. In order of most
common:
1. Propagate it
2. After receiving it, io.recancel() and then don't propagate it
3. Make it unreachable with io.swapCancelProtection()
2025-12-29 22:51:06 -08:00
Andrew Kelley 2adfd4d107 std.Io: fix and improve Group API
Rename `wait` to `await` to be consistent with Future API. The
convention here is that this set of functionality goes together:
* async/concurrent
* await/cancel

Also rename Select `wait` to `await` for the same reason.

`Group.await` now can return `error.Canceled`. Furthermore,
`Group.await` does not auto-propagate cancelation. Instead, users should
follow the pattern of `defer group.cancel(io);` after initialization,
and doing `try group.await(io);` at the end of the success path.
Advanced logic can choose to do something other than this pattern in the
event of cancelation.

Additionally, fixes a bug in `std.Io.Threaded` future await, in which it
swallowed an `error.Canceled`. Now if a task is canceled while awaiting
a future, after propagating the cancel request, it also recancels,
meaning that the awaiting task will properly detect its own cancelation
at the next cancelation point.

Furthermore, fixes a bug in the compiler where `error.Canceled` was
being swallowed in `dispatchPrelinkWork`.

Finally, fixes std.crypto code that inappropriately used
`catch unreachable` in response to cancelation without even so much as a
comment explaining why it was believed to be unreachable. Now, those
functions have `error.Canceled` in the error set and propagate
cancelation properly.

With this way of doing things, `Group.await` has a nice property: even if
all tasks in the group are CPU bound and without cancelation points, the
`Group.await` can still be canceled. In such case, the task that was
waiting for `await` wakes up with a chance to do some more resource
cleanup tasks, such as canceling more things, before entering the
deferred `Group.cancel` call at which point it has to suspend until the
canceled but uninterruptible CPU bound tasks complete.

closes #30601
2025-12-29 22:47:34 -08:00
Michael Dusan 3c851ec396 dragonfly: Io.Threaded: use futex 2025-12-29 22:29:48 -05:00
Michael Dusan ceb59b48b4 openbsd: Io.Threaded: use futex 2025-12-29 21:06:08 -05:00
Andrew Kelley f862762f09 std: fix Progress flickering
move some of the clearing logic from std.Io.Threaded to the Io interface
layer, thus allowing it to be skipped by advanced usage code via calling
the vtable functions directly.

then take advantage of this in std.Progress to avoid clearing the
terminal twice.

closes #30611
2025-12-29 12:49:43 -08:00
Lukas Lalinsky e8a2e6578a Add std.Io.net.Stream.shutdown 2025-12-29 02:20:37 +01:00
Michael Dusan b335e52ed6 openbsd: fixup nullz handling in Io.Threaded 2025-12-28 20:20:43 +01:00
Andrew Kelley 212968c574 std.Io.Threaded: handle missing atime from statx 2025-12-27 11:18:16 -08:00
Andrew Kelley 10e72a8cad std.Io.Threaded: implement fileSetTimestamps on WASI 2025-12-27 11:01:16 -08:00
Andrew Kelley dd98188ce0 std.Io.Threaded: mostly implement fileSetTimestamps for Windows
it's still missing the case of setting to now (which was also not
implemented before)
2025-12-27 10:52:17 -08:00
Andrew Kelley a7c9d11b28 std.Io: make file access time optional
Some filesystems, such as ZFS, do not report atime. It's pretty useless
in general, so make it an optional field in File.Stat.

Also take the opportunity to make setting timestamps API more flexible
and match the APIs widely available, which have UTIME_OMIT and UTIME_NOW
constants that can be independently set for both fields.

This is needed to handle smoothly the case when atime is null.
2025-12-27 10:40:24 -08:00
Andrew Kelley d8460910b4 std.Io.Threaded: init stderr_writer in streaming mode 2025-12-26 19:58:56 -08:00
Andrew Kelley f160d5f979 std.Io.Threaded: implement file writing for Windows 2025-12-26 19:58:56 -08:00
Ryan Liptak 58f0c8cfed Fix logic for detecting zero buffer capacity in fileReadStreamingWindows/fileReadPositionalWindows 2025-12-26 19:58:56 -08:00
Andrew Kelley eb29737d5d std.Io.Threaded: more efficient statx mask check 2025-12-26 19:58:56 -08:00
Andrew Kelley 6dcf951391 std.Io.Threaded: error instead of assert for statx
when statx fails to return some of the requested fields
2025-12-26 19:58:56 -08:00
Andrew Kelley 2e9415d8a0 std.Io.Threaded: fix missing @memcpy in realPathPosix for FreeBSD 2025-12-26 19:58:56 -08:00
Andrew Kelley 0dbcf97551 std.Io.Threaded: fix resource leak in dirRealPathFilePosix
it should unconditionally close the opened file descriptor, not only on
error.
2025-12-26 19:58:56 -08:00
Andrew Kelley dc1d27a646 std.Io.Threaded: more verbose TODO panic messages 2025-12-26 19:58:56 -08:00
Andrew Kelley 315bc10a0c std.Io.Threaded: dirRealPathFilePosix use libc realpath sometimes
When linking libc, and the directory is the cwd.

This avoids bugs on FreeBSD.
2025-12-24 12:57:18 -08:00
Andrew Kelley 6f4e93f0dd std.Io.Threaded: wasi does not support processExecutablePath 2025-12-24 00:55:27 -08:00
Andrew Kelley ace08ba642 std.Io.Threaded: prevent 0-byte file writes
these cause EINVAL on darwin and are generally a wasted syscall.

and remove a bogus error from File.Writer.Error.
2025-12-23 23:46:43 -08:00
Andrew Kelley a901e92413 std.Io.Threaded: implement process executable path on darwin 2025-12-23 23:31:47 -08:00
Andrew Kelley fa79d34674 std: add changing cur dir back
There's a good argument to not have this in the std lib but it's more
work to remove it than to leave it in, and this branch is already
20,000+ lines changed.
2025-12-23 22:15:12 -08:00
Andrew Kelley 98e9716c08 std: fix compilation on NetBSD 2025-12-23 22:15:12 -08:00
Andrew Kelley bd6acbf7da std.Io: minor cleanups to futex and event
mainly avoid an unnecessary `@ptrCast`
2025-12-23 22:15:12 -08:00
Andrew Kelley e9da2783ce std.Io.Threaded: add a workaround for self-hosted wasm backend
Tracked by #30575
2025-12-23 22:15:12 -08:00
Andrew Kelley c98157a3c9 std.Io.Threaded: implement environment variable scanning 2025-12-23 22:15:12 -08:00
Andrew Kelley 3c2f5adf41 std: integrate Io.Threaded with environment variables
* std.option allows overriding the debug Io instance
* if the default is used, start code initializes environ and argv0

also fix some places that needed recancel(), thanks mlugg!

See #30562
2025-12-23 22:15:12 -08:00
Andrew Kelley a29d79313a std.Io.Threaded: accept argv and environ on init
This is needed unfortunately for OpenBSD and Haiku for process
executable path.

I made it so that you can omit the options usually, but you get a
compile error if you omit the options on those targets.
2025-12-23 22:15:12 -08:00
Michael Dusan 60481b8223 std.Io: trim terminating NUL from exe path on BSDs 2025-12-23 22:15:12 -08:00
Andrew Kelley 1132e329d3 std.Io.Threaded: fix up processExecutablePath OpenBSD
* policy is to always handle EINTR from all syscalls
* assert the result of realpath
* don't ignore errors from realpath
* hard-code path separators for code simplicity
2025-12-23 22:15:11 -08:00
Michael Dusan 7014976d3d openbsd: avoid error.OperationUnsupported
- realPathPosix does not support OpenBSD
- change processExecutablePath OpenBSD section to use std.c.realpath
2025-12-23 22:15:11 -08:00
Michael Dusan 90bc371f56 openbsd: use Io.Dir.realPathFileAbsolute 2025-12-23 22:15:11 -08:00
Ryan Liptak bb788cd392 dirOpenFileWtf16: Disallow opening directories when requesting write permissions
This matches the behavior of POSIX
2025-12-23 22:15:11 -08:00