Skip to content

Replace the sandbox socket RPC with a multiprocessing pipe - #59

Merged
eliotwrobson merged 7 commits into
mainfrom
claude/pipe-transport-spike
Nov 18, 2026
Merged

eliotwrobson merged 7 commits into
mainfrom
claude/pipe-transport-spike

Conversation

@eliotwrobson

@eliotwrobson eliotwrobson commented Nov 18, 2026 •

Copy link
Copy Markdown
Collaborator

Spike for #57: swap the TCP-socket, JSON-lines RPC to the student-code sandbox for a multiprocessing child process communicating over a Connection pipe. Opened to size the change and get a draft up, not necessarily to merge.

Rebased onto main now that #58 has merged; this diff is just the transport swap.

What changed

  • The sandbox now runs as a spawn-context multiprocessing.Process (_serve) that drops privileges first, then serves requests in a blocking conn.recv()/conn.send() loop. spawn (not fork) keeps the child from inheriting the grader's memory, preserving sandbox isolation.
  • _run_sandbox and student_function_runner are now synchronous: the per-call timeout is executor.submit(fn).result(timeout=...) instead of the asyncio.wait_for wrapper, keeping the single-worker ThreadPoolExecutor concurrency model. asyncio, the stdout host/port handshake, the HOST constant, and the Windows ProactorEventLoop branch are gone.
  • Privilege drop moved from subprocess preexec_fn into _serve (Process has no preexec_fn); drop_privileges itself is untouched (incl. Drop root's supplementary groups when sandboxing student code #55's setgroups([])).
  • Query/function return values still cross the pipe as JSON strings, so only JSON data — never a student-controlled object — is unpickled in the grader process. The message envelope travels as a pickled dict (enums + strings only).

Size (git diff --stat against main)

 _student_code_runner.py | 185 +++++------------
 fixture.py              | 143 +++++---------
 tests/test_subtests.py  |  16 +-
 3 files changed, 150 insertions(+), 194 deletions(-)

Testing

  • Full suite in grader-python-pytest:plot-assert-test: 323 pytest green.
  • CI-faithful lint on Python 3.13 (fresh uv resolve): uv lock --check, ruff check, ruff format --check, mypy all clean.
  • Manual grading run in prairielearn/grader-python-pytest:latest with --worker-username=ag on the privilege-drop scenario: sandbox spawns, drops to ag, grades 3/3 — confirms the privilege drop still works now that it runs inside the child rather than via preexec_fn.

Notes for reviewers

  • The old "is this value serializable" check was implicitly JSON (the whole message was json.dumps'd). Values are now explicitly JSON-encoded in the sandbox and decoded in the grader, so NOT_SERIALIZABLE still fires for un-JSON-able student values instead of crashing the sandbox on a pickle error — and no student object is ever unpickled in the grader.
  • daemon=True on the child means student code can no longer spawn its own processes (the old Popen child could). Desirable for cleanup, and the sandbox blocks multiprocessing imports anyway, but it is a behavior change.
  • tests/test_subtests.py's socket-timeout test was rewritten to wrap the pipe Connection.

🤖 Generated with Claude Code

eliotwrobson and others added 2 commits November 18, 2026 17:26
Refactor only; no behavior change. Unifies the near-identical student and
workspace implementations behind one shared, parameterized path in each layer:

- _student_code_runner.py: student_code_runner and workspace_runner now wrap a
  single _run_sandbox helper; workspace mode is selected by workspace_dir and the
  main code object to exec is supplied by a load_main callback.
- fixture.py: lift helper_modules into _SandboxBase and extract the shared
  subprocess/connect/send/read sequence into _SandboxBase._start_server.
- plugin.py: _initialize_sandbox_fixture and _initialize_workspace_fixture share a
  new _resolve_sandbox_config helper that resolves the common config.

Public API (sandbox, module_sandbox, workspace_sandbox fixtures, ConfigObject,
StudentFixture, WorkspaceFixture) is unchanged.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Issue #57: swap the TCP-socket, JSON-lines transport to the sandbox for a
multiprocessing child process and a Connection pipe.

- The sandbox runs as a spawn-context Process that drops privileges first,
  then serves requests over conn.recv()/conn.send() in a blocking loop.
  spawn (not fork) keeps the child from inheriting the grader's memory.
- The sandbox runner and function runner become synchronous: the per-call
  timeout is now executor.submit(fn).result(timeout=...) instead of the
  asyncio.wait_for wrapper, keeping the single-worker concurrency model.
- Query/function return values still cross the pipe as JSON strings, so only
  JSON data (never a student object) is unpickled in the grader process.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@eliotwrobson
eliotwrobson changed the base branch from claude/collapse-runners to main November 18, 2026 23:02
…-spike

# Conflicts:
#	src/pytest_prairielearn_grader/_student_code_runner.py
#	src/pytest_prairielearn_grader/fixture.py
@eliotwrobson
eliotwrobson marked this pull request as ready for review November 18, 2026 23:09
@eliotwrobson
eliotwrobson merged commit da01f4f into main Nov 18, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant