Skip to content

std.Io: implement childWait and childKill - #368

Merged
lalinsky merged 2 commits into
mainfrom
process-child-wait-kill
Apr 29, 2026
Merged

lalinsky merged 2 commits into
mainfrom
process-child-wait-kill

Conversation

@lalinsky

Copy link
Copy Markdown
Owner

Part of #366.

Summary

  • Adds src/process.zig with childWait and childKill implementations backed by ev.ProcessWait
  • childWait uses waitForIo for cancellation-aware async waiting; converts ExitStatus to std.process.Child.Term and closes stdio pipes
  • childKill sends SIGTERM (POSIX) / NtTerminateProcess (Windows), then reaps the process uncancelably via waitForIoUncancelable
  • Wires both into io.zig's vtable, replacing the @panic("TODO") stubs
  • Initializes std.testing.io_instance in the test runner so tests can spawn processes via std.testing.io

Notes

The io_uring backend uses IORING_OP_WAITID (requires Linux 6.7+) and will return error.Unexpected on older kernels; tests pass with the epoll backend.

Test plan

  • ./check.sh --filter "child" --backend epoll

Part of #366.

Also initialize std.testing.io_instance in the test runner so tests
can use std.process.spawn via std.testing.io.
@coderabbitai

coderabbitai Bot commented Apr 29, 2026 •

Copy link
Copy Markdown
Contributor

Warning

Rate limit exceeded

@lalinsky has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 43 minutes and 47 seconds before requesting another review.

To keep reviews running without waiting, you can enable usage-based add-on for your organization. This allows additional reviews beyond the hourly cap. Account admins can enable it under billing.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: b9c73d29-0664-4ae9-a4d6-3da106eb0693

📥 Commits

Reviewing files that changed from the base of the PR and between ea9edbc and 1443082.

📒 Files selected for processing (1)
  • src/process.zig
📝 Walkthrough

Walkthrough

The PR implements concrete process child wait and kill operations. Previously stubbed methods in the std.Io vtable now delegate to a new process utility layer (src/process.zig) that handles process termination, status collection, and resource cleanup. Test runner gains per-test IO instance initialization.

Changes

Cohort / File(s) Summary
Process implementation
src/io.zig, src/process.zig
Replaces panic stubs with real childWait and childKill implementations. New functions use event-driven ProcessWait, handle exit-to-term conversion, translate wait errors, and clean up resources. Includes platform-specific termination (NtTerminateProcess on Windows, SIGTERM elsewhere).
Test infrastructure
test_runner.zig
Adds per-test IO instance initialization and deinitialization alongside existing allocator leak detection.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related issues

Possibly related PRs

Poem

🌱 Where stubs once panicked, logic blooms,
Child processes wait their destined rooms,
Events and signals now find their way,
Resource cleanup saves the day. ⚙️

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: implementing childWait and childKill functions in the std.Io vtable.
Description check ✅ Passed The description is directly related to the changeset, covering the new process.zig implementations, vtable integration, and test runner changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch process-child-wait-kill

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
Review rate limit: 0/1 reviews remaining, refill in 43 minutes and 47 seconds.

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Nitpick comments (2)
src/io.zig (1)

1763-1765: Optional cleanup: redundant compile-anchor test.

process_impl is already referenced by childWaitImpl/childKillImpl, so this test block can be dropped unless you want it as explicit documentation.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/io.zig` around lines 1763 - 1765, The small test block that anchors
process_impl for compilation is redundant because process_impl is already
referenced by childWaitImpl and childKillImpl; remove the test block containing
"test { _ = process_impl; }" to clean up the code, leaving process_impl
referenced only via childWaitImpl and childKillImpl (no other code changes
required).
src/process.zig (1)

31-35: Consume/check ProcessWait result in childKill to honor reap semantics.

Right now childKill waits for completion but never inspects op.getResult(). That hides wait failures and makes “reap uncancelably” harder to trust/debug.

Suggested fix
 pub fn childKill(child: *std.process.Child) void {
     sendTermSignal(child.id.?);
     var op = ev.ProcessWait.init(child.id.?);
     waitForIoUncancelable(&op.c);
+    _ = op.getResult() catch {};
     childCleanup(child);
 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/process.zig` around lines 31 - 35, childKill currently calls
