Repository navigation
Closed
Description
Background
The grader runs student code in a separate process and talks to it over a hand-rolled RPC: the sandbox process starts an asyncio TCP server on 127.0.0.1 (_student_code_runner.py, main/handle_client), prints host, port on stdout, and the grader connects a socket and exchanges newline-delimited JSON messages (fixture.py, _SandboxBase). This is the single largest piece of the codebase — roughly 900 lines in _student_code_runner.py plus the client half in fixture.py.
Why change it
- Hand-rolled framing. We reimplement an RPC loop (message dispatch, a 10 MB line limit, JSON encode/decode on both ends, start/query/query-function/query-setup message types) that a higher-level primitive would give us for free.
- Open TCP port with no auth. Any process in the container's network namespace can connect to the sandbox port and drive the protocol or read responses, and
socketis not in the default import blacklist. A pipe or Unix domain socket removes that exposure. - Windows-specific plumbing.
mainselects aProactorEventLoop, and there aresys.platform == "win32"branches throughout. (Note: this is also why we can't simply switch the TCP server to a Unix domain socket —asyncio.start_unix_serveris Unix-only, so that narrowing belongs with this rework rather than as a standalone change.) - Transport plumbing leaks into the design. The
host, porthandshake over stdout,HOST, the socket timeouts, and the connect/read helpers all exist only to move bytes.
Proposed direction
Replace the socket transport with multiprocessing using a Pipe (or ProcessPoolExecutor), keeping the same responsibilities:
- Privilege drop stays, via the child's
preexec_fn/ an initializer (whatdrop_privilegesdoes today in thePopenpreexec_fn). - Per-call timeout maps to
recv/future timeouts. - stdout/stderr capture of student code is unchanged.
- Serialization keeps
dillfor the grader→sandbox argument direction and the JSON codec for values coming back (see Protect grader data from student code and load data.json in images #27 for the direction constraint).
Expected payoff: delete most of the framing and handshake code, close the network-port exposure, and shrink the Windows surface.
Considerations / risks
- Cancellation and timeout semantics must still interrupt a hung student call cleanly (today a timed-out call stops the sandbox; see
SandboxStopped). preexec_fnis POSIX-only; the Windows story for the sandbox needs a decision (keep a fallback, or drop Windows support deliberately).- This is a large, behavior-sensitive change and should land on its own, after the current in-flight PRs.
Metadata
Metadata
Assignees
Labels
No labels
Activity
Replace the sandbox socket RPC with a multiprocessing pipe (#59)