Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to Debian Bookworm #193

Merged
merged 3 commits into from
Sep 16, 2023
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
8 changes: 4 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# syntax=docker/dockerfile:1.4
FROM buildpack-deps:buster as builder-nsjail
FROM buildpack-deps:bookworm as builder-nsjail

WORKDIR /nsjail

Expand All @@ -17,7 +17,7 @@ RUN git clone -b master --single-branch https://github.com/google/nsjail.git . \
RUN make

# ------------------------------------------------------------------------------
FROM buildpack-deps:buster as builder-py-base
FROM buildpack-deps:bookworm as builder-py-base

ENV PYENV_ROOT=/pyenv \
PYTHON_CONFIGURE_OPTS='--disable-test-modules --enable-optimizations \
Expand All @@ -42,7 +42,7 @@ RUN git clone -b v2.3.26 --depth 1 https://github.com/pyenv/pyenv.git $PYENV_ROO
&& /build_python.sh 3.12.0rc2

# ------------------------------------------------------------------------------
FROM python:3.11-slim-buster as base
FROM python:3.11-slim-bookworm as base

ENV PIP_DISABLE_PIP_VERSION_CHECK=1 \
PIP_NO_CACHE_DIR=false
Expand All @@ -52,7 +52,7 @@ RUN apt-get -y update \
gcc \
git \
libnl-route-3-200 \
libprotobuf17 \
libprotobuf32 \
&& rm -rf /var/lib/apt/lists/*

COPY --link --from=builder-nsjail /nsjail/nsjail /usr/sbin/
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ services:
ports:
- "8060:8060"
init: true
ipc: none
ipc: private
tty: true
environment:
SNEKBOX_DEBUG: 1
Expand Down
5 changes: 3 additions & 2 deletions snekbox/nsjail.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import subprocess
import sys
from collections.abc import Generator
from contextlib import nullcontext
from pathlib import Path
from tempfile import NamedTemporaryFile
from typing import Iterable, TypeVar
Expand Down Expand Up @@ -56,7 +57,7 @@ def __init__(
memfs_home: str = "home",
memfs_output: str = "home",
files_limit: int | None = 100,
files_timeout: int | None = 5,
files_timeout: float | None = 5,
files_pattern: str = "**/[!_]*",
):
"""
Expand Down Expand Up @@ -267,7 +268,7 @@ def python3(

# Parse attachments with time limit
try:
with time_limit(self.files_timeout):
with time_limit(self.files_timeout) if self.files_timeout else nullcontext():
attachments = fs.files_list(
limit=self.files_limit,
pattern=self.files_pattern,
Expand Down
8 changes: 5 additions & 3 deletions snekbox/utils/timed.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


@contextmanager
def time_limit(timeout: int | None = None) -> Generator[None, None, None]:
def time_limit(timeout: float) -> Generator[None, None, None]:
"""
Decorator to call a function with a time limit.

Expand All @@ -25,10 +25,12 @@ def time_limit(timeout: int | None = None) -> Generator[None, None, None]:
def signal_handler(_signum, _frame):
raise TimeoutError(f"time_limit call timed out after {timeout} seconds.")

# ITIMER_PROF would be more appropriate, but SIGPROF doesn't seem to interrupt sleeps.
signal.signal(signal.SIGALRM, signal_handler)
signal.alarm(timeout)
signal.setitimer(signal.ITIMER_REAL, timeout)

try:
yield
finally:
signal.alarm(0)
# Clear the timer if the function finishes early.
signal.setitimer(signal.ITIMER_REAL, 0)
4 changes: 2 additions & 2 deletions tests/test_nsjail.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def test_file_parsing_timeout(self):
size = 32 * 1024 * 1024

with open("file", "w") as f:
for _ in range((size // 1024) - 5):
for _ in range(size // 1024):
f.write(data)

for i in range(100):
Expand All @@ -242,7 +242,7 @@ def test_file_parsing_timeout(self):
).strip()
# A value higher than the actual memory needed is used to avoid the limit
# on total file size being reached before the timeout when reading.
nsjail = NsJail(memfs_instance_size=512 * Size.MiB, files_timeout=1)
nsjail = NsJail(memfs_instance_size=128 * Size.MiB, files_timeout=0.1)
result = nsjail.python3(["-c", code])
self.assertEqual(result.returncode, None)
self.assertEqual(
Expand Down
Loading