Spaces:
Running on Zero
Running on Zero
Upload sandbox.py with huggingface_hub
Browse files- sandbox.py +19 -3
sandbox.py
CHANGED
|
@@ -49,7 +49,10 @@ RUN_TIMEOUT = int(os.getenv("RUN_TIMEOUT", "60"))
|
|
| 49 |
|
| 50 |
# POSIX-only resource ceilings.
|
| 51 |
MAX_CPU_SECONDS = int(os.getenv("MAX_CPU_SECONDS", "60"))
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
| 53 |
MAX_OUTPUT_CHARS = 20_000
|
| 54 |
|
| 55 |
|
|
@@ -142,8 +145,21 @@ def _preexec():
|
|
| 142 |
mem = MAX_MEMORY_MB * 1024 * 1024
|
| 143 |
resource.setrlimit(resource.RLIMIT_AS, (mem, mem))
|
| 144 |
resource.setrlimit(resource.RLIMIT_FSIZE, (64 * 1024 * 1024, 64 * 1024 * 1024))
|
| 145 |
-
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
# New process group so a timeout kill takes any children with it.
|
| 148 |
os.setsid()
|
| 149 |
|
|
|
|
| 49 |
|
| 50 |
# POSIX-only resource ceilings.
|
| 51 |
MAX_CPU_SECONDS = int(os.getenv("MAX_CPU_SECONDS", "60"))
|
| 52 |
+
# rustc with -C lto=fat is memory-hungry; 2 GB was tight enough to risk killing
|
| 53 |
+
# legitimate compiles. The free Spaces container has 16 GB, so this is generous
|
| 54 |
+
# without being unbounded.
|
| 55 |
+
MAX_MEMORY_MB = int(os.getenv("MAX_MEMORY_MB", "4096"))
|
| 56 |
MAX_OUTPUT_CHARS = 20_000
|
| 57 |
|
| 58 |
|
|
|
|
| 145 |
mem = MAX_MEMORY_MB * 1024 * 1024
|
| 146 |
resource.setrlimit(resource.RLIMIT_AS, (mem, mem))
|
| 147 |
resource.setrlimit(resource.RLIMIT_FSIZE, (64 * 1024 * 1024, 64 * 1024 * 1024))
|
| 148 |
+
|
| 149 |
+
# RLIMIT_NPROC counts processes per *user*, not per process tree, so it
|
| 150 |
+
# includes everything the container is already running under this uid. A
|
| 151 |
+
# tight cap here made rustc fail to spawn its own threads:
|
| 152 |
+
# Os { code: 11, kind: WouldBlock, message: "Resource temporarily unavailable" }
|
| 153 |
+
# Set high enough that a compiler's thread pool always fits, while still
|
| 154 |
+
# bounding a runaway fork loop. Never lower than the existing soft limit,
|
| 155 |
+
# since raising it back is not permitted for an unprivileged process.
|
| 156 |
+
try:
|
| 157 |
+
soft, hard = resource.getrlimit(resource.RLIMIT_NPROC)
|
| 158 |
+
target = 4096 if hard == resource.RLIM_INFINITY else min(4096, hard)
|
| 159 |
+
if soft == resource.RLIM_INFINITY or target > soft:
|
| 160 |
+
resource.setrlimit(resource.RLIMIT_NPROC, (target, hard))
|
| 161 |
+
except (ValueError, OSError):
|
| 162 |
+
pass # not fatal - CPU time, memory, and the wall-clock kill still apply
|
| 163 |
# New process group so a timeout kill takes any children with it.
|
| 164 |
os.setsid()
|
| 165 |
|