Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/pytest_prairielearn_grader/fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import os
import shutil
import signal
import socket
import stat
import subprocess
Expand Down Expand Up @@ -40,6 +41,8 @@
# Extra time to wait for the start response, so that the sandbox's own
# initialization timeout is reported instead of racing against the socket timeout.
START_RESPONSE_GRACE = 0.5
# Seconds to wait for the sandbox's process to exit after it closes the connection, to read why it stopped.
STOPPED_PROCESS_WAIT = 1.0

# Modes for the data files folder (see _make_data_files_dir). Everyone can read the files and list the
# folders, and nobody can change them; only the owner (root, in the Docker image) can make them writable again.
Expand Down Expand Up @@ -276,13 +279,29 @@ def _read_from_socket(self) -> bytes:
except TimeoutError as e:
raise TimeoutError("Socket read timed out.") from e
if not chunk:
raise Exception("Connection closed by peer before termination character was found.")
self._server_failure = self._describe_stopped_sandbox()
self._assert_no_server_failure()
buffer.extend(chunk)
if max_len is not None and len(buffer) >= max_len:
raise Exception(f"Maximum read length of {max_len} exceeded.")
loc = len(buffer) - len(chunk) + idx + len(terminator)
return buffer[:loc]

def _describe_stopped_sandbox(self) -> str:
"""Say why the sandbox closed the connection, from how its process exited."""
return_code = None
if self.process is not None:
try:
return_code = self.process.wait(timeout=STOPPED_PROCESS_WAIT)
except subprocess.TimeoutExpired:
pass
# The kernel's out-of-memory killer, and container memory limits, stop a process with SIGKILL.
if sys.platform != "win32" and return_code == -signal.SIGKILL:
return "The sandbox running your code was killed, probably because your code used too much memory."
if return_code is not None:
return f"The sandbox running your code stopped unexpectedly with exit code {return_code}."
return "The sandbox running your code stopped unexpectedly."

def _assert_no_server_failure(self) -> None:
if self._server_failure is not None:
raise RuntimeError(self._server_failure)
Expand Down
47 changes: 47 additions & 0 deletions tests/test_sandbox_stopped.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import json
import sys

import pytest

pytest_plugins = ("pytester",)

TESTS = """
import pytest

from pytest_prairielearn_grader import ConfigObject

autograder_config = ConfigObject(import_blacklist=[])


@pytest.mark.grading_data(name="first", points=1)
def test_first(module_sandbox):
module_sandbox.query_function("stop")


@pytest.mark.grading_data(name="second", points=1)
def test_second(module_sandbox):
module_sandbox.query_function("stop")
"""


@pytest.mark.skipif(sys.platform == "win32", reason="SIGKILL is Unix-only")
@pytest.mark.parametrize(
("stop", "message"),
[
pytest.param(
"os.kill(os.getpid(), signal.SIGKILL)",
"The sandbox running your code was killed, probably because your code used too much memory.",
id="killed",
),
pytest.param("os._exit(3)", "The sandbox running your code stopped unexpectedly with exit code 3.", id="exit_code"),
],
)
def test_stopped_sandbox_is_explained(pytester: pytest.Pytester, stop: str, message: str) -> None:
pytester.path.joinpath("test_grader.py").write_text(TESTS)
pytester.mkdir("test_grader").joinpath("student_code.py").write_text(f"import os\nimport signal\n\n\ndef stop():\n {stop}\n")
pytester.runpytest("test_grader.py")
results = json.loads(pytester.path.joinpath("autograder_results.json").read_text())

for result in results["tests"]:
assert result["points"] == 0
assert message in result["message"]
Loading