Skip to content

std.Io: implement process vtable methods #366

Closed

Description

@lalinsky

The following std.Io vtable methods related to process management are currently unimplemented (@panic("TODO: ...")):

  • processExecutableOpen
  • processSpawn / processSpawnPath
  • processReplace / processReplacePath
  • childWait
  • childKill
  • progressParentFile

These need to be wired up to zio's runtime to support spawning and managing child processes via the std.Io interface.

Activity

  1. lalinsky commented on Apr 29, 2026

    @lalinsky
    OwnerAuthor

    @claude analyze if we could delegate process spawning to Io.Threaded and make pipe handles compatible with zio after, or if we need to port the entire code

  2. claude commented on Apr 29, 2026

    @claude

    Claude finished @lalinsky's task in 14m 25s —— View job


    Analysis: Delegating Process Spawning to Io.Threaded

    • Gather context: explore the codebase structure
    • Examine Io.Threaded implementation and vtable methods
    • Examine current process-related vtable stubs
    • Analyze pipe handle compatibility
    • Provide analysis and recommendation

    Findings

    std.Io.File is a raw fd wrapper

    File.handle is just posix.fd_t (an integer) on POSIX, or a HANDLE on Windows. The flags.nonblocking field records whether O_NONBLOCK / FILE_FLAG_OVERLAPPED is set. There is no other hidden state — the handle is fully portable across Io backends.

    What Io.Threaded.processSpawn does

    POSIX (spawnPosix, Threaded.zig ~14871):

    • Creates pipes with CLOEXEC only — no O_NONBLOCK:
      const pipe_flags: posix.O = .{ .CLOEXEC = true };
      
    • Reads the exec-error pipe synchronously after fork (a blocking read that is very fast in practice)
    • Returns Child with pipe handles marked nonblocking = false

    Windows (processSpawnWindows, Threaded.zig ~15457):

    • stdin pipe: SYNCHRONOUS_NONALERT mode → nonblocking = false (no FILE_FLAG_OVERLAPPED)
    • stdout/stderr pipes: ASYNCHRONOUS mode → nonblocking = true (FILE_FLAG_OVERLAPPED)

    This nonblocking mismatch is the central compatibility problem.

    Why zio's async backends require nonblocking fds

    Both epoll and poll backends handle error.WouldBlock by requeuing operations (lines ~570–716 in each backend). If a pipe fd has O_NONBLOCK unset, reads/writes block the thread instead of returning EAGAIN, deadlocking the async runtime. io_uring bypasses this (it can submit async I/O on blocking fds), but epoll/kqueue/poll cannot.

    Method-by-method verdict

    Method Threaded impl Delegate directly? Notes
    processExecutableOpen Full impl (Linux: /proc/self/exe, macOS: _NSGetExecutablePath, Windows: PEB) ✅ Yes Returns a regular file fd — no async needed
    progressParentFile Reads zig_progress_file from env ✅ Yes (w/ minor caveat) Returns a pipe fd; may need setNonblocking for epoll/poll backends
    processReplace Calls posixExecv (execve w/ PATH search) ✅ Yes Fire-and-forget execve, never returns on success
    processReplacePath Panics "TODO" in Threaded ❌ No Must implement from scratch
    processSpawnPath Panics "TODO" in Threaded ❌ No Must implement from scratch
    processSpawn Implemented but returns blocking pipes ⚠️ Needs fixup See below

    processSpawn: delegation + fixup is viable on POSIX

    After delegating to globalIo(), set O_NONBLOCK on the returned pipe fds:

    fn processSpawnImpl(_: ?*anyopaque, options: std.process.SpawnOptions) std.process.SpawnError!std.process.Child {
        const io = globalIo();
        var child = try io.vtable.processSpawn(io.userdata, options);
        if (builtin.os.tag != .windows) {
            if (child.stdin)  |*f| { try os.posix.setNonblocking(f.handle); f.flags.nonblocking = true; }
            if (child.stdout) |*f| { try os.posix.setNonblocking(f.handle); f.flags.nonblocking = true; }
            if (child.stderr) |*f| { try os.posix.setNonblocking(f.handle); f.flags.nonblocking = true; }
        }
        return child;
    }
    

    fcntl(F_SETFL, O_NONBLOCK) on a valid pipe fd cannot fail in practice. The fork+exec itself runs synchronously (microseconds), consistent with how zio already handles other blocking OS calls.

    processSpawn on Windows: fixup is not viable

    The FILE_FLAG_OVERLAPPED attribute on a pipe handle is set at creation time by NtCreateNamedPipeFile and cannot be changed afterwards. Threaded creates the stdin pipe without OVERLAPPED → it cannot be used with IOCP. The stdout/stderr pipes are already ASYNCHRONOUS (overlapped) so those are fine.

    To fix this properly on Windows, the spawn code would need to create the stdin pipe with ASYNCHRONOUS mode too, which requires porting (or wrapping) windowsCreatePipe.

    Recommendation

    Short path (POSIX only, covers most use cases):

    • processExecutableOpen, progressParentFile, processReplace → delegate to globalIo()
    • processSpawn → delegate then setNonblocking on returned pipe fds
    • processSpawnPath → delegate to globalIo() once argv is adjusted for the dir, then same fixup
    • processReplacePath → needs a chdir + exec or equivalent

    Full port (needed for correct Windows support and processSpawnPath):
    Use zio's os.fs.pipe() (which always creates nonblocking pipes on POSIX, and we'd need to create overlapped pipes on Windows) and port the fork+exec/CreateProcessW logic. The pure-logic parts (PATH search, argv/env serialization) in Threaded.zig can't be reused directly since they're tangled with Threaded's internal environ cache; they'd need to call std.process.getEnvMap or equivalent instead.

    The delegation-with-fixup approach is a good incremental step for the POSIX path. Windows will require porting the pipe creation regardless.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions