Skip to content

Track both coroutine and blocking tasks in runtime registry - #81

Merged
lalinsky merged 5 commits into
mainfrom
refactor/track-blocking-tasks
Oct 26, 2025
Merged

lalinsky merged 5 commits into
mainfrom
refactor/track-blocking-tasks

Conversation

@lalinsky

Copy link
Copy Markdown
Owner

No description provided.

Previously, Runtime.tasks only tracked AnyTask (coroutine-based tasks),
causing the runtime to potentially shut down while blocking tasks were
still running.

Changes:
- Add intrusive list fields to Awaitable (next, prev, in_list)
- Create AwaitableList to track Awaitable instead of AnyTask
- Add blocking tasks to registry when created and remove when completed
- Consolidate cleanup logic with releaseAwaitable(awaitable, done) flag
- Move maybeShutdown from Executor to Runtime for better architecture
- Fix shutdown bug: blocking tasks now properly trigger shutdown

This ensures proper lifecycle management for all awaitable types and
prevents premature runtime shutdown.
@coderabbitai

coderabbitai Bot commented Oct 26, 2025 •

Copy link
Copy Markdown
Contributor

Caution

Review failed

The pull request is closed.

Walkthrough

Replaces a concurrent task registry with a simpler Awaitable-based registry: adds intrusive list fields to Awaitable, introduces AwaitableList, removes ConcurrentAwaitableList, updates WaitNode shape, and changes Runtime lifecycle to use AwaitableList, a done flag on release, and a maybeShutdown() coordination path.

Changes

Cohort / File(s) Summary
Awaitable struct & node
src/core/Awaitable.zig, src/core/WaitNode.zig
Added intrusive-list fields next: ?*Awaitable, prev: ?*Awaitable, and in_list (debug-only) to Awaitable. Removed tail field from WaitNode.
New non-concurrent registry
src/core/AwaitableList.zig
Added AwaitableList singleton-like container and public API: add, remove, close, isEmpty, isClosed, pop, plus internal state constants and empty queue init.
Removed concurrent registry
src/core/ConcurrentAwaitableList.zig
Deleted entire file and its API (type, state machine, mutation lock, push/pop/remove, and tests).
Runtime integration
src/runtime.zig
Replaced AnyTaskList with AwaitableList; changed releaseAwaitable(self, awaitable, comptime done: bool) signature and behavior; added maybeShutdown() and updated spawn/spawnBlocking, draining, deinit, and blocking task lifecycle to use the new registry and done semantics.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

  • Check every call-site of releaseAwaitable for correct done usage (final vs intermediate releases).
  • Verify shutdown edge-cases around AwaitableList.pop() and close() transitions (last-item handling).
  • Confirm removal of ConcurrentAwaitableList doesn't reintroduce races where concurrent mutation previously relied on its lock semantics.
  • Inspect tests removed with ConcurrentAwaitableList—ensure equivalent behavior is covered or intentionally deprecated.

Possibly related PRs

Poem

A node now holds a little link,
The list is simple, no more sync.
One done to whisper "this is last",
maybeShutdown wakes the past.
Tasks march out — the registry trimmed. 🎉

Pre-merge checks and finishing touches

❌ Failed checks (2 warnings)
Check name Status Explanation Resolution
Description Check ⚠️ Warning No pull request description was provided by the author. The description field is empty, which means it does not describe any aspect of the changeset. While the title check is about clarity, the description check requires at least some form of explanation relating to the changes made. An empty description fails to meet this baseline requirement.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (1 passed)
Check name Status Explanation
Title Check ✅ Passed The title "Track both coroutine and blocking tasks in runtime registry" directly reflects the core purpose of these changes. The changeset refactors the task registry system (replacing AnyTaskList/ConcurrentAwaitableList with AwaitableList) and updates runtime.zig to track Awaitable objects that encompass both coroutine and blocking task types. The title is specific and descriptive enough that a developer scanning the history would understand the registry was updated to manage both task types together. While the title focuses on the what (what gets tracked) rather than the how (replacing the registry implementation), that's appropriate for a commit title.

