Repository navigation
Add instrumentation to help TSan understand fiber context switches - #566
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
🚧 Files skipped from review as they are similar to previous changes (3)
📝 WalkthroughWalkthroughThis change adds conditional TSan fiber wrappers, stores fiber handles in coroutine contexts, switches TSan fibers during context changes, and destroys fibers during coroutine and task teardown. ChangesTSan coroutine integration
Estimated code review effort: 3 (Moderate) | ~25 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 inconclusive)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/coro/tsan.zig`:
- Around line 40-48: Add a module-level `//!` documentation comment in
`src/coro/tsan.zig` explaining why `switchToFiber` passes `flags == 0` for
cooperative context switches, or remove the “see the module doc comment”
parenthetical from `switchToFiber`’s comment.
🪄 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: aeecc22a-2839-427e-835c-8cf893a45e9e
📒 Files selected for processing (3)
src/coro/coroutines.zigsrc/coro/tsan.zigsrc/task.zig
| /// Tell TSan that `fiber` is now the running fiber on this OS thread. Call at | ||
| /// the moment of a context switch, just before the stacks are swapped. Uses | ||
| /// flags == 0 so the switch establishes the cooperative happens-before edge | ||
| /// (see the module doc comment). | ||
| pub inline fn switchToFiber(fiber: Fiber) void { | ||
| if (enabled) { | ||
| if (fiber) |f| c.__tsan_switch_to_fiber(f, 0); | ||
| } | ||
| } |
There was a problem hiding this comment.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Dangling reference: there's no module doc comment to "see".
The comment says the flags==0 rationale is explained in "the module doc comment", but this file has no //! module doc — line 1 jumps straight into const builtin. For a module encoding subtle TSan happens-before semantics, the next reader will go looking for something that isn't there. Either add the //! doc that explains why flags==0 (sync) is the right choice for cooperative switches, or drop the parenthetical.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@src/coro/tsan.zig` around lines 40 - 48, Add a module-level `//!`
documentation comment in `src/coro/tsan.zig` explaining why `switchToFiber`
passes `flags == 0` for cooperative context switches, or remove the “see the
module doc comment” parenthetical from `switchToFiber`’s comment.
There was a problem hiding this comment.
try to assume why is the flag set that way
There was a problem hiding this comment.
@lalinsky Reasoning it out:
The __tsan_switch_to_fiber API has one documented flag: __tsan_switch_to_fiber_no_sync (value 1). Passing 0 means "no flags", i.e. use the default sync behavior — TSan establishes a happens-before edge between the fiber you're switching away from and the fiber you're switching to.
That's exactly what you want here. Coroutines are cooperatively scheduled — only one runs at a time on a given OS thread, and control is explicitly handed off via switchContext, like passing a baton. So any writes the outgoing coroutine did before yielding must be visible to whatever runs next when it reads that same state later. Using sync (0) tells TSan "this is a legitimate ordering point", so it won't flag those accesses as a data race just because they happened on "different" fibers.
The no_sync flag exists for cases where you don't want that ordering guarantee — e.g. a thread pool grabbing a fiber to run unrelated, independent work, where there's no real handoff relationship between whoever ran on that fiber last and whoever's running now. That's not this case: zio's coroutine switches are always resume/yield pairs with a defined program order, so sync is the correct choice.
So the flag isn't arbitrary — it's the deliberate choice to model coroutine switches as synchronization points, matching how a mutex handoff would be treated. Worth spelling this out in a //! module doc comment since it's the reasoning switchToFiber's own comment refers back to but doesn't currently exist.
✏️ Learnings added
Learnt from: lalinsky
Repo: lalinsky/zio PR: 566
File: src/coro/tsan.zig:40-48
Timestamp: 2026-07-10T21:51:36.493Z
Learning: In zio's src/coro/tsan.zig, `switchToFiber` calls `__tsan_switch_to_fiber(fiber, 0)` deliberately using flags=0 (sync, not `__tsan_switch_to_fiber_no_sync`). This is intentional: coroutine context switches are cooperative resume/yield handoffs with a defined program order (only one fiber runs at a time per OS thread), so TSan should establish a happens-before edge across the switch, same as a mutex handoff. The `no_sync` flag would be appropriate only for unrelated fiber reuse (e.g. thread pool grabbing an arbitrary fiber), not for zio's coroutine switching.
Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.
523b8ba to
d4ab269
Compare
No description provided.