Skip to content

runtime: add zio.recancel() - #699

Merged
lalinsky merged 1 commit into
mainfrom
public-recancel
Aug 21, 2026
Merged

lalinsky merged 1 commit into
mainfrom
public-recancel

Conversation

@lalinsky

Copy link
Copy Markdown
Owner

Re-arming a cancellation that a cancellation point already reported was only reachable through getCurrentTask().recancel(). This exposes it as zio.recancel(), next to checkCancel/beginShield/endShield, and it is a no-op outside a task context for the same reason checkCancel falls through to the syscall token there: a blocking task's cancellation is never consumed, so there is nothing to put back.

AnyTask.recancel() asserted user_canceled. That is stricter than std.Io.recancel, which asserts only that "error.Canceled was returned from a prior cancelation point" without caring who requested it (Dispatch/Uring assert cancel_protection.acknowledged, Threaded marks every non-.canceled status unreachable). It also does not fit a public API, since a task canceled by withTimeout has auto_canceled set and no user_canceled. The assert now accepts either kind.

That second part is a fix, not just API polish: waitForIo re-arms when an operation completes successfully despite a cancel request, and reaches that path from an auto-cancel whenever a withTimeout deadline races an I/O completion, which trips the old assert under runtime safety.

One difference from std remains by design: std tracks a boolean acknowledged flag and can assert on a double recancel, while zio counts pending_errors, which has to tolerate a second real cancel arriving while the first is being handled.

Tests: user-cancel re-arm in runtime.zig, auto-cancel re-arm in autocancel.zig (verifying withTimeout still attributes the re-delivered error and reports error.Timeout). The existing net.zig test now uses the public API.

Re-arming a cancellation that a cancellation point already reported was
only reachable through getCurrentTask().recancel(), i.e. through an
internal API. Expose it as zio.recancel(), mirroring std.Io.recancel and
the checkCancel/beginShield/endShield wrappers: a no-op outside a task
context, where a blocking task's cancellation is never consumed and so
needs no re-arming.

AnyTask.recancel() asserted user_canceled, which is stricter than
std.Io.recancel's "error.Canceled was returned from a prior cancelation
point" and does not fit a public API: a task canceled by withTimeout has
auto_canceled set instead. Assert the task is under a cancellation of
either kind. That also covers waitForIo, which re-arms when an operation
completes successfully despite a cancel request and could reach the
assert from an auto-cancel.
@coderabbitai

coderabbitai Bot commented Aug 21, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: c19b0efa-5174-4dfd-85bc-b2aeaaa1fb76

📥 Commits

Reviewing files that changed from the base of the PR and between 78bff08 and 625b2cb.

📒 Files selected for processing (5)
  • src/autocancel.zig
  • src/net.zig
  • src/runtime.zig
  • src/task.zig
  • src/zio.zig

Included review availability: Your plan provides up to 4 included reviews per hour; 0 remain after this review.


📝 Walkthrough

Walkthrough

The PR adds recancel support for consumed task cancellation, exports it through zio, updates a network test, and adds regression coverage for repeated cancellation and timeout attribution.

Changes

Cancellation Re-arming

Layer / File(s) Summary
Cancellation re-arming core
src/task.zig, src/runtime.zig
AnyTask.recancel accepts user cancellation or live auto-cancellation. runtime.recancel re-arms cancellation for the current task and does nothing outside task context.
Public API and call-site integration
src/zio.zig, src/net.zig
recancel is exported through zio. The network test uses the runtime helper.
Timeout behavior validation
src/autocancel.zig
The timeout test re-arms cancellation after error.Canceled, checks cancellation again, and verifies error.Timeout.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Merge Risk: ⚪ Minimal · up to 625b2

This change exposes cancellation re-arming through the public API and supports both user- and timeout-triggered cancellation paths; no actionable merge-blocking risk remains beyond normal checks and review.

Suggested reviewers: psznm

Poem

A canceled task wakes once more,
With recancel at its core.
Timeouts keep their proper name,
Checks return errors just the same.
The runtime rings the bell again.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the main change: adding the public zio.recancel() API.
Description check ✅ Passed The description accurately explains the new API, cancellation behavior changes, runtime safety fix, and related tests.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 0 files. (5 skipped: 5 unsupported.)
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.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch public-recancel

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

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

@lalinsky
lalinsky merged commit df666c2 into main Aug 21, 2026
31 checks passed
@lalinsky
lalinsky deleted the public-recancel branch August 21, 2026 07:49
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