8000 Support a non-GC build for RPC workers, so ASAN runs in a supported configuration · Issue #76 · nixidae/nixidae · GitHub
[go: up one dir, main page]

Skip to content

Support a non-GC build for RPC workers, so ASAN runs in a supported configuration #76

Description

@Lillecarl

What

Nix builds its evaluator without the Boehm collector when -Dgc=disabled sets
NIX_USE_BOEHMGC to 0. The evaluator then allocates and never releases.
Support that build for an RPC worker process, and recycle the process instead
of collecting inside it.

Why

A worker process can afford to leak. The RPC engine already gives each
worker its own process, and the operating system reclaims the memory of a
process that exits. A recycle policy replaces the collector. The in-process
engine cannot make that trade, so this is a worker capability and not a
project-wide switch.

AddressSanitizer needs it. libexpr refuses ASAN together with the
collector, in src/libexpr/meson.build:

bdw_gc_required = get_option('gc').disable_if(
  'address' in get_option('b_sanitize'),
  error_message : 'Building with Boehm GC and ASAN is not supported',
)

#64 first built an ASAN variant that met this test only because the flag
arrived through NIX_CFLAGS_COMPILE, which meson does not read. That build
reported a tag read of a Value in value.hh on three Nix versions, which is
what a conservative collector produces when it frees an object that is still
live. The report was not usable as evidence. #64 now carries UBSan alone, and
a worker without the collector is the supported route to ASAN.

What the evaluator does without the collector

From eval-gc.hh and eval-inline.hh:

NIX_USE_BOEHMGC=1 =0
allocBytes(n) GC_MALLOC(n) calloc(n, 1)
allocValue() GC_malloc_many batch, thread-local free list allocBytes(sizeof(Value))
allocEnv(1) the same batch allocBytes(...)
traceable_allocator, gc_allocator the Boehm allocators std::allocator
GC_MALLOC_ATOMIC Boehm std::malloc
gc, gc_cleanup base classes the Boehm classes empty structs
initGC() starts and tunes Boehm does nothing

eval.cc and eval.hh contain no free, no delete[] and no ~EvalMemory.
Nothing releases a Value or an Env in either mode.

Work

  1. Guard the Boehm API in nanopynix-bindings. nix_expr.cpp calls
    GC_get_stack_base, GC_register_my_thread, GC_gcollect and the
    counters with no #if NIX_USE_BOEHMGC. The headers are absent in a non-GC
    build, so the extension does not compile today. Four functions carry all of
    it: enter_evaluator_thread and exit_evaluator_thread become no-ops,
    because there is no collector to register a thread with. _gc_collect and
    _gc_stats must report that no collector is present.

  2. Add a non-GC variant to the package scope.
    nix-expr.override { enableGC = false; } already evaluates.

  3. Choose how the worker gets that build. Two seams exist, and they are
    not equal:

    • The stdio worker (nanopynix-worker, _worker:_stdio_main) execs a
      fresh process, so it can come from a non-GC environment, and the choice
      can differ for each worker.
    • The forkserver preloads nanopynix.rpc.worker._worker
      (_pool.py:256), so each child inherits the extension that the
      forkserver loaded. The forkserver is itself a fresh exec and copies the
      environment once when it starts, so the choice is possible there, but it
      is one choice for every worker of the process.
  4. Give the worker a recycle policy. A nixpkgs evaluation allocates
    gigabytes, and no memory returns until the process exits. The trigger is
    part of this work: a count of operations, a resident-size watermark, or
    both.

  5. Add the ASAN variant on top. nix/sanitizer.nix takes a name, and
    the file says at both relevant places why address is absent today. A
    non-GC worker removes that reason.

Acceptance criteria

  • nanopynix-bindings compiles against a libexpr with NIX_USE_BOEHMGC=0.
  • An RPC worker runs the full RPC test suite against a non-GC build.
  • A worker recycles on a stated trigger, and a caller does not observe the
    recycle.
  • The in-process engine keeps the collector. A build with no collector is a
    worker capability, and tests/nanopynix/test_engine_parity.py records the
    difference or shows that no difference reaches the API.
  • An ASAN job builds and runs against the non-GC worker, with no override of
    the libexpr test quoted above.

Tests

  • A test that proves _gc_stats and _gc_collect report the absence of a
    collector rather than raising an unrelated error.
  • A test that drives a worker past its recycle trigger and proves that the
    session continues, and that the process identity changed.
  • A memory test that proves the resident size of a worker returns after a
    recycle. This is the test that says the design works.
  • The existing RPC suite, against the non-GC worker, in CI.

This blocks a decision already recorded

Q4 of docs/nanopynix/decisions.md asks whether the stdio worker has a
future, and its recommended default is to delete it: "It has an entry point,
no client, a stale docstring and no test."
This issue is the client that it
lacks, and the stdio worker is the cleaner of the two seams in item 3. Decide
Q4 and this issue together, and do not action Q4 first.

Activity

  1. Lillecarl commented on Aug 3, 2026

    @Lillecarl
    ContributorAuthor

    Item 4 (the worker recycle policy) is now #79, and this issue keeps items 1, 2, 3 and 5.

    The reason for the split is item 3, and it changes the answer this issue gives to it.

    No Python code can pick between a build with the collector and one without. multiprocessing_worker_with_backchannel passes worker_service_factory as the Process target (grpclib-transports/src/grpclib_transports/multiprocessing.py:255). The forkserver child unpickles that target, and unpickling a module-level function imports its module — so nanopynix.rpc.worker._worker, and the extension under it, is loaded before any code of ours runs in the child. Preload only moves that import earlier. And multiprocessing keeps one forkserver for each process, started from the parent's sys.path. So the choice is made once, by the venv.

    Two facts behind that:

    • EvalState carries a baseEnvP member only under NIX_USE_BOEHMGC (eval.hh:790), so the two builds are ABI-incompatible and one process must load exactly one of them, whichever mechanism selects it.
    • The compile break is small. eval-gc.hh aliases traceable_allocator and gc_allocator to std::allocator when the macro is 0, so nix::allocRootValue, PyValue and every other binding compile unchanged. Only #include <gc/gc.h> and the four functions this issue names break.

    So the non-GC build is a scope of default.nix, beside -tsan and -ubsan, and not a runtime choice. Measured: -Dgc=disabled rebuilds nix-expr, nix-flake and the bindings, and nothing else — three derivations.

    This also answers the last section of this issue. Q4 of docs/nanopynix/decisions.md asks whether the stdio worker has a future, and this issue said it would be the client that it lacks. It is not: the forkserver needed no change, and the stdio worker gained no client here. decisions.md now records that, and Q4 stands with its original recommendation.

    Acceptance criterion 4 needs no code, and here is why. It asks tests/nanopynix/test_engine_parity.py to record the difference between the engines or to show that none reaches the API. With one build for each closure, both engines lose the collector together, so there is no difference to record.

  2. Lillecarl commented on Aug 4, 2026

    @Lillecarl
    ContributorAuthor

    The ASAN job finds a real memory defect. The proof is on #64, and it is a stack-use-after-return that names the variable and the source line of the restored defect of #63. #89 records the one thing that still makes a red job ambiguous.

  3. Lillecarl commented on Aug 5, 2026

    @Lillecarl
    ContributorAuthor

    Acceptance criterion 2 is met. Run 31016955422 ran the full suite against the non-GC build on all four versions -- test-nogc-nix_2_31, test-nogc-nix_2_34, test-nogc-nix_2_35 and test-nogc-git -- and all four passed.

    What unblocked it was the base environment overflow, now closed as #81. Criterion 1 is met with it, because the suite cannot run against a build that does not compile.

    Criteria 3 and 5 stay open: the recycle policy is #79, and the ASAN job is #64 with #90 and #100 behind it.

    One correction to the last section of this issue. It cites Q4 of docs/nanopynix/decisions.md, whose recommended default is to delete the stdio worker. That default is withdrawn -- see #54 for the evidence. grpclib_transports.ssh.connect_ssh_stdio is its client, and it is the only SSH path that reaches a host running normal OpenSSH.

  4. Lillecarl commented on Aug 7, 2026

    @Lillecarl
    ContributorAuthor

    Acceptance criterion 5 is now met

    The comment above says: "Criteria 3 and 5 stay open: the recycle policy is
    #79, and the ASAN job is #64 with #90 and #100 behind it."

    #64 is closed, and #89, #90 and #100 are closed with it. Criterion 5 asks for
    "an ASAN job that builds and runs against the non-GC worker, with no override
    of the libexpr test"
    . Run
    31202037009
    on develop passes test-asan-git, test-asan-nix_2_31, test-asan-nix_2_34
    and test-asan-nix_2_35, and test-nogc-* on the same four versions.

    What is left

    criterion state
    1. the bindings compile against NIX_USE_BOEHMGC=0 met
    2. a worker runs the full RPC suite against a non-GC build met
    3. a worker recycles on a stated trigger #50
    4. the in-process engine keeps the collector met, and it needs no code
    5. an ASAN job runs against the non-GC worker met

    Only criterion 3 is left, and #79 owns it with its own acceptance criteria and
    its own tests. This issue therefore holds no work of its own.

    Refs #64, #79

  5. added
    source: nanopynix
    on Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    source: nanopynix

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0