forkserver preload's COW sharing is destroyed by the GC in each job process (~96MB per idle worker)
Summary
On Linux the worker preloads every registered plugin package plus
livekit.agents.inference._warmup into the forkserver process, so that job
processes inherit the already-imported modules and the warmed native models
through copy-on-write (worker.py, set_forkserver_preload).
That sharing does not survive. Each job process runs its own cyclic garbage collector, and the first full collection in the child writes to nearly every page that holds a preloaded Python object, forcing the kernel to copy it. The memory the preload was supposed to save comes back, per process.
Why it happens
CPython puts a PyGC_Head (two pointers) immediately before every
GC-tracked object. A collection pass rewrites those two words for every object
in the generation being scanned — it is a write, even when nothing is
collected. Preloaded modules, classes, functions, docstring/annotation dicts,
enum members, pydantic models etc. are long-lived, so they all end up in the
oldest generation. The first full collection inside a forked job process
therefore touches the header of essentially every preloaded object and
un-shares the pages holding them.
To confirm the scale, a forkserver preloading just
livekit.agents.ipc transitively (i.e. livekit.agents + its deps) hands each
forked child 233,838 GC-tracked objects. Those objects are spread across
almost every page of the inherited heap, so a full pass dirties nearly all of
it.
Two things this is not:
- It is not the native model weights.
init_vad()/init_eot()allocate outside the Python heap and stay shared — that part of the preload works. - It is not fixed by fewer jobs. It is per process and it happens without any
job traffic: an idle prewarmed process reaches a full collection during
initialize_process_fnc/ prewarm, so the pages are already private before the first job is ever assigned.
Observed on a Linux worker: ~96 MB per idle job process moves from shared
to private. With num_idle_processes > 1 that multiplies directly.
Reproducing
On Linux, for a running worker:
# forkserver pid, then any job process pid
grep -E '^(Shared_Clean|Private_Dirty)' /proc/<job-pid>/smaps_rollup
Sample it right after the process is forked, then again after the process has
run a full collection (gc.collect(2), or just let it idle through a prewarm).
Shared_Clean drops and Private_Dirty rises by the same amount.
Fix
gc.freeze() in the forkserver process once everything is preloaded. It moves
every currently tracked object into the permanent generation, which the
collector never scans, so their GC headers are never rewritten and the pages
stay shared for the life of the child. Frozen objects are still freed normally
by refcounting if they ever die; freezing module-level state costs nothing
because it lives for the process lifetime anyway.
It has to run inside the forkserver, not in the worker — job processes fork
from the forkserver, so freezing in the parent has no effect on them. The
codebase already has the mechanism for running code there: a side-effect module
in the preload list (livekit.agents.inference._warmup). So the fix is one
more such module, listed last:
# livekit/agents/ipc/_preload_freeze.py
import gc
gc.collect() # don't make import-time cycle garbage permanent
gc.freeze()
plugin_packages = [p.package for p in Plugin.registered_plugins] + [
"av",
"livekit.agents.inference._warmup",
"livekit.agents.ipc._preload_freeze", # keep last
]
Kept import-free on purpose: the forkserver's preload loop swallows
ImportError, so putting the gc.freeze() call at the end of _warmup.py
would silently skip it on any host where livekit-local-inference fails to
import — while the plugin preload, and therefore the problem, is still there.
Side benefit: those ~230k objects also stop being walked on every full collection in the job process, which removes a recurring GC pause from the audio path.
Alternatives considered and rejected:
gc.freeze()in the worker process — wrong process, job processes fork from the forkserver.gc.disable()or raised thresholds in the job process — leaks real cycles for the whole lifetime of a long-running session.
Verified that the freeze survives the fork: a child forked from a forkserver
that preloaded the module reports gc.get_freeze_count() == 233838.
No behaviour change on spawn (macOS/Windows), where there is no preload to
begin with.
PR: #7118
@theomonnom @chenghao-mou can pls review it