Repository navigation
Replace the sandbox socket RPC with a multiprocessing pipe - #59
Merged
Merged
Conversation
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>
…-spike # Conflicts: # src/pytest_prairielearn_grader/_student_code_runner.py # src/pytest_prairielearn_grader/fixture.py
eliotwrobson
marked this pull request as ready for review
November 18, 2026 23:09
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Spike for #57: swap the TCP-socket, JSON-lines RPC to the student-code sandbox for a
multiprocessingchild process communicating over aConnectionpipe. Opened to size the change and get a draft up, not necessarily to merge.Rebased onto
mainnow that #58 has merged; this diff is just the transport swap.What changed
spawn-contextmultiprocessing.Process(_serve) that drops privileges first, then serves requests in a blockingconn.recv()/conn.send()loop.spawn(notfork) keeps the child from inheriting the grader's memory, preserving sandbox isolation._run_sandboxandstudent_function_runnerare now synchronous: the per-call timeout isexecutor.submit(fn).result(timeout=...)instead of theasyncio.wait_forwrapper, keeping the single-workerThreadPoolExecutorconcurrency model. asyncio, the stdout host/port handshake, theHOSTconstant, and the WindowsProactorEventLoopbranch are gone.subprocesspreexec_fninto_serve(Process has nopreexec_fn);drop_privilegesitself is untouched (incl. Drop root's supplementary groups when sandboxing student code #55'ssetgroups([])).Size (
git diff --statagainstmain)Testing
grader-python-pytest:plot-assert-test: 323 pytest green.uvresolve):uv lock --check,ruff check,ruff format --check, mypy all clean.prairielearn/grader-python-pytest:latestwith--worker-username=agon the privilege-drop scenario: sandbox spawns, drops toag, grades 3/3 — confirms the privilege drop still works now that it runs inside the child rather than viapreexec_fn.Notes for reviewers
json.dumps'd). Values are now explicitly JSON-encoded in the sandbox and decoded in the grader, soNOT_SERIALIZABLEstill 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=Trueon the child means student code can no longer spawn its own processes (the oldPopenchild could). Desirable for cleanup, and the sandbox blocksmultiprocessingimports anyway, but it is a behavior change.tests/test_subtests.py's socket-timeout test was rewritten to wrap the pipeConnection.🤖 Generated with Claude Code