8000 inproc: a forced Boehm collection kills the process after other inproc tests run · Issue #82 · nixidae/nixidae · GitHub
[go: up one dir, main page]

Skip to content

inproc: a forced Boehm collection kills the process after other inproc tests run #82

Closed

Description

@Lillecarl

What

pytest tests/nanopynix/inproc kills the process. The dev shell build has the
collector, and this is not the no-collector work of #76.

scripts/run-tests.sh --wait tests/nanopynix/inproc
[pytest-agent] 10s pass=9 fail=0 done=9 tot=103 cur=tests/nanopynix/inproc/test_inproc.py::test_a_collected_value_gives_its_root_back_to_the_collector[local]
Fatal Python error: Aborted

Current thread 0x00007fffc3fff6c0 [nix-eval_0] (most recent call first):
  File "tests/nanopynix/inproc/test_inproc.py", line 206 in _collect_and_read_non_gc_bytes
  File "nanopynix/src/nanopynix/inproc/_impl.py", line 193 in _run_with_log_context
  File "nanopynix/src/nanopynix/_core/_nix_executor.py", line 98 in _run_interruptible

Line 206 is nanopynix_expr._gc_collect(). The collection runs on the
evaluator thread, which is the documented rule for that probe.

Evidence

The process dies with SIGABRT or with SIGSEGV. The signal is not stable,
and the point of death moves with the selection of tests. A signal that
changes between two runs of the same command is a sign of memory corruption,
and a forced collection is what finds it.

run selection result last test that finished
1163 tests/nanopynix/inproc SIGABRT test_a_collected_value_releases_its_nix_root
1159 tests/nanopynix/inproc SIGSEGV test_a_collected_value_releases_its_nix_root
1158 tests/meta tests/nanopynix/inproc tests/nanopynix/bindings ... SIGABRT test_inproc_value_build_raises_on_build_failure
1161 tests/nanopynix/inproc/test_inproc.py -k collected_value 2 passed —

The two collector tests pass when they run alone. They need the state that the
tests before them leave behind.

Memory is not the cause. The transient unit peaked at 437 MB.

What is ruled out