ev.ProcessWait.init and waitForIoUncancelable but never inspects op.getResult(),
which hides wait failures; update childKill to call op.getResult() after
waitForIoUncancelable (or use the returned Result/!Error path) and handle errors
appropriately before calling childCleanup—e.g., propagate or log the error from
op.getResult() so wait failures are not ignored; ensure you still call
childCleanup in success and handle the case where op.getResult() indicates a
failure from ProcessWait/sendTermSignal.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/process.zig`:
- Around line 20-24: The switch handling the error from op.getResult() currently
maps error.Canceled to error.Canceled but must map backend/IO-level
cancellations to error.Unexpected (user cancellation is already handled by
waitForIo); update the switch in the op.getResult() catch branch to return
error.Unexpected for error.Canceled (alongside other backend errors like
ProcessNotFound and SystemResources) so IO-level cancellation isn't surfaced as
a user cancel.
- Around line 73-104: These tests call POSIX binaries (/bin/true, /bin/false,
/bin/sleep) and will fail on Windows; update the tests (the test cases
"childWait: exit code 0", "childWait: exit code 1", and "childKill: terminates
process") to skip on Windows by checking std.os.tag (or equivalent) at the top
of each test and invoking std.testing.skip with a short message when running on
Windows, so the tests only run on POSIX platforms; reference the childWait and
childKill usages when making the change.

---

Nitpick comments:
In `@src/io.zig`:
- Around line 1763-1765: The small test block that anchors process_impl for
compilation is redundant because process_impl is already referenced by
childWaitImpl and childKillImpl; remove the test block containing "test { _ =
process_impl; }" to clean up the code, leaving process_impl referenced only via
childWaitImpl and childKillImpl (no other code changes required).

In `@src/process.zig`:
- Around line 31-35: childKill currently calls ev.ProcessWait.init and
waitForIoUncancelable but never inspects op.getResult(), which hides wait
failures; update childKill to call op.getResult() after waitForIoUncancelable
(or use the returned Result/!Error path) and handle errors appropriately before
calling childCleanup—e.g., propagate or log the error from op.getResult() so
wait failures are not ignored; ensure you still call childCleanup in success and
handle the case where op.getResult() indicates a failure from
ProcessWait/sendTermSignal.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 68b2114d-7bfb-4b6f-971a-7a99a60f8d3e

📥 Commits

Reviewing files that changed from the base of the PR and between aff2715 and ea9edbc.

📒 Files selected for processing (3)
  • src/io.zig
  • src/process.zig
  • test_runner.zig

Comment thread src/process.zig
Comment on lines +20 to +24
const status = op.getResult() catch |err| switch (err) {
error.ProcessNotFound => return error.Unexpected,
error.SystemResources => return error.Unexpected,
error.Canceled => return error.Canceled,
error.Unexpected => return error.Unexpected,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Map op.getResult().Canceled to error.Unexpected, not error.Canceled.

User cancellation is already handled by waitForIo. A canceled result from getResult() is backend/IO-level cancellation and should not be surfaced as user cancel.

Suggested fix
     const status = op.getResult() catch |err| switch (err) {
         error.ProcessNotFound => return error.Unexpected,
         error.SystemResources => return error.Unexpected,
-        error.Canceled => return error.Canceled,
+        error.Canceled => return error.Unexpected,
         error.Unexpected => return error.Unexpected,
     };

Based on learnings: In src/udp.zig, error.Canceled from completion result data is OS/IO-level cancellation and is expected to map to error.Unexpected.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const status = op.getResult() catch |err| switch (err) {
error.ProcessNotFound => return error.Unexpected,
error.SystemResources => return error.Unexpected,
error.Canceled => return error.Canceled,
error.Unexpected => return error.Unexpected,
const status = op.getResult() catch |err| switch (err) {
error.ProcessNotFound => return error.Unexpected,
error.SystemResources => return error.Unexpected,
error.Canceled => return error.Unexpected,
error.Unexpected => return error.Unexpected,
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/process.zig` around lines 20 - 24, The switch handling the error from
op.getResult() currently maps error.Canceled to error.Canceled but must map
backend/IO-level cancellations to error.Unexpected (user cancellation is already
handled by waitForIo); update the switch in the op.getResult() catch branch to
return error.Unexpected for error.Canceled (alongside other backend errors like
ProcessNotFound and SystemResources) so IO-level cancellation isn't surfaced as
a user cancel.

Comment thread src/process.zig Resolved
@lalinsky
lalinsky merged commit 5b89bb9 into main Apr 29, 2026
25 of 26 checks passed
@lalinsky
lalinsky deleted the process-child-wait-kill branch April 29, 2026 07:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant