Repository navigation
Description
The in-process engine never frees a rooted Nix value. The RPC engine does.
Evidence
CoreEvalState._values is a strong set (_core/_objects.py:382).
wrap_value adds to it, and only CoreValue.close() removes from it.
inproc.Value (inproc/_impl.py:1329) has no weakref.finalize and no
__del__.
Measured on an attrset of 500 attributes:
inproc rooted after eval_string: 1
after as_dict: 501 after drop+gc: 501
after as_dict: 1001 after drop+gc: 1001
after as_dict: 1501 after drop+gc: 1501
Dropping every Python reference and running gc.collect() reclaims nothing.
The rpc engine does the opposite. ValueProxy.__init__ installs a
weakref.finalize (rpc/client/_session.py:403), _ensure_resolved installs
one for a lazily-selected child (:466), and
EvalProxy._drain_deferred_releases_locked (:298) sends the release calls at
the next RPC.
Why it matters
Each retained CoreValue holds a Nix RootValue, which is a root of the
Boehm GC. It therefore pins everything reachable from it in the Nix heap, not
one cell. Value.as_dict() and Value.as_list() root every child in one
call, and both are the documented way to read a value that Nix cannot flatten
to JSON.
The consumers most exposed are the long-lived ones: the REPL of pynix and
its LSP server, which hold one evaluator for the life of a process.
This is also an engine asymmetry that process isolation does not force, which
is the definition of a defect that tests/nanopynix/test_engine_parity.py
states.
Scope
Give inproc.Value the shape that rpc already has:
- Make
CoreEvalState._valuesaweakref.WeakSetofCoreValue, so the
close-everything sweep of the evaluator keeps working without the set being
the reason a value stays alive. - Register a
weakref.finalizeoninproc.Valuethat pushes itsCoreValue
onto a release queue for each evaluator. Do not call Nix from the
finalizer: it can run on any thread, andCoreValue.close()must run on
the evaluator thread. - Drain that queue at the start of
EvalSession.run, which is the single
chokepoint that every evaluator operation already passes through. This
mirrorsEvalProxy._drain_deferred_releases_locked.
Files
_core/_objects.py, inproc/_impl.py.
Depends on
#38, because both touch the ordering of the close.
Risk
Medium. A finalizer runs on an arbitrary thread, and the queue is what keeps
that safe. Copy the shape of _DeferredReleases rather than inventing one.
Acceptance criteria
The measured count returns to 1 after a drop, a collection, and one evaluator
operation.
Tests
A resource test with explicit numbers. Also a test that a value the caller
still references survives a collection and stays usable.
Alternative, rejected
Releasing eagerly in as_dict/as_list is wrong, because those methods
return handles that the caller is meant to use.
Finding F3 of CODE_IMPROVEMENT_PLAN.md.
Activity
fix(bindings): confine an evaluator to the thread that built it
nanopynix: wire bundled primops (ipaddress/jsonschema/yaml) into inpr…
fix(core): let a dropped value free its Nix root
fix(bindings): confine an evaluator to the thread that built it