📜 Recent review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1649231 and 5cbf24d.

📒 Files selected for processing (1)
  • src/runtime.zig (11 hunks)

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 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 (1)
src/runtime.zig (1)

1739-1749: The done flag logic works but is subtle.

When done=false, we skip both the registry removal and the shutdown check. This is used in two scenarios:

  1. JoinHandle drop (awaitable stays in registry until task completes)
  2. Deinit cleanup (pop already removed it, don't trigger shutdown)

The deinit usage (lines 1484-1486) works because pop() already removed the awaitable, but it's not obvious from reading this function alone.

Consider adding a comment:

 fn releaseAwaitable(self: *Runtime, awaitable: *Awaitable, comptime done: bool) void {
+    // done=true: awaitable completed, remove from registry and check for shutdown
+    // done=false: JoinHandle drop or already removed (e.g., during deinit)
     if (done) {
         _ = self.tasks.remove(awaitable);
     }
📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e3c79f0 and 1649231.

📒 Files selected for processing (5)
  • src/core/Awaitable.zig (1 hunks)
  • src/core/AwaitableList.zig (1 hunks)
  • src/core/ConcurrentAwaitableList.zig (0 hunks)
  • src/core/WaitNode.zig (0 hunks)
  • src/runtime.zig (11 hunks)
💤 Files with no reviewable changes (2)
  • src/core/WaitNode.zig
  • src/core/ConcurrentAwaitableList.zig
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-10-16T14:33:19.718Z
Learnt from: lalinsky
PR: lalinsky/zio#48
File: src/core/ConcurrentAwaitableList.zig:254-269
Timestamp: 2025-10-16T14:33:19.718Z
Learning: In the zio repository's `src/core/ConcurrentAwaitableList.zig` and `src/core/Awaitable.zig`, the `in_list` field should remain debug-only (conditional compilation with `builtin.mode == .Debug`). Double-removal detection in `ConcurrentAwaitableList.remove()` uses pointer-based checks (comparing `prev`/`next` against `head`/`tail`) rather than relying on a regular `in_list` field.

Applied to files:

  • src/core/Awaitable.zig
  • src/core/AwaitableList.zig
🔇 Additional comments (8)
src/core/Awaitable.zig (1)

38-41: Intrusive list fields look good.

The three fields (next, prev, in_list) properly support the AwaitableList registry. The debug-only in_list field follows the established pattern from the codebase.

Based on learnings.

src/core/AwaitableList.zig (2)

4-17: Clean registry design.

The state encoding is straightforward: you can only close when empty, which prevents the complexity of draining a closed registry. Using WaitQueue's sentinel states for this is clever.


19-61: Registry methods are solid.

The atomic transitions in add() and close() properly coordinate shutdown. Making close() idempotent (line 40) is the right call. The behavior where pop() transitions back to empty_open means the registry can accept new tasks after draining during non-shutdown scenarios.

src/runtime.zig (5)

119-121: Correct cleanup for blocking tasks.

Calling releaseAwaitable with done=true after the blocking task completes properly removes it from the registry and triggers shutdown checks.


358-361: Correct: JoinHandle drop doesn't mean task completion.

Using done=false here is right—the awaitable stays in the registry until it actually completes execution, even if the caller drops their handle.


526-532: JoinHandle cleanup is correct.


1037-1038: Task completion properly handled.


1719-1737: Shutdown coordination is sound.

The atomic close() transition ensures only one thread initiates shutdown. The race between task completion and new spawns is handled correctly—if a spawn sneaks in before shutdown, we simply wait for that task to complete and try again.

Comment thread src/runtime.zig Resolved
Comment thread src/runtime.zig Resolved
@lalinsky
lalinsky merged commit e2d3afa into main Oct 26, 2025
@lalinsky
lalinsky deleted the refactor/track-blocking-tasks branch October 26, 2025 14:07
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