8000 bindings: an EvalState can be driven from any thread, and nothing says no · Issue #59 · nixidae/nixidae · GitHub
[go: up one dir, main page]

Skip to content

bindings: an EvalState can be driven from any thread, and nothing says no #59

Closed

Description

@Lillecarl

An EvalState is confined to one thread by convention only. Nothing enforces
it, and breaking it raises nothing.

Measured

An evaluator thread registers with Boehm and builds an EvalState. Another
thread then drives it:

evaluator thread: 130880799491776   this thread: 130881351686016
  attr_get + as_int: 2
  type_name: 'attrs'
  to_python: {'a': 2}
  eval_string: 2
survived

No error. The foreign thread forced thunks, allocated in the Nix heap, and
returned correct answers.

Why that is not fine

Boehm neither scans nor suspends a thread it does not know. Its own contract:
a thread must register "before a thread can allocate garbage collected memory,
or assign pointers to the garbage collected heap". A foreign thread driving an
evaluator does both. So a value reachable only from that thread's stack can be
collected under it, and a collection can run while it mutates the heap.

Which threads are registered is not obvious either. nix::initGC runs
wherever init_libexpr is called, and inproc.Session calls it on the
session's Nix thread — so the Python main thread is not registered.
Measured: GC_gcollect from the main thread is
ABORT("Collecting from unknown thread"), a core dump, which is what led to
the guard now in _gc_collect.

This is a candidate cause for the intermittent multi-evaluator corruption that
CI still shows and that has no explanation.

Not a public-API defect

The public engines are safe, and this must stay true. A Value handed to a
second thread running its own event loop works: every operation funnels
through EvalSession.run onto the executor's single thread, and
asyncio.wrap_future(future, loop=...) returns the result to whichever loop
asked. Measured: 42 and 7 read correctly from a foreign loop.

So this guards nanopynix_bindings against misuse, and guards both engines
against a future bug that routes past the executor.

Proposal

Record std::this_thread::get_id() in PyEvalState at construction. Compare
it in PyValue::evalState() — every accessor already reaches that through
checkedValue() — and in the PyEvalState methods. Throw
std::runtime_error naming both threads.

The cost is one comparison against a Nix operation. The shape copies
eval_alive, which already turns a use-after-free into an exception: this
turns silent corruption into an exception.

The destructor must be exempt. ~PyValue runs wherever the last Python
reference dies, which since #40 is usually not the evaluator thread. It only
frees a Boehm root, which needs no registration.

The owner is well defined. EvalSession.open builds the EvalState through
await self.run(...), on a NixThreadExecutor with one worker whose thread is
stable for its life. _worker_eval.py does the same for rpc.

Acceptance

  • A foreign thread calling any Value or EvalState method raises, naming
    the owning thread and the calling thread.
  • ~PyValue on a foreign thread still frees the root, and the tests from inproc: a Nix value is never released when its Python handle is collected #40
    still pass.
  • The public API keeps working across event loops — add the two-loop test
    above, because it is the property this must not break.
  • Both engines behave the same, so test_engine_parity stays quiet.
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