Skip to content

Pass timestamp to StackPool.release() instead of getting current time - #177

Merged
lalinsky merged 1 commit into
mainfrom
stack-pool-timestamp-param
Jan 6, 2026
Merged

lalinsky merged 1 commit into
mainfrom
stack-pool-timestamp-param

Conversation

@lalinsky

@lalinsky lalinsky commented Jan 6, 2026

Copy link
Copy Markdown
Owner

Summary

  • StackPool.release() now takes a timestamp_ns parameter instead of calling std.time.Instant.now() internally
  • Runtime passes the event loop's cached monotonic time (loop.now())
  • Tests can pass mock timestamps, eliminating the need for 150ms sleep in the age-based expiration test

Test plan

  • All existing StackPool tests pass
  • Full test suite passes

@coderabbitai

coderabbitai Bot commented Jan 6, 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 15 minutes and 0 seconds before requesting another review.

⌛ 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.

📥 Commits

Reviewing files that changed from the base of the PR and between 07f7231 and 05097e5.

📒 Files selected for processing (3)
  • src/coro/stack_pool.zig
  • src/runtime.zig
  • src/runtime/task.zig
📝 Walkthrough

Walkthrough

The PR threads monotonic timestamps through StackPool.release, replacing implicit real-time calls with explicit timestamp_ns parameters. FreeNode stores timestamp_ns, and age calculations compute elapsed time from passed timestamps rather than calling now(). Call sites in runtime.zig and runtime/task.zig updated to pass timestamps.

Changes

Cohort / File(s) Summary
StackPool core implementation
src/coro/stack_pool.zig
Updated release() signature to accept timestamp_ns: u64 parameter; FreeNode now stores timestamp_ns instead of std.time.Instant; age calculations changed from now() - node.timestamp to timestamp_ns - node.timestamp_ns
Runtime call sites
src/runtime.zig, src/runtime/task.zig
Both files updated to pass self.loop.now() and rt.now() respectively as second argument to stack_pool.release()

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Poem

⏳ Time flows through the pool, no longer guessed,
Timestamps passed where stacks once rest,
Ages calculated, no real-time call,
Monotonic truth tells all. ✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: passing a timestamp parameter to StackPool.release() instead of having it call time internally.
Description check ✅ Passed The description clearly explains the change: removing internal time calls from StackPool.release(), having runtime pass loop.now(), and enabling mock timestamps in tests.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

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/coro/stack_pool.zig:
- Around line 92-93: The timestamp unit is mismatched: change the
StackPool.release parameter from timestamp_ns to timestamp_ms (u64) and update
its docstring to "milliseconds"; then convert the expiration check to compare
like-units by deriving max_age_ms = self.config.max_age_ns / 1_000_000 and using
that in the age comparison (or alternatively multiply timestamp_ms by 1_000_000
if you prefer nanosecond math), and update all call sites (calls passing
loop.now() / rt.now() and any other callers) to pass the millisecond value and
the renamed parameter.
📜 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 0969111 and 07f7231.

📒 Files selected for processing (3)
  • src/coro/stack_pool.zig
  • src/runtime.zig
  • src/runtime/task.zig
🧰 Additional context used
📓 Path-based instructions (1)
**/*.zig

📄 CodeRabbit inference engine (CLAUDE.md)

Use zig env to get the path to the Zig standard library when checking standard library

Files:

  • src/runtime/task.zig
  • src/runtime.zig
  • src/coro/stack_pool.zig
🧠 Learnings (12)
📚 Learning: 2025-11-04T10:29:56.949Z
Learnt from: lalinsky
Repo: lalinsky/zio PR: 118
File: src/core/task.zig:33-115
Timestamp: 2025-11-04T10:29:56.949Z
Learning: In zio's task implementation (src/core/task.zig), the Closure struct intentionally caps result_len and context_len at 4096 bytes (using u12 fields) and alignment at 16 bytes (using u4 padding fields). This is a deliberate design decision to avoid bloating task structures. Users needing larger result or context storage should heap-allocate and pass pointers instead. These limits are more generous than std lib's fixed storage (64 bytes for results, 1024 bytes for args).
<!-- [add_learning]
The task storage limits in zio (4 KiB for result/context, 16-byte alignment) are intentional design constraints to prevent task bloat, not correctness issues. The maintainer's position is that larger data should be heap-allocated.

Applied to files:

  • src/runtime/task.zig
📚 Learning: 2025-10-03T11:09:46.846Z
Learnt from: lalinsky
Repo: lalinsky/zio PR: 18
File: src/net.zig:18-25
Timestamp: 2025-10-03T11:09:46.846Z
Learning: In zio's `Runtime.BlockingTask`, calling `deinit()` on a blocking task only frees the task wrapper (TaskData) itself, not the result returned by the blocking function. The result remains valid and owned by the caller, who is responsible for its cleanup. This is the intended design for async blocking tasks.

Applied to files:

  • src/runtime/task.zig
📚 Learning: 2026-01-06T09:33:09.370Z
Learnt from: CR
Repo: lalinsky/zio PR: 0
File: src/libs/coro/CLAUDE.md:0-0
Timestamp: 2026-01-06T09:33:09.370Z
Learning: Applies to src/libs/coro/**/*.zig : Don't assume you know how Zig std APIs work; check the current documentation as information becomes outdated

