Skip to content

Always free Tsan fibers - #595

Merged
lalinsky merged 2 commits into
lalinsky:mainfrom
psznm:free-tsan-fibers
Jul 21, 2026
Merged

lalinsky merged 2 commits into
lalinsky:mainfrom
psznm:free-tsan-fibers

Conversation

@psznm

@psznm psznm commented Jul 21, 2026 •

Copy link
Copy Markdown
Contributor

Partially solves #585

It does not fully solve the leak, but the memory usage growth is now much milder

~/git/zig-playground$ ./run.sh
RSS 22:01:29 = 20188 KB
RSS 22:01:31 = 75140 KB
RSS 22:01:33 = 95284 KB
RSS 22:01:35 = 125088 KB

(125M in 6 seconds instead of original 5G)

I couldn't find where the remaining leak is coming from, but I couldn't figure out if it is due to zio's tsan handling (because when tsan handling is disabled, tsan just crashes the example program), or if it just a TSan thing. Fibers now seem to be freed correctly and zio does nothing else with tsan.

Also this does not at all touch the issue mentioned in comment where TSan caused segfault after destroying seemingly already destroyed fiber, and that issue still persists.

Also as a side effect of this change, weird pause during program exit has disappeared in my program when run with thread sanitizer.

@coderabbitai

coderabbitai Bot commented Jul 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

Run ID: 38a01cd2-8214-4f36-9160-8302b451b3d7

📥 Commits

Reviewing files that changed from the base of the PR and between 96d7ab2 and e63e158.

📒 Files selected for processing (2)
  • src/runtime.zig
  • src/task.zig
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/task.zig

📝 Walkthrough

Walkthrough

AnyTask.releaseCoro now owns coroutine stack release and deinitialization. AnyTask.destroy and executor finish cleanup both use this helper, with cleanup skipped when no stack allocation remains.

Changes

Task lifecycle

Layer / File(s) Summary
Centralized coroutine teardown
src/task.zig, src/runtime.zig
AnyTask.releaseCoro releases allocated stacks, resets allocation state, and deinitializes the coroutine; task destruction and executor finish cleanup delegate to it.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

Suggested reviewers: lalinsky

Poem

Stacks return to the pool,
Coroutines rest when done.
One helper guards the exit,
Cleanup follows one path,
Tasks fade cleanly into none.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title matches the core change: freeing TSan fibers when tasks finish.
Description check ✅ Passed The description is directly about the same TSan fiber leak fix and its remaining limits.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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

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.

Reclaim the stack and the TSan fiber together in AnyTask.releaseCoro,
called from the executor's finish cleanup instead of waiting for the
last task reference to drop. destroy() still calls it for tasks that
were created but never ran.
@lalinsky

Copy link
Copy Markdown
Owner

Oh, thank you. I messed up, the goal was to have the tsan fiber destroy done in two places, but I apparently forgot the main one. I've pushed a commit to your branch with a small refactoring, to destroy the tsan fiber immediately after the task exits, not after it's "reaped".

@lalinsky
lalinsky merged commit 3fb22a1 into lalinsky:main Jul 21, 2026
28 checks passed
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.

2 participants