8000 bindings: a Value pointer becomes a small integer when an evaluator closes beside two others · Issue #99 · nixidae/nixidae · GitHub
[go: up one dir, main page]

Skip to content

bindings: a Value pointer becomes a small integer when an evaluator closes beside two others #99

Closed

Description

@Lillecarl

The soak found a Value * that is not a Value *. UBSAN caught it, and the
process then died with SIGSEGV.

What happened

Run 30927089860,
job test-ubsan-nix_2_35
(92052107768),
commit c1f2a1b6 on ci-develop.

UBSAN reported the same address seven times, up the whole force chain, and
the last report is an SSE load that faults:

../eval.cc:1418:21: runtime error: reference binding to misaligned address 0x000000007b82 for type 'struct Value', which requires 16 byte alignment
../include/nix/expr/eval-inline.hh:98:18: runtime error: member call on misaligned address 0x000000007b82 for type 'struct Value'
../include/nix/expr/value.hh:1240:27: runtime error: member call on misaligned address 0x000000007b82
../include/nix/expr/value.hh:1211:33: runtime error: member access within misaligned address 0x000000007b82
../include/nix/expr/value.hh:1211:33: runtime error: member call on misaligned address 0x000000007b82 for type 'const struct ValueStorage'
../include/nix/expr/value.hh:828:38:  runtime error: member call on misaligned address 0x000000007b82 for type 'const struct ValueStorage'
../include/nix/expr/value.hh:706:37:  runtime error: member access within misaligned address 0x000000007b82
.../include/emmintrin.h:701:11:       runtime error: load of misaligned address 0x000000007b82 for type 'const __m128i'
Fatal Python error: Segmentation fault

0x7b82 is 31618. It is not a heap address, and UBSAN says
<memory cannot be printed> for it. So a Value * carried a small integer,
and eval.cc:1418 bound a reference to it.

What the process was doing

Three things at once, which is what makes this report worth more than the
earlier ones:

thread frame
nix-eval_0 (crashing) CoreEvalState.repl_process_line
nix-eval_0 (another) CoreEvalState.eval_string
nanopynix (main) EvalSession.close -> NixThreadExecutor.shutdown (_nix_executor.py:430)

The main thread was tearing an evaluator down while two other evaluators
evaluated. The teardown came from
test_deep_conversion_of_a_true_cycle_raises_instead_of_crashing, through
soak.py:516.

A teardown that runs beside an evaluation is the new fact. No earlier
report of this corruption had one, and the destructor of EvalState is the
one operation this repository lets leave its own thread (see the thread
confinement rule that #59 added). That makes it the first thing to look at,
and not a conclusion.

Rate

test-ubsan-nix_2_35 ran three times on ci-develop with the soak present:
runs 30916818149 and 30920512961 passed, and 30927089860 failed. The soak
deals tests to lanes from a seed, so the schedule differs between runs.

What this is not

This is not the Boehm stop-the-world abort of nixidae/nanopynix#69. That one exits 134 with
Signals delivery fails constantly at GC nixidae/nixidae#30, reports nothing, and has a
correction. This one is a real memory error, with a report and a signal.

It is the same family as the intermittent corruption that
test_inproc_parallel_batch_builds_use_multiple_store_workers shows on
GitHub runners: a value seen in a state it cannot be in, only under
concurrent evaluators, never locally. Those forms were a SIGSEGV in
nix::ExprVar::maybeThunk, an attrset that lost its type attribute, and
an InfiniteRecursionError on nixpkgs code that does not recurse.

Acceptance criteria

  • Name the operation that makes a Value * a small integer.
  • Say whether a concurrent EvalSession.close is sufficient to cause it, or
    only makes it visible.
  • Add a test that fails before the correction. The soak is the reproducer of
    last resort, and it is not a test.

Notes for whoever takes this

libexpr/eval-gc.cc calls GC_set_no_dls(1) and
GC_set_all_interior_pointers(0). Both make an embedding more likely to
lose a value than the Nix CLI is.

Already excluded by earlier work on the same family, so do not repeat them:
unregistered store threads (libstore never touches the Boehm heap), shared
mutable globals in the eval hot path (one Counter nrThunks), unrooted GC
pointers in the bindings (PyValue holds a nix::RootValue), and
EvalState's own GC data (Nix roots it with traceable_allocator).

Activity

  1. Lillecarl commented on Aug 4, 2026

    @Lillecarl
    ContributorAuthor

    It reproduces with no sanitizer, and the close hypothesis is wrong

    Run 30931403310,
    job test-daemon-nix_2_34
    (92066746861),
    commit 682cfb27. This is a plain job: no ASAN, no UBSAN, no TSAN. The
    process died with signal 11, and the step exited 139.

    tests/nanopynix/test_concurrent_soak.py::test_soak_inproc[daemon] Fatal Python error: Segmentation fault
    

    The thread layout is the same as the UBSAN report above:

    thread frame
    nix-eval_0 (crashing) CoreEvalState.repl_process_line
    nix-eval_0 (another) CoreEvalState.eval_string
    nanopynix (main) Session._forward_logs -> LogCollector.take_dropped

    The C stack names the faulting frame:

    libnixexpr.so.2.34.8, at nix::ExprVar::eval(nix::EvalState&, nix::Env&, nix::Value&)+0x15d
      nanopynix_bindings/_ext...so, at +0x58717
      nanopynix_bindings/_ext...so, at +0x540c3
    

    nix::ExprVar::eval is the same place the 2026-07-28 SIGSEGV landed, in
    test_inproc_parallel_batch_builds_use_multiple_store_workers. That report
    named nix::ExprVar::maybeThunk, its neighbour. So the soak reproduces the
    old intermittent corruption, and it does so without instrumentation.

    The concurrent EvalSession.close is not necessary. I wrote above that
    a teardown beside an evaluation was the new fact and the first thing to look
    at. This occurrence has no teardown: the main thread is forwarding log
    records. Two evaluators evaluating at once is sufficient. Withdraw that
    lead.

    What is now available that was not before

    NANOPYNIX_CORE_DEBUG=1 produced a core, and the job ran gdb over it:

    === gdb post-mortem backtrace from /tmp/core.nanopynix ===
    Core was generated by `.../python3.14 ...'
    Program terminated with signal SIGABRT, Aborted.
    #0  subtype_dealloc.lto_priv () from libpython3.14.so.1.0
    [Current thread is 1 (Thread 0x7f2051766780 (LWP 3541))]
    

    Three Boehm marker threads were parked in GC_help_marker at the time. That
    is PARALLEL_MARK: the collector was not in a stop-the-world pass, the
    markers were idle and waiting.

    Effect on the branch that found it

    tests/nanopynix/test_concurrent_soak.py is new, and it runs in the ordinary
    matrix as well as under the sanitizers. So this defect, which used to appear
    about once in a set of full runs, now has a test that reaches for it every
    run. Until this issue is closed, test-local-* and test-daemon-* can go
    red on any commit, and the cause will be this and not that commit.

    10000
  2. Lillecarl commented on Aug 4, 2026

    @Lillecarl
    ContributorAuthor

    It reproduces in a plain dev shell, and one run corrupted a value without crashing

    The soak driver reproduces this with no sanitizer and no CI runner. The
    command is one plain invocation, repeated over five seeds:

    direnv exec . timeout 300 pytest tests -m soak --soak-seed=$SEED -q --capture=no --nix-test-backends local,daemon
    

    Fifteen such runs gave 4 SIGSEGVs. Every one of them faults in the same
    frame this issue already names:

    Current thread 0x…6c0 [nix-eval_0] (most recent call first):
      … libnixexpr.so.2.34.8, at _ZN3nix7ExprVar4evalERNS_9EvalStateERNS_3EnvERNS_5ValueE+0x15d
    

    with three other nix-eval_0 threads live beside it.

    One run failed without crashing, and that run is worth more than the four
    that crashed.
    In lane 3, beside 40 other tests,
    test_a_cancelled_interruptible_operation_frees_the_evaluator raised:

    nanopynix.exceptions.NixTypeError: [NixTypeError] error:
           … while evaluating list element at index 72679
           … while evaluating list element at index 0
           error: cannot convert a function to JSON
           at «string»:1:19:
                1| builtins.genList (x: x) 12000000
                 |                   ^
    

    The caret points at the lambda. Nix says the value at index 72679 is
    x: x, and not the integer that applying x: x produces.

    That expression cannot produce a function. genList f n stores f i at
    index i, so every element forces to an integer. Checked directly, on the
    same build:

    >>> builtins.genList (x: x) 5   ->  [0, 1, 2, 3, 4]
    

    So a Value answered "function" where an integer was written into it. That
    is type confusion, and not a crash — the same shape as the attrset that
    reached lib.isDerivation having lost its type attribute, which this issue
    already records.

    Note the two nested "list element" frames as well. genList builds one flat
    list, so there is no second level for the evaluator to be inside.

    Why the non-fatal manifestation is the better lead

    A segfault gives a faulting address and a stack, and the process is gone. This
    gives a live process that keeps running, a Nix error with a source
    position, and an index into a list. Whatever wrote the wrong thing into a
    Value wrote it at a place that stays reachable, and the process survived to
    report it.

    An abort shared these runs. It is a different defect, and it is fixed

    Six of the same fifteen runs ended in SIGABRT and not SIGSEGV, with

    pthread_kill failed at suspend: errcode= 22
    

    straight after test_an_operation_that_runs_a_nix_thread_pool_still_completes.
    That is Boehm signalling a thread that already exited. This repository
    carried the patch for it, but applied the patch to the sanitized builds
    alone, so the collector that ships was the one collector still without it.
    nix/boehmgc.nix now applies the patch to every build with a collector.

    Fifteen runs of the identical command after that change:

    build runs SIGABRT SIGSEGV
    before the patch reaches plain builds 15 6 4
    after 15 0 1

    The abort is gone, and the crash of this issue remains — in ExprVar::eval
    again. So the abort was never part of this issue, and its removal makes what
    is left attributable.

  3. Lillecarl commented on Aug 4, 2026

    @Lillecarl
    ContributorAuthor

    The collector is in the causal path, and the rate says so

    Two arms of the same command, five seeds each, on the same machine, with the
    Boehm patch of #101 in place so the aborts do not confound the count:

    direnv exec . timeout 600 pytest tests -m soak --soak-seed=$SEED -q --capture=no --nix-test-backends local,daemon
    
    arm Boehm setting runs SIGSEGV
    default none 30 1
    amplified GC_FREE_SPACE_DIVISOR=64 15 5

    GC_FREE_SPACE_DIVISOR is Boehm's own knob for how much free space it keeps
    before it collects again. The default is 3, so 64 asks for far more
    collections over the same work. Nothing else differs between the two arms:
    same build, same seeds, same machine.

    3% against 33%. Fisher's exact on those counts gives p near 0.002.

    One run of the amplified arm ended in a lane timeout rather than a crash,
    which is the price of collecting that often, and it is not a finding.

    What that rules in, and what it rules out

    A crash whose rate follows the collection rate is a crash that a collection
    causes. The two shapes that fit are a live object that the collector cannot
    see and therefore reclaims, and an object handed out twice. Both give the same
    symptom: a Value that holds something other than what was written into it,
    which is the type confusion in the comment above.

    A plain data race does not fit. Collecting more often does not make two
    threads write the same word more often, and ThreadSanitizer reports nothing:
    test-tsan-nix_2_34, test-tsan-nix_2_35 and test-tsan-git all pass with
    the soak in them, in run 30940324091.

    What is already excluded

    The next measurement

    GC_DONT_GC=1 is the other half of the argument. Five runs of the default
    arm with it gave 0 crashes, which points the same way, and the arm needs
    repeating: an unrelated rebuild interrupted it after the first round.

  4. Lillecarl commented on Aug 4, 2026

    @Lillecarl
    ContributorAuthor

    The third arm closes the argument: no collection, no crash

    GC_DONT_GC=1 turns Boehm's collection off for the life of the process
    (misc.c reads the variable at init and sets GC_dont_gc). Twenty runs of
    the same command, the same five seeds, the same build:

    arm Boehm setting runs SIGSEGV
    off GC_DONT_GC=1 20 0
    default none 30 1
    amplified GC_FREE_SPACE_DIVISOR=64 15 5

    The rate is monotone in how often the collector runs, across a factor of
    about 20 in each direction. A crash that stops when collection stops, and
    gets 10 times more likely when collection gets more frequent, is a crash a
    collection causes.

    That leaves the two shapes named in the comment above: a live object the
    collector cannot see and reclaims, or an object handed out twice. Both give a
    Value holding what nobody wrote into it.

    What this does not say

    It does not say the bug is in Boehm. A missing root is a defect of the
    program that owns the pointer, and the collector is only the thing that acts
    on it. The next step is to find what holds a Value in memory the collector
    does not scan.

    Two candidates are already excluded by reading, and are written down so the
    next reader does not repeat them:

    • Nix's own roots. traceable_allocator is GC_MALLOC_UNCOLLECTABLE to
      allocate and GC_FREE to release (bdwgc gc_allocator.h), and
      uncollectable memory is still scanned. allocRootValue, baseEnvP,
      ValueVector, ValueMap and the file caches all use it.
    • ~PyValue on a foreign thread. GC_free takes the allocation lock on
      both of its paths and touches no thread-local state, so releasing a root
      from an unregistered thread is safe.
  5. Lillecarl commented on Aug 4, 2026

    @Lillecarl
    ContributorAuthor

    One more sighting, away from the four-evaluator test

    The full local suite died once at ~110 s, in tests/nanopynix/rpc/test_log_backpressure.py,
    with two nix-eval_0 threads live:

    Thread [nix-eval_0]  _objects.py:509 in eval_flake
    Thread [nix-eval_0]  _objects.py:428 in eval_string
    C stack: nix::ExprVar::eval  <- _ext
    

    So the type confusion is not tied to the four-evaluator test. Two concurrent
    evaluators are enough, and the crashing frame is the same shape: a Value
    read as the wrong type inside eval.

    Rate, so far, on this machine: 1 crash in 5 full-suite runs. Four further
    runs of the same suite, on the same build, passed with 2126 tests each, and
    the CI matrix of 23 jobs is green. So this is not a reliable reproduction, and
    it did not follow any change of that day: three full-suite runs on the build
    before it also passed.

    Recorded here because the earlier note says "the four-evaluator test", and
    that is too narrow.

  6. Lillecarl commented on Aug 5, 2026

    @Lillecarl
    ContributorAuthor

    The soak did not reproduce #99, and it found the mechanism anyway

    CI run 30966905346, three arms of five runs each, on gc-soak at 9b28f36d.

    arm knob failures
    control the shipped configuration 0 / 5
    interior GC_set_all_interior_pointers(1) 0 / 5
    amplified GC_FREE_SPACE_DIVISOR=64 1 / 5

    The interior arm settles nothing. #99 did not appear in the control arm
    either, so the soak measured no rate to compare against. The displacement
    class stays open.

    The one failure has a different signature, and it names the cause

    gc-soak (4, amplified), in test_string_arg_with_context_is_realised. It is
    not a type confusion. It is SIGABRT inside the allocator, on the main
    thread. Resolved against the exact libgc.so.1 of that job:

    GC_malloc_kind_global      <- nix::Value::mkString
    GC_generic_malloc
    GC_generic_malloc_inner+0x42f
    GC_collect_or_expand+0xb8
    GC_try_to_collect_inner+0x1c4
    GC_stopped_mark+0x27d
    GC_mark_some+0x5ff         <- after GC_push_roots
    abort
    

    The abort message is at .rodata 0x29ca0, and it reads
    Collecting from unknown thread. That is GC_push_all_stacks, in
    pthread_stop_world.c:918:

    if (!found_me && !GC_in_thread_creation)
        ABORT("Collecting from unknown thread");

    Why the main thread is unknown to Boehm

    tests/conftest.py:439 builds the evaluator directly:

    return nanopynix.EvalState(store)

    That call runs on the pytest main thread, and so does every eval_string on
    the result. Only NixThreadExecutor calls enter_evaluator_thread, which is
    the one place that calls GC_register_my_thread. The main thread therefore
    evaluates Nix, allocates Nix values, and is absent from Boehm's thread
    table.
    test_the_main_thread_does_not_own_the_collector already asserts
    this, and it passes.

    One cause, two faces

    GC_push_all_stacks walks the registered threads, pushes the stack of each
    one as a root, and aborts only when the calling thread is not among them. A
    thread that is absent and is not the caller is never visited, and its stack is
    never scanned. Two outcomes follow, and the trigger picks between them:

    1. A collection starts on the unregistered main thread. found_me stays
      false, and Boehm aborts. This is the failure above, and the amplified arm
      makes it frequent because it makes every collection frequent.
    2. A collection starts on a registered evaluator thread while the main
      thread holds values.
      found_me is true, so there is no abort. The main
      thread is not suspended and its stack is not scanned, so a value that only
      that stack refers to is unreachable, and the collector frees it. The
      next read of that pointer gives whatever took the memory.

    Outcome 2 is the symptom of this issue: a Value pointer that reads as a
    small integer. The rate follows the collection rate, which is the measurement
    already recorded in docs/collector-and-threads.md: 0 in 20 with
    GC_DONT_GC=1, 1 in 30 by default, 5 in 15 with the amplified divisor.

    I have not yet proven that outcome 2 causes this issue. The mechanism fits
    every measurement so far, and it is testable: register the thread that builds
    an EvalState, and the rate must go to zero.

    Next

    Register the calling thread with Boehm when an EvalState is constructed, so
    that a caller that never touches the executor is safe. This is the other half
    of the argument that moved the collector's owner into C++ (7f6ef4e7): owning
    the collector's thread there makes a direct EvalState caller safe from the
    first_thread bug, and this makes the same caller safe from the root-scanning
    bug. The registration needs to be released at thread exit, or it becomes the
    stale entry of #101 and #82.

  7. 53 remaining items

  8. added
    source: nanopynix
    on Oct 19, 2026
  9. 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