|
14 | 14 | import tempfile |
15 | 15 | import time |
16 | 16 | from contextlib import contextmanager |
| 17 | +from contextlib import suppress |
17 | 18 | from textwrap import dedent |
18 | 19 |
|
19 | 20 | import uuid_utils.compat as uuid |
20 | 21 | from django.utils.translation import gettext_lazy as _ |
21 | | - |
22 | 22 | from common.utils.logger import maxkb_logger |
23 | 23 | from maxkb.const import BASE_DIR, CONFIG |
24 | 24 | from maxkb.const import PROJECT_DIR |
@@ -242,17 +242,18 @@ def _exec(self, execute_file): |
242 | 242 | kwargs = {'cwd': BASE_DIR, 'env': { |
243 | 243 | 'LD_PRELOAD': f'{_sandbox_path}/lib/sandbox.so', |
244 | 244 | }} |
| 245 | + def _set_resource_limit(): |
| 246 | + if not _enable_sandbox or not sys.platform.startswith("linux"): return |
| 247 | + with suppress(Exception): resource.setrlimit(resource.RLIMIT_AS, (_process_limit_mem_mb * 1024 * 1024,) * 2) |
| 248 | + with suppress(Exception): os.sched_setaffinity(0, set(random.sample(list(os.sched_getaffinity(0)), _process_limit_cpu_cores))) |
245 | 249 | try: |
246 | 250 | subprocess_result = subprocess.run( |
247 | 251 | [sys.executable, execute_file], |
248 | 252 | timeout=_process_limit_timeout_seconds, |
249 | 253 | text=True, |
250 | 254 | capture_output=True, |
251 | 255 | **kwargs, |
252 | | - preexec_fn=(lambda: None if (not _enable_sandbox or not sys.platform.startswith("linux")) else ( |
253 | | - resource.setrlimit(resource.RLIMIT_AS, (_process_limit_mem_mb * 1024 * 1024,) * 2), |
254 | | - os.sched_setaffinity(0, set(random.sample(list(os.sched_getaffinity(0)), _process_limit_cpu_cores))) |
255 | | - )) |
| 256 | + preexec_fn=_set_resource_limit |
256 | 257 | ) |
257 | 258 | return subprocess_result |
258 | 259 | except subprocess.TimeoutExpired: |
|
0 commit comments