Repository navigation
Description
nanopynix.EvalState(store) aborts the process when init_libexpr() has not
run, and #93 left open what a forked child may do next. This issue carries
the contract, per engine, and the tests for it.
What the measurements say
A forked child has an initialised collector and no owner thread.
init_gc_on_the_owner_thread (nix_expr.cpp:1752) spawns a detached
nix-gc-owner thread that parks for ever and is Boehm's first_thread.
Measured in a child: init_libexpr() returns as a no-op, because
std::call_once is already tripped. So the child carries
gcInitialised == true and a thread table naming a tid that does not exist.
That is #82, #98 and #101 by construction.
Boehm is not the blocker, and its fork support does not help.
GC_set_handle_fork(1) before GC_INIT installs pthread_atfork handlers
whose child hook calls GC_remove_all_threads_but_me()
(bdwgc pthread_support.c:1316), which purges exactly that entry. What
blocks a forked inproc session is everything else the child inherits:
- a
daemon://orssh-ng://store holds a pooled socket, and two processes
on one nix-daemon socket desync exactly as the rpc worker pipe does in Session is not fork-safe: both engines are silently corrupted by a fork() #93; - the eval cache is sqlite, whose handles are unsafe across a fork;
- every evaluator is thread-confined by
PyEvalState::checkThreadto a
thread the fork did not keep, and everyNixThreadExecutorpool is empty.
The child would have to rebuild the store, rebuild every executor and re-bind
every evaluator, which is more work than opening a new session, for warm
state that cannot cross. gc.h:498 also warns that the handlers are "not
fully POSIX-compliant". Do not enable it.
init_libexpr() makes the process multi-threaded, and Python says so.
Measured: os.fork() after it emits Python 3.14's DeprecationWarning: This process is multi-threaded, use of fork() may lead to deadlocks in the child.
import nanopynix alone does not.
#64 left a hole in _InprocProcessGuard. release clears
_active_session and _active_pid, and never clears
_initialization_signature. acquire only refuses on a live inherited
session, and it runs before mark_initialized. So open, close, fork, and the
child's guard is empty: acquire passes and the child opens a session over
the poisoned collector. The comment at inproc/_impl.py:151 calls that case
"a second corruption", and the guard does not catch it.
rpc fails in a forked child for a reason that is in CPython.
multiprocessing.forkserver.ForkServer carries no pid guard and no
register_after_fork, so ensure_running's
os.waitpid(self._forkserver_pid, WNOHANG) (forkserver.py:134) raises.
resource_tracker handles the same case (resource_tracker.py:226).
Measured, in a forked child:
spawn -> start() succeeded
forkserver -> ChildProcessError: [Errno 10] No child processes
What the preload buys, 5 workers each, with
preload=['nanopynix.rpc.worker._worker']:
forkserver 7.7 ms per worker
spawn 72.1 ms per worker
The contract
inproc: refused for the life of the process, once Nix is initialised in
that address space, whether or not a session is still open. "Fork first, then
open" stays supported, because a parent that never touched Nix leaves the
guard empty.
rpc: supported. A forked child opens a new Session and gets a worker of
its own. worker_start selects the mechanism, and auto picks spawn when
this process is a fork.
Work
PyEvalState::initcallsinit_gc_on_the_owner_thread()before
nanopynix_ensure_gc_thread_registered(), declared in
nanopynix_gc_thread.hh. A thread cannot register with a collector that
has not started, andgc_register_this_thread_unowned
(nix_expr.cpp:1840) already refuses for that reason. This closes bindings: constructing an EvalState in a forked child aborts, which blocks the fork route for a no-collector build #83._InprocProcessGuardgains an_initialized_pid, set by
mark_initializedand never cleared, checked inacquirebefore the
lock. ReuseForkedSessionError.process_is_forked()innanopynix/_fork.py: the pid at import plus the
flag the existingafter_in_childhook sets. No CPython private state.get_worker_context(method, preload)ingrpclib-transports, and
worker_start: auto | forkserver | spawnonNanopynixSettings.docs/nanopynix/api/process.mdstates the contract above.
Acceptance criteria
nanopynix.EvalState(store)constructs in a process where nothing called
init_libexpr(), and the ordering-only fixtures attest_flake.py:150
and:164come out.- A parent that opened and closed an inproc session refuses a forked child.
- A parent that never touched Nix forks, and the child opens an inproc
session that works. - A forked child opens an rpc
Sessionthat answers.
Tests
All in tests/nanopynix/test_fork_safety.py, which already carries the fork
harness and pytest.mark.forks_the_process. A new module would silently join
the concurrency soak roster.
One test for each acceptance criterion, plus a unit test that auto resolves
to spawn in a fork and to forkserver otherwise, and one that
worker_start="spawn" works with no fork at all.
Activity
fix(nanopynix): give the evaluator a collector, and fork a contract p…