Applied to files:

  • src/runtime/task.zig
  • src/runtime.zig
  • src/coro/stack_pool.zig
📚 Learning: 2026-01-06T09:33:09.370Z
Learnt from: CR
Repo: lalinsky/zio PR: 0
File: src/libs/coro/CLAUDE.md:0-0
Timestamp: 2026-01-06T09:33:09.370Z
Learning: Applies to src/libs/coro/**/*.zig : Do not depend on `std.posix` - create custom wrappers instead in preparation for Zig 0.16 upgrade

Applied to files:

  • src/runtime/task.zig
  • src/runtime.zig
  • src/coro/stack_pool.zig
📚 Learning: 2026-01-06T09:33:09.370Z
Learnt from: CR
Repo: lalinsky/zio PR: 0
File: src/libs/coro/CLAUDE.md:0-0
Timestamp: 2026-01-06T09:33:09.370Z
Learning: Applies to src/libs/coro/**/build.zig.zon : Update version in build.zig.zon during release process

Applied to files:

  • src/runtime.zig
  • src/coro/stack_pool.zig
📚 Learning: 2026-01-06T09:32:59.436Z
Learnt from: CR
Repo: lalinsky/zio PR: 0
File: src/libs/aio/CLAUDE.md:0-0
Timestamp: 2026-01-06T09:32:59.436Z
Learning: Applies to src/libs/aio/**/build.zig.zon : Release process step 2: Update version in build.zig.zon

Applied to files:

  • src/runtime.zig
📚 Learning: 2025-10-15T19:38:44.659Z
Learnt from: lalinsky
Repo: lalinsky/zio PR: 47
File: src/sync/Mutex.zig:237-237
Timestamp: 2025-10-15T19:38:44.659Z
Learning: In the `unlock` method of `src/sync/Mutex.zig`, the `runtime` parameter is intentionally retained (marked with `_ = runtime;`) for API consistency and future-proofing, even though it's not currently used in the implementation.

Applied to files:

  • src/runtime.zig
📚 Learning: 2026-01-06T09:32:59.436Z
Learnt from: CR
Repo: lalinsky/zio PR: 0
File: src/libs/aio/CLAUDE.md:0-0
Timestamp: 2026-01-06T09:32:59.436Z
Learning: Applies to src/libs/aio/**/*.zig : In preparation for Zig 0.16 upgrade, don't depend on `std.posix`; create and use custom wrappers instead

Applied to files:

  • src/coro/stack_pool.zig
📚 Learning: 2026-01-06T09:32:50.539Z
Learnt from: CR
Repo: lalinsky/zio PR: 0
File: CLAUDE.md:0-0
Timestamp: 2026-01-06T09:32:50.539Z
Learning: Applies to **/*.zig : Use `zig env` to get the path to the Zig standard library when checking standard library

Applied to files:

  • src/coro/stack_pool.zig
📚 Learning: 2026-01-06T09:32:59.436Z
Learnt from: CR
Repo: lalinsky/zio PR: 0
File: src/libs/aio/CLAUDE.md:0-0
Timestamp: 2026-01-06T09:32:59.436Z
Learning: Use `zig env` to get the path to the Zig standard library when checking standard library APIs

Applied to files:

  • src/coro/stack_pool.zig
📚 Learning: 2026-01-06T09:33:09.370Z
Learnt from: CR
Repo: lalinsky/zio PR: 0
File: src/libs/coro/CLAUDE.md:0-0
Timestamp: 2026-01-06T09:33:09.370Z
Learning: Use `zig env` to get the path to the Zig standard library when checking Zig APIs

Applied to files:

  • src/coro/stack_pool.zig
📚 Learning: 2026-01-06T09:32:59.436Z
Learnt from: CR
Repo: lalinsky/zio PR: 0
File: src/libs/aio/CLAUDE.md:0-0
Timestamp: 2026-01-06T09:32:59.436Z
Learning: Don't assume knowledge of how std APIs work in Zig; most likely you have outdated information

Applied to files:

  • src/coro/stack_pool.zig
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (18)
  • GitHub Check: test (master, ReleaseSafe, ubuntu-latest, x86_64-netbsd, netbsd)
  • GitHub Check: test (0.15.2, ReleaseSafe, windows-latest, native)
  • GitHub Check: test (master, ReleaseSafe, ubuntu-latest, riscv64-linux, true)
  • GitHub Check: test (master, ReleaseSafe, ubuntu-latest, loongarch64-linux, true)
  • GitHub Check: test (master, Debug, macos-15-intel, native)
  • GitHub Check: test (master, ReleaseSafe, windows-latest, native)
  • GitHub Check: test (master, Debug, windows-latest, native)
  • GitHub Check: test (master, ReleaseSafe, ubuntu-latest, x86_64-freebsd, freebsd)
  • GitHub Check: test (master, ReleaseSafe, macos-latest, native)
  • GitHub Check: test (0.15.2, ReleaseSafe, macos-latest, native)
  • GitHub Check: test (master, ReleaseSafe, macos-15-intel, native)
  • GitHub Check: test (master, Debug, ubuntu-latest, riscv64-linux, true)
  • GitHub Check: test (master, Debug, ubuntu-latest, x86_64-netbsd, netbsd)
  • GitHub Check: test (0.15.2, Debug, ubuntu-latest, x86_64-netbsd, netbsd)
  • GitHub Check: test (0.15.2, ReleaseSafe, ubuntu-latest, x86_64-netbsd, netbsd)
  • GitHub Check: test (0.15.2, ReleaseSafe, ubuntu-latest, x86_64-freebsd, freebsd)
  • GitHub Check: test (0.15.2, Debug, windows-latest, native)
  • GitHub Check: test (windows-latest)
🔇 Additional comments (6)
src/coro/stack_pool.zig (4)

13-13: Good simplification to use u64 directly.

Storing raw nanoseconds instead of wrapping in std.time.Instant makes the code simpler and the intent clearer.


113-113: Saturating subtraction is the right choice here.

Using -| handles the edge case where timestamps might be out of order (though shouldn't happen with monotonic time). Good defensive coding.


201-236: Test updates look good.

Passing explicit timestamps (0) to tests makes them deterministic and easy to understand.


252-283: Excellent test improvement - this is the real win.

Replacing the 150ms sleep with mock timestamps makes this test:

  • Instant: No waiting for real time to pass
  • Deterministic: No flakiness from timing variations
  • Clearer: The test logic directly validates age-based expiration without timing noise

This kind of testability improvement is exactly why explicit time injection is valuable.

src/runtime/task.zig (1)

315-315: Call site correctly updated.

Passing rt.now() provides the event loop's cached monotonic timestamp to the release function. Clean and consistent with the new signature.

src/runtime.zig (1)

551-551: Efficient use of direct loop access.

Since this code is already in the executor's scheduler loop, using self.loop.now() directly is more efficient than calling self.runtime.now() which would need to resolve back to the same loop.

Comment thread src/coro/stack_pool.zig Resolved
This makes StackPool not access system time internally, instead requiring
the caller to pass a timestamp in milliseconds. The runtime passes the
event loop's cached monotonic time, and tests can pass mock timestamps
to avoid sleep.
@lalinsky
lalinsky force-pushed the stack-pool-timestamp-param branch from 07f7231 to 05097e5 Compare January 6, 2026 10:06
@lalinsky
lalinsky merged commit 7aba80a into main Jan 6, 2026
@lalinsky
lalinsky deleted the stack-pool-timestamp-param branch January 6, 2026 10:09
This was referenced Jan 16, 2026
@coderabbitai coderabbitai Bot mentioned this pull request Jul 21, 2026
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