Skip to content

Replace the TCP socket RPC to the sandbox with a multiprocessing pipe #57

Closed

Description

@eliotwrobson

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 socket is not in the default import blacklist. A pipe or Unix domain socket removes that exposure.
  • Windows-specific plumbing. main selects a ProactorEventLoop, and there are sys.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_server is Unix-only, so that narrowing belongs with this rework rather than as a standalone change.)
  • Transport plumbing leaks into the design. The host, port handshake 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 (what drop_privileges does today in the Popen preexec_fn).
  • Per-call timeout maps to recv/future timeouts.
  • stdout/stderr capture of student code is unchanged.
  • Serialization keeps dill for 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_fn is 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.
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions