8000 rpc: give the stdio worker a worker_start value, a client and a test · Issue #54 · nixidae/nixidae · GitHub
[go: up one dir, main page]

Skip to content

rpc: give the stdio worker a worker_start value, a client and a test #54

Closed

Description

@Lillecarl

This body replaces the original report. That report said the stdio worker
"has no client" and recommended deletion. Both halves are wrong, and the
comments below record the readings that showed it. A reader should not have to
reconstruct the position from four comments, so this states it once.

The stdio worker is a shipped feature

nanopynix/pyproject.toml:43 declares
nanopynix-worker = "nanopynix.rpc.worker._worker:main", and
grpclib_transports.ssh.connect_ssh_stdio is its client.

That is the only SSH path that reaches a machine running OpenSSH.
connect_ssh opens a bare session and expects the far end to speak H2 on it,
which only serve_ssh's in-process demo server does (_DemoSSHServer,
ssh.py:181). connect_ssh_stdio runs a command and speaks over its stdin and
stdout, and the command it runs is nanopynix-worker.
grpclib-transports/tests/test_ssh.py:117 exercises that path.

What is missing is a client inside nanopynix. rpc/client/_pool.py reaches
its worker through multiprocessing_worker_with_backchannel and through
nothing else. The original report read "no caller inside nanopynix" as "no
client", and those are two different statements.

Three reasons to build it

1. It is the one seam with no multiprocessing singleton. #129 measured
that a forked child cannot start a forkserver worker at all: ensure_running
calls os.waitpid(self._forkserver_pid, WNOHANG) on a process that is not its
child, and raises ChildProcessError. #129 added spawn as the fork-safe
value. A stdio worker execs a fresh process, so it works from any process
state with no multiprocessing involved at all.

2. It can run a different Nix build per worker. That is item 3 of #76. The
forkserver preloads once, so one process gets one build for every worker it
starts. An exec chooses per worker, which is how a worker with no Boehm
collector would be selected per session rather than per process.

3. It is the only way to set an environment variable before Nix loads.
#94 landed Session 8000 (env=...), which serves every name Nix reads after
initLibStore. It cannot serve a name Nix reads while libnixstore loads,
because the forkserver preloads nanopynix.rpc.worker._worker and the library
is therefore loaded before any worker child exists. Those names are refused,
and nanopynix._env.READ_WHILE_LIBSTORE_LOADS lists them. An exec sets the
environment before the interpreter starts.

Where it plugs in, and what already exists

NanopynixSettings.worker_start is Literal["auto", "forkserver", "spawn"]
today. "stdio" is the fourth value, and this issue is what makes it
selectable.

Two halves of the transport are already there:

  • grpclib_transports.stdio_worker_with_backchannel
    (grpclib-transports/src/grpclib_transports/stdio.py) already takes
    env: Mapping[str, str] | None, and serve_stdio_with_backchannel is beside
    it.
  • _stdio_main in rpc/worker/_worker.py still calls the plain serve_stdio,
    so it serves no backchannel and no RPC primop can reach the client.

Acceptance

  • worker_start="stdio" starts a worker with sys.executable -m nanopynix.rpc.worker._worker, and a session on it passes the same tests as
    one on forkserver.
  • _stdio_main serves the backchannel, so an RPC primop works on this start
    method too.
  • The docstring of main names nanopynix.rpc.worker._worker. It names
    nanopynix._worker today, and that module does not exist.
  • A test at the nanopynix level starts the console script and speaks to it.

Still open on the client side

  • worker_oom_score_adj. multiprocessing_worker takes on_process_start;
    the stdio worker has no equivalent hook.
  • The worker exit status that WorkerSignaledError reports. The pool reads it
    from the multiprocessing process object.
  • The namespace. It travels today as a pickled functools.partial over the
    service factory, and an exec cannot pickle: it would have to travel as argv.

Out of scope, and why

A version field on InitRequest. The original report asked for one,
because a separately started worker can be a different version from its
client. That holds for connect_ssh_stdio against a remote machine. It does
not hold for this issue: the client here always execs the same installed
package, so skew cannot occur. State that reasoning in worker.proto, so the
next reader does not derive it again.

Resolved since the original report

run_worker is gone, and worker_child_teardown now runs in the
multiprocessing child (rpc/client/_pool.py:314). The comment below that found
"the live multiprocessing worker runs no teardown at all" no longer holds.

Findings F14 and Q4/Q5 of CODE_IMPROVEMENT_PLAN.md.

Activity

  1. Lillecarl commented on Aug 5, 2026

    @Lillecarl
    ContributorAuthor

    The premise of this issue is wrong: the client exists

    This issue says the stdio worker "has no client", and recommends deletion. The
    claim holds only inside nanopynix. It does not hold for the library that
    carries the transport, and deletion would remove the far end of a shipped
    feature.

    grpclib_transports.ssh.connect_ssh_stdio is the client:

    async def connect_ssh_stdio(host: str, command: str, ...):
        """Execute *command* over SSH and speak gRPC over its stdin/stdout.
    
        This is the OpenSSH-compatible deployment mode: the remote command is a
        stdio gRPC worker process, and SSH only provides the encrypted byte stream.
        """

    grpclib-transports/tests/test_ssh.py:117 exercises it.

    SSH mode does not replace stdio. SSH mode needs stdio.

    There are two SSH client paths, and only one of them reaches a real machine:

    • connect_ssh opens a bare session, and it expects the far end to speak H2 on
      that session. The only server that does is serve_ssh, which starts an
      asyncssh server in the same process with a generated key and password
      authentication (_DemoSSHServer, ssh.py:181). A host that runs OpenSSH
      cannot answer it.
    • connect_ssh_stdio runs a command, and it speaks over the stdin and stdout
      of that command. This is the only path to a remote host, and the command that
      it runs is nanopynix-worker, which is main -> _stdio_main ->
      serve_stdio.

    So the stdio entry point is the remote half of the one SSH deployment mode that
    works against a normal machine. This issue read "no caller inside nanopynix" as
    "no client", and those are two different statements.

    What is still true, and what this issue becomes

    The evidence in the report stands on three points, and each one is a defect:

    1. The docstring of main names nanopynix._worker. That module path does not
      exist. The real path is nanopynix.rpc.worker._worker.
    2. No test at the nanopynix level starts the console script and speaks to it.
    3. The version field of InitRequest is absent, and a separately started
      worker can be a different version from its client.

    This issue therefore takes its own second branch -- "give it a client and a
    test" -- and the deletion branch is withdrawn.

    One more finding, from the same reading

    run_worker (rpc/worker/_worker.py:657) has no caller anywhere: not in
    nanopynix, not in tests/, and not in grpclib-transports. The pool reaches
    the worker through multiprocessing_worker_with_backchannel, whose child runs
    _run_multiprocessing_worker_with_backchannel.

    _shutdown_worker is reachable only from run_worker and _stdio_main. So
    the live multiprocessing worker runs no teardown at all: collector.close(),
    rpc_bridge.stop() and executor.shutdown(wait=True) never run in the child
    that the pool actually starts. The child returns from serve_h2 and exits.

    _exit_now_if_nix_will_not_stop has the same shape: it is called only from
    main(), so the guard against a stuck Nix thread applies to the stdio path
    alone. The multiprocessing child instead waits for the parent's terminate()
    and then kill().

    Refs #94, which needs the same deployment mode.

  2. changed the title [-]Resolve the stdio worker: give it a client, or delete it[/-] [+]Give the stdio worker its client and a test (do not delete it)[/+] on Aug 5, 2026
  3. Lillecarl commented on Aug 7, 2026

    @Lillecarl
    ContributorAuthor

    Two things since this was written give the stdio worker a concrete reason to exist, and a place to be selected from.

    It is the one seam with no multiprocessing singleton. #129 measures that a forked child cannot start a forkserver worker at all -- ensure_running calls os.waitpid(self._forkserver_pid, WNOHANG) on a process that is not its child, and raises ChildProcessError. #129 adds spawn as the fork-safe value. A stdio worker execs a fresh process, so it works from any process state with no multiprocessing involved.

    It can run a different Nix build per worker. That is item 3 of #76: the forkserver preloads once and copies its environment when it starts, so one process gets one build for every worker. An exec chooses per worker, which is how a non-GC worker would be selected per session rather than per process.

    Where it plugs in. #129 adds worker_start: auto | forkserver | spawn to NanopynixSettings. stdio is the third value, and the client this issue asks for is what makes it selectable.

    Refs #76, #129.

  4. Lillecarl commented on Aug 7, 2026

    @Lillecarl
    ContributorAuthor

    The load-time half of #94 is the second reason to build this client, and it names the shape.

    env on a session now serves every variable Nix reads after initLibStore. It cannot serve one Nix reads while libnixstore loads, because the forkserver preloads nanopynix.rpc.worker._worker and the library is therefore loaded before a worker child exists. Those names are refused, and nanopynix._env.READ_WHILE_LIBSTORE_LOADS lists them.

    An exec sets the environment before the interpreter starts, so a stdio worker moves that whole set out of the refusal list. It would also make Session(nix_conf=...) work on Nix 2.31, which tests/support/nix_markers.py::NIX_CONF_FILE_IGNORED currently skips three tests for.

    Two things are already in place:

    • grpclib_transports.stdio_worker_with_backchannel (grpclib-transports/src/grpclib_transports/stdio.py) already takes env: Mapping[str, str] | None, and serve_stdio_with_backchannel is beside it. _stdio_main in rpc/worker/_worker.py still calls the plain serve_stdio, so it serves no backchannel and no primop can reach the client.
    • NanopynixSettings.worker_start is the seam a third value goes in. It is a Literal["auto", "forkserver", "spawn"] today.

    What is still missing on the client side: worker_oom_score_adj (on_process_start has no stdio equivalent), the worker exit status that WorkerSignaledError reports, and the namespace path, which today travels as a pickled functools.partial and would have to travel as argv instead.

  5. changed the title [-]Give the stdio worker its client and a test (do not delete it)[/-] [+]rpc: give the stdio worker a worker_start value, a client and a test[/+] on Aug 7, 2026
  6. 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