This is not the work of #76, and it is not the base environment patch. The
same command was run again at two earlier commits, with no other change:

  • f712fce5, which is the commit before the change that found this. It dies
    the same way, at the same test.
  • 5fdfb3f1, which is the commit before 2395efab ("give the base
    environment room, and check the bound"). It dies the same way. That patch
    changes BASE_ENV_SIZE and buildBindings in the shipping build with the
    collector, so it was the first suspect, and it is not the cause.

Why this matters more than one red run

The suite passes in CI today, so the ordering that produces this does not
occur there. A corruption that a forced collection finds is a corruption that
an ordinary evaluation can also find, and the suite gives it no other place to
report.

test_a_collected_value_gives_its_root_back_to_the_collector is the only
caller of _gc_collect, so it is the only test that would ever say so.

Acceptance criteria

  • The command above completes, and the result does not depend on the order of
    the tests before it.
  • The root cause is named. A change that only reorders the tests, or that
    removes the forced collection, is not a repair.

Tests

  • pytest tests/nanopynix/inproc in the dev shell, as a whole directory and
    not as one file.
  • The two collector tests keep their forced collection.

Activity

  1. Lillecarl commented on Aug 4, 2026

    @Lillecarl
    ContributorAuthor

    A selection that crashes every time, in 48 seconds

    The issue records that the point of death moves with the selection. One
    selection does not move, and it is short:

    direnv exec . pytest tests/nanopynix/test_logging.py tests/nanopynix/test_verbosity.py \
        tests/nanopynix/inproc tests/nanopynix/bindings
    

    Six runs, one after another, on ci-develop at abc3da69 with no local
    change:

    runs SIGSEGV last test that finished
    6 6 test_a_collected_value_releases_its_nix_root[local]

    Every one of the six dies at the same place: the test after that one is
    test_a_collected_value_gives_its_root_back_to_the_collector, which is the
    _gc_collect() probe this issue already names.

    tests/nanopynix/inproc on its own passes in the same shell, 101 passed. So
    the two logging modules in front of it are what makes the crash certain, and
    they are what a reader should start from.

    One earlier run of the same four paths passed, straight after the dev shell
    rebuilt. That is the only pass seen, against 6 failures in a row afterwards,
    so treat the selection as reliable rather than certain.

    Why this is worth writing down

    The reproduction in the issue needs the whole of tests/nanopynix/inproc and
    gives a different signal each time. This one is 48 seconds, gives SIGSEGV
    every time, and stops at one named test. That is enough to bisect the
    selection, to run it under a sanitizer, and to test a hypothesis in a minute
    rather than in an afternoon.

    How it was found, and what it is not

    It arrived as the failure of a change to the logger (issue #95). The control
    is the same command with that change reverted and the extension rebuilt, and
    it is the table above: the baseline crashes 6 times in 6. So the logger change
    is not the cause, and this is the defect the issue already tracks.

  2. Lillecarl commented on Aug 4, 2026

    @Lillecarl
    ContributorAuthor

    Where it faults: pthread_kill, inside Boehm's stop-the-world

    The reproduction above, under gdb. faulthandler prints nothing here, because
    the signal arrives inside a libc call that never returns, so gdb is what
    gives the frame:

    Thread 41 "nix-eval_0" received signal SIGSEGV, Segmentation fault.
    #0  __pthread_kill_implementation ()   libc.so.6
    nixidae/nixidae#30  GC_suspend_all ()                  libgc.so.1
    nixidae/nixidae#31  GC_stop_world ()                   libgc.so.1
    nixidae/nixidae#32  GC_stopped_mark ()                 libgc.so.1
    nixidae/nixidae#33  GC_try_to_collect_inner ()         libgc.so.1
    nixidae/nixidae#34  GC_try_to_collect_general ()       libgc.so.1
    nixidae/nixidae#35  GC_gcollect ()                     libgc.so.1
    nixidae/nixidae#36  _ext.cpython-314-x86_64-linux-gnu.so
    

    GC_suspend_all walks GC_threads and signals each registered thread. The
    fault is the pthread_kill itself: glibc reads the thread descriptor to get
    the tid, and the memory it reads is not there.

    This is the same call site as #101. That issue is
    pthread_kill failed at suspend: errcode= 22, an EINVAL return from the
    same line. Here glibc does not get as far as returning an errno. So the two
    issues are one signal site with two outcomes, and the patch that widens
    Boehm's tolerance to EINVAL cannot help this one: there is no return value
    to tolerate.

    What the registration bookkeeping says, and what it rules out

    NANOPYNIX_GC_THREAD_DEBUG=1 with NANOPYNIX_GC_THREAD_DEBUG_FILE set,
    over one crashing run:

    17 enter_evaluator_thread:begin
    17 enter_evaluator_thread:registered
    16 exit_evaluator_thread:begin
    16 exit_evaluator_thread:unregistered
    

    17 distinct operating-system thread ids. Every one of them enters once and
    exits once, except the last, which is the thread that called _gc_collect
    and died in it. And enter_evaluator_thread:duplicate appears zero times.

    So at the moment of the collection, Boehm knows exactly one thread, and that
    thread is the caller. GC_suspend_all skips the caller
    (THREAD_EQUAL(p->id, self)), and it skips an entry with the FINISHED flag.
    It should therefore signal nobody.

    That excludes the explanations that do not need corruption:

    • A leaked registration. Every registration has its unregistration.
    • A stale entry from a recycled pthread_t. That would show as
      GC_DUPLICATE at the next GC_register_my_thread, and
      enter_evaluator_thread logs that case. It never fired.
    • A thread that exits without the finalizer. Same evidence: the counts
      pair, per thread id.

    What is left is that GC_threads itself holds something it should not --
    Boehm's own bookkeeping, walked with the allocation lock held, giving a p
    whose id is not a live thread. GC_register_my_thread marks a thread
    DETACHED, so GC_unregister_my_thread calls GC_delete_thread, which
    frees the GC_thread struct through GC_INTERNAL_FREE. A freed struct that
    something else wrote over fits this exactly.

    That is heap corruption, which is what this issue says. The new part is that
    the collection does not die in the mark phase over a corrupt Value; it dies
    before marking starts, over Boehm's own thread table.

  3. Lillecarl commented on Aug 4, 2026

    @Lillecarl
    ContributorAuthor

    The same condition also produces the abort of #98

    The reproducer above was run four times against a collector built with
    --enable-gc-assertions (and four times against an otherwise identical
    control, to show that the rebuild is not what matters):

    collector runs crashes
    control, dontStrip, patched 4 4 SIGSEGV
    the same plus --enable-gc-assertions 4 3 SIGSEGV, 1 SIGABRT

    The abort resolves to:

    resend_lost_signals   pthread_stop_world.c:491
    GC_stop_world         pthread_stop_world.c:1115
    GC_stopped_mark       alloc.c:844
    GC_try_to_collect_inner
    GC_gcollect
    

    That is the Signals delivery fails constantly at GC #N abort of issue #98,
    in a plain dev shell with no sanitizer. So this issue, #98 and #101 are one
    condition -- Boehm cannot suspend a thread it has in GC_threads -- and the
    three signals are only which way the timing falls.

    GC_ASSERTIONS fires on nothing. Boehm's own invariants hold right up to
    the signal, so its bookkeeping is not being corrupted in a way it can notice.
    That is a point against "a stray write landed in GC_threads", and a point
    for "the entry is intact and names a thread that is gone".

    The next step is to read GC_threads at the moment of the fault. The
    assertion build carries DWARF, so gdb can walk the table and compare it with
    info threads. The run passes more often under gdb, so it needs a loop.

  4. Lillecarl commented on Aug 4, 2026

    @Lillecarl
    ContributorAuthor

    Root cause: Boehm's first_thread named a thread that had exited

    A core dump gives the state directly. GC_threads held two entries:

    bucket=2   entry=0x778fd95bf2e0 (&first_thread)  id=0x778fcbfff6c0  flags=0x6
    bucket=98  entry=0x778fd0001800                  id=0x778fabfff6c0  flags=0x2
    

    0x778fcbfff6c0 is not in info threads. Flags 0x6 is
    DETACHED | MAIN_THREAD, with FINISHED clear, so GC_suspend_all signals
    it:

    #0 __pthread_kill_implementation   nixidae/nixidae#30 GC_suspend_all (pthread_stop_world.c:983)
    nixidae/nixidae#31 GC_stop_world   nixidae/nixidae#32 GC_stopped_mark   nixidae/nixidae#35 GC_gcollect   nixidae/nixidae#36 _ext (_gc_collect)
    

    Why that entry exists

    bdwgc has one statically allocated GC_thread, first_thread.
    GC_thr_init gives it to whichever thread reaches GC_INIT() first, and
    marks it DETACHED | MAIN_THREAD. GC_delete_thread unlinks first_thread
    but never frees it, and first_thread_used is never reset. Nothing removes
    the entry at thread exit, because bdwgc installs its exit hook only for
    threads that GC_pthread_create made.

    Session.open reached GC_INIT() on a nix-store executor thread:
    inproc/_impl.py runs await executor.run(self._init_nix), and _init_nix
    calls init_libexpr() -> nix::initGC(). That pool has no
    enter/exit_evaluator_thread hooks, and it shuts down with the session. Every
    later collection then signals a thread that is gone.

    This is one condition with three outcomes

    The earlier bookkeeping evidence stays true and is not contradicted: 17
    enters, 17 exits, zero GC_DUPLICATE. Our registrations were always balanced.
    The bad entry is the one bdwgc made for itself, which our hooks never see.

    The fix, and the measurement

    nix::initGC now runs while the extension module imports, so first_thread
    is the process main thread, which lives as long as the process.

    GC_INIT() runs on crashes
    a nix-store pool thread 3 in 4
    the main thread, at import 0 in 6

    Same build, same selection, same machine:

    pytest tests/nanopynix/test_logging.py tests/nanopynix/test_verbosity.py \
        tests/nanopynix/inproc tests/nanopynix/bindings
    

    One trap, for whoever reads this next

    Moving only Boehm's own bring-up to import is worse than doing nothing. It
    leaves nix::initGC to call GC_set_all_interior_pointers(0) with
    GC_is_initialized already true, which resets the valid offsets and runs
    GC_bl_init_no_interiors() over a heap that is in use. Four evaluators then
    read a value as the wrong type, 6 runs in 6, where the unmodified build fails
    0 in 6. Move the whole of initGC, or none of it.

  5. Lillecarl commented on Aug 5, 2026

    @Lillecarl
    ContributorAuthor

    The crash no longer reproduces

    The command of this issue, run 12 times in the dev shell on e356c18a:

    pytest tests/nanopynix/inproc
    
    result runs
    no crash 12
    SIGABRT (134) 0
    SIGSEGV (139) 0

    The issue recorded SIGABRT or SIGSEGV in 3 runs of 4, on this same machine. Three of the 12 runs exited 1, and none of those is this defect: each is one assertion in test_inproc_parallel_batch_builds_use_multiple_store_workers, which measures build dispatch timing. That test is a separate finding, and it gets its own issue.

    What corrected it

    b1675f82 and 7f6ef4e7. nix::initGC reached GC_INIT() on a nix-store executor thread, so bdwgc's one static first_thread entry named a thread that the session later shut down. GC_suspend_all then signalled a thread that was gone. The collector now comes up on an owner thread that parks and never returns.

    Both commits say Refs nixidae/nixidae#82 rather than Closes, because the measurement above had not been made yet. It has now.

    What this does not close

    The multi-evaluator corruption of #99 is a different fault. It lands in nix::ExprVar::eval rather than in GC_suspend_all or pthread_kill, and it survives this fix -- run 30993492366 segfaulted in the concurrency soak on e356c18a. #99 stays open and carries that record.

    Closes #82

  6. added
    source: nanopynix
    on Oct 19, 2026
  7. added theissue type 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

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0