Skip to content

Add blockInPlace for zero-allocation blocking operations - #311

Merged
lalinsky merged 1 commit into
mainfrom
block-in-place
Feb 10, 2026
Merged

lalinsky merged 1 commit into
mainfrom
block-in-place

Conversation

@lalinsky

Copy link
Copy Markdown
Owner

Summary

  • Add blockInPlace function that runs blocking functions on the thread pool without allocations (all state kept on stack)
  • Add task.getThreadPool() helper method
  • Update HostName.lookup to use blockInPlace instead of spawnBlocking

Test plan

  • Existing tests pass
  • New blockInPlace: basic computation test added

Add a new blockInPlace function that runs blocking functions on the
thread pool without allocating. Unlike spawnBlocking, all state is kept
on the stack. Supports cancellation by attempting to cancel the work
and waiting for completion.

Also adds task.getThreadPool() helper and updates HostName.lookup to
use blockInPlace instead of spawnBlocking.
@coderabbitai

coderabbitai Bot commented Feb 10, 2026 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

This PR introduces blockInPlace, a public API for executing blocking functions on the thread pool while keeping state on the stack. It includes a new inline method on AnyTask to access the thread pool, refactors DNS lookup to use the new blocking call, and exports the function at the module level.

Changes

Cohort / File(s) Summary
blockInPlace Core Implementation
src/common.zig
New public function to execute blocking operations on thread pool without allocations. Manages context, work submission, waiter signaling, and cancellation handling. Includes test coverage.
Runtime Task Support
src/runtime/task.zig
Added inline getThreadPool() method to AnyTask for accessing the Zigev thread pool from the task's runtime context.
DNS Lookup Refactor
src/net.zig
Replaced spawnBlocking with blockInPlace in HostName.lookup. Removed task management boilerplate; direct inline blocking execution with simplified error handling.
Public API Export
src/zio.zig
Re-exported blockInPlace at module level for public access.
Documentation
docs/changelog.md
Added Unreleased section documenting the new blockInPlace feature.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~28 minutes

Possibly related PRs

Poem

On the thread pool's stage, work finds its place,
No allocations cluttering the stack's space,
A task blocks in—no dancers to wait,
Context and result sealed tight by fate.
Cancellation handled with pragmatic grace,
DNS lookups now find their own pace. 🧵⚡

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main feature: adding blockInPlace for zero-allocation blocking operations, which is the core of this PR.
Description check ✅ Passed The description directly relates to the changeset, outlining the three main changes (blockInPlace function, getThreadPool helper, and HostName.lookup update) plus test coverage.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

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

✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch block-in-place

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: 1

🤖 Fix all issues with AI agents
In `@src/common.zig`:
- Around line 372-384: The test calls blockInPlace directly on the main thread
so Waiter.init sees getCurrentTaskOrNull() == null and takes the fast
direct-call path, leaving the thread-pool submission/completion/cancellation
logic untested; update the test to initialize the Runtime with a thread_pool
(Runtime.init with .{ .thread_pool = .{} }) and spawn a task that calls
blockInPlace (use Runtime.spawn to run a Test.run that invokes blockInPlace on
the worker), then join the handle so the test executes the thread-pool code
paths (submission, completion callback and cancellation) rather than the
main-thread fast path.
🧹 Nitpick comments (1)
src/common.zig (1)

370-370: Import buried at the bottom of the file.

All other imports live at lines 4-16. This one at line 370 is easy to miss. Consider moving it to the top with the rest.

Comment thread src/common.zig
Comment on lines +372 to +384
test "blockInPlace: basic computation" {
var rt = try Runtime.init(std.testing.allocator, .{});
defer rt.deinit();

const double = struct {
fn call(x: i32) i32 {
return x * 2;
}
}.call;

const result = blockInPlace(double, .{21});
try std.testing.expectEqual(42, result);
}

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

Test doesn't exercise the thread pool path.

This test runs blockInPlace on the main thread with no task context. Waiter.init() inside blockInPlace will see getCurrentTaskOrNull() == null, so it takes the fast path (line 349-351) and calls the function directly. The thread pool submission, completion callback, and cancellation logic are all untested.

To actually hit the interesting codepath, you'd need to run blockInPlace from inside a spawned task, similar to how the HostName: lookup tests do it:

const rt = try Runtime.init(std.testing.allocator, .{ .thread_pool = .{} });
defer rt.deinit();

const Test = struct {
    fn run() !void {
        const result = blockInPlace(double, .{21});
        try std.testing.expectEqual(42, result);
    }
};

var handle = try rt.spawn(Test.run, .{});
try handle.join();
🤖 Prompt for AI Agents
In `@src/common.zig` around lines 372 - 384, The test calls blockInPlace directly
on the main thread so Waiter.init sees getCurrentTaskOrNull() == null and takes
the fast direct-call path, leaving the thread-pool
submission/completion/cancellation logic untested; update the test to initialize
the Runtime with a thread_pool (Runtime.init with .{ .thread_pool = .{} }) and
spawn a task that calls blockInPlace (use Runtime.spawn to run a Test.run that
invokes blockInPlace on the worker), then join the handle so the test executes
the thread-pool code paths (submission, completion callback and cancellation)
rather than the main-thread fast path.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

Runtime.init() turns the main thread into task

@lalinsky
lalinsky merged commit e4d7ffc into main Feb 10, 2026
9 checks passed
@lalinsky
lalinsky deleted the block-in-place branch February 10, 2026 09:56
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