Repository navigation
Auto-reap group tasks on completion - #164
Conversation
Previously, group tasks accumulated in memory until group.wait() or group.cancel() was called. For long-running groups, this could cause unbounded memory growth. Now, when a group task completes, it is immediately removed from the group's task list and the group's reference is released. This prevents memory accumulation while maintaining all existing functionality. The implementation is thread-safe: both regular tasks (completed on executor threads) and blocking tasks (completed on aio event loop thread) are handled correctly, with proper synchronization to ensure exactly one release happens even if groupCancel races with completion.
WalkthroughAdds group-aware cleanup during awaitable completion across two runtime contexts. When an awaitable finishes, the code now checks if it belongs to a group, removes it from the group's task list if present, and releases the group's reference without shutdown semantics to prevent improper double-release. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
Possibly related PRs
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro 📒 Files selected for processing (2)
🧰 Additional context used📓 Path-based instructions (1)**/*.zig📄 CodeRabbit inference engine (CLAUDE.md)
Files:
🧠 Learnings (3)📓 Common learnings📚 Learning: 2025-10-03T11:09:46.846ZApplied to files:
📚 Learning: 2025-10-16T14:33:19.718ZApplied to files:
⏰ 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). (1)
🔇 Additional comments (2)
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 |
Problem
Long-running groups accumulate task allocations because completed tasks are only reaped when
group.wait()orgroup.cancel()is called. While stacks are released immediately, the task structs remain allocated (~200 bytes + context size per task).For groups that run indefinitely or for long periods, this causes unbounded memory growth.
Solution
When a group task completes, it is now immediately removed from the group's task list and the group's reference is released. This prevents memory accumulation while maintaining all existing functionality.
Implementation
The changes are minimal and focused:
src/runtime.zig): After marking a task complete in the scheduler loop, remove it from the group list and release the group's referencesrc/core/blocking_task.zig): Same logic in the completion callback which runs on the aio event loop threadThread Safety
The implementation handles concurrent access correctly:
remove()andpop()on CompactWaitQueue are protected by atomic mutexpop()in cleanup won't see itremove()returns falseremove(), we ensure exactly one release happensTesting
All existing tests pass without modification (168/168), including the group-specific tests:
Behavior
User-visible: None - groups work exactly the same way from user perspective
Internal: Long-running groups no longer accumulate completed task allocations. Memory is released immediately after each task completes.