Break the DispatchSession ↔ worker-task reference cycle to reduce per-dispatch GC pressure — Closes #266#272
Merged
conradbzura merged 2 commits intoJul 2, 2026
Conversation
The worker driver task runs a coroutine that closes over its DispatchSession, and the session holds the task in _worker_task, so the session, its task, and the task's forked contexts form a reference cycle that only the cyclic collector can reclaim. Under sustained dispatch these accumulate between collections. Clear _worker_task in the task's done-callback once the completion future is settled, breaking the cycle so reference counting reclaims the session and task as soon as the dispatch ends. The only reader after completion, cancel, already tolerates a None task, and teardown synchronizes on the separate completion future, so dropping the reference is safe.
Drive a coroutine dispatch to natural completion and assert the worker driver task is reclaimed, observed through a weakref that clears with automatic collection disabled, so reference counting rather than a cyclic pass frees it. Retain the finished session explicitly to prove it no longer pins the task. The driver task is identified through its coroutine name, as no public handle distinguishes it from an in-flight step task on the worker loop.
760955e to
7759fd0
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Break the
DispatchSession → _worker_task → coro → sessionreference cycle so a completed dispatch's session, worker task, and per-fork contexts are reclaimed promptly by refcounting instead of lingering until the next cyclic-GC pass. The worker driver's_runcoroutine closes over the session, so the session's strong reference to its worker task formed a cycle only the cyclic collector could reclaim; under sustained dispatch these accumulate between GC passes (~40× more live tasks / ~14× more contexts without forced GC). Clearself._worker_taskin the task's done-callback to sever it. This is safe: the only post-completion reader,cancel(), already guards on the worker task being non-None, and teardown (drain/__aexit__) synchronizes on_worker_done, not_worker_task. Closes #266Proposed changes
Clear the worker-task reference on completion (
runtime/worker/session.py)In
DispatchSession._schedule_worker's_on_donedone-callback, setself._worker_task = Noneafter settling the completion future. This drops the session's strong reference to the finished worker task, sosession ↔ _worker_task ↔ corois no longer a cycle and refcounting reclaims the objects immediately.Test cases
test_sessionweakrefclears) with automatic GC disabledtest_sessiongc.collect()is forcedweakrefto the finished worker task still clears — refcounting reclaims it, not the retained session pinning it