|
1 | | -"""Resource limits applied before the user script is imported.""" |
| 1 | +"""Resource limits applied by the interpreter to itself, before the user script is imported. |
| 2 | +
|
| 3 | +The limit is applied here, inside the real interpreter process, because on Windows the venv |
| 4 | +``python.exe`` is a launcher that starts the interpreter as a child process: a Job Object assigned |
| 5 | +by the .NET runner after ``Process.Start`` can miss that child. |
| 6 | +""" |
2 | 7 |
|
3 | 8 | from __future__ import annotations |
4 | 9 |
|
5 | 10 | import sys |
| 11 | +from typing import Any |
6 | 12 |
|
| 13 | +_MB = 1024 * 1024 |
| 14 | +_keep_alive: list[Any] = [] |
7 | 15 |
|
8 | | -def apply_memory_limit(limit_mb: int) -> bool: |
9 | | - """Limits the address space of this process (POSIX only). |
10 | 16 |
|
11 | | - On Windows the .NET runner enforces the limit with a Job Object, so this is a no-op. |
12 | | - Returns ``True`` when a limit was applied. |
13 | | - """ |
| 17 | +def apply_memory_limit(limit_mb: int) -> bool: |
| 18 | + """Limits the memory of this process. Returns ``True`` when a limit was applied.""" |
14 | 19 | if limit_mb <= 0: |
15 | 20 | return False |
16 | 21 | if sys.platform == "win32": |
17 | | - return False |
| 22 | + return _limit_windows(limit_mb * _MB) |
| 23 | + else: |
| 24 | + return _limit_posix(limit_mb * _MB) |
| 25 | + |
| 26 | + |
| 27 | +if sys.platform == "win32": |
| 28 | + import ctypes |
| 29 | + from ctypes import wintypes |
| 30 | + |
| 31 | + _JOB_OBJECT_EXTENDED_LIMIT_INFORMATION = 9 |
| 32 | + _JOB_OBJECT_LIMIT_PROCESS_MEMORY = 0x00000100 |
18 | 33 |
|
| 34 | + class _BasicLimits(ctypes.Structure): |
| 35 | + _fields_ = [ |
| 36 | + ("PerProcessUserTimeLimit", ctypes.c_int64), |
| 37 | + ("PerJobUserTimeLimit", ctypes.c_int64), |
| 38 | + ("LimitFlags", wintypes.DWORD), |
| 39 | + ("MinimumWorkingSetSize", ctypes.c_size_t), |
| 40 | + ("MaximumWorkingSetSize", ctypes.c_size_t), |
| 41 | + ("ActiveProcessLimit", wintypes.DWORD), |
| 42 | + ("Affinity", ctypes.c_size_t), |
| 43 | + ("PriorityClass", wintypes.DWORD), |
| 44 | + ("SchedulingClass", wintypes.DWORD), |
| 45 | + ] |
| 46 | + |
| 47 | + class _IoCounters(ctypes.Structure): |
| 48 | + _fields_ = [ |
| 49 | + ("ReadOperationCount", ctypes.c_ulonglong), |
| 50 | + ("WriteOperationCount", ctypes.c_ulonglong), |
| 51 | + ("OtherOperationCount", ctypes.c_ulonglong), |
| 52 | + ("ReadTransferCount", ctypes.c_ulonglong), |
| 53 | + ("WriteTransferCount", ctypes.c_ulonglong), |
| 54 | + ("OtherTransferCount", ctypes.c_ulonglong), |
| 55 | + ] |
| 56 | + |
| 57 | + class _ExtendedLimits(ctypes.Structure): |
| 58 | + _fields_ = [ |
| 59 | + ("BasicLimitInformation", _BasicLimits), |
| 60 | + ("IoInfo", _IoCounters), |
| 61 | + ("ProcessMemoryLimit", ctypes.c_size_t), |
| 62 | + ("JobMemoryLimit", ctypes.c_size_t), |
| 63 | + ("PeakProcessMemoryUsed", ctypes.c_size_t), |
| 64 | + ("PeakJobMemoryUsed", ctypes.c_size_t), |
| 65 | + ] |
| 66 | + |
| 67 | + def _limit_windows(limit: int) -> bool: |
| 68 | + kernel32 = ctypes.WinDLL("kernel32", use_last_error=True) |
| 69 | + kernel32.CreateJobObjectW.argtypes = [ctypes.c_void_p, wintypes.LPCWSTR] |
| 70 | + kernel32.CreateJobObjectW.restype = wintypes.HANDLE |
| 71 | + kernel32.SetInformationJobObject.argtypes = [ |
| 72 | + wintypes.HANDLE, |
| 73 | + ctypes.c_int, |
| 74 | + ctypes.c_void_p, |
| 75 | + wintypes.DWORD, |
| 76 | + ] |
| 77 | + kernel32.SetInformationJobObject.restype = wintypes.BOOL |
| 78 | + kernel32.AssignProcessToJobObject.argtypes = [wintypes.HANDLE, wintypes.HANDLE] |
| 79 | + kernel32.AssignProcessToJobObject.restype = wintypes.BOOL |
| 80 | + kernel32.GetCurrentProcess.restype = wintypes.HANDLE |
| 81 | + |
| 82 | + job = kernel32.CreateJobObjectW(None, None) |
| 83 | + if not job: |
| 84 | + print(f"psrunner: CreateJobObject failed ({ctypes.get_last_error()})", file=sys.stderr) |
| 85 | + return False |
| 86 | + |
| 87 | + info = _ExtendedLimits() |
| 88 | + info.BasicLimitInformation.LimitFlags = _JOB_OBJECT_LIMIT_PROCESS_MEMORY |
| 89 | + info.ProcessMemoryLimit = limit |
| 90 | + applied = kernel32.SetInformationJobObject( |
| 91 | + job, _JOB_OBJECT_EXTENDED_LIMIT_INFORMATION, ctypes.byref(info), ctypes.sizeof(info) |
| 92 | + ) and kernel32.AssignProcessToJobObject(job, kernel32.GetCurrentProcess()) |
| 93 | + if not applied: |
| 94 | + print( |
| 95 | + f"psrunner: could not apply memory limit ({ctypes.get_last_error()})", |
| 96 | + file=sys.stderr, |
| 97 | + ) |
| 98 | + return False |
| 99 | + _keep_alive.append(job) |
| 100 | + return True |
| 101 | + |
| 102 | +else: |
19 | 103 | import resource |
20 | 104 |
|
21 | | - limit = limit_mb * 1024 * 1024 |
22 | | - try: |
23 | | - resource.setrlimit(resource.RLIMIT_AS, (limit, limit)) |
24 | | - except (ValueError, OSError) as exc: |
25 | | - print(f"psrunner: could not apply memory limit: {exc}", file=sys.stderr) |
26 | | - return False |
27 | | - return True |
| 105 | + def _limit_posix(limit: int) -> bool: |
| 106 | + try: |
| 107 | + resource.setrlimit(resource.RLIMIT_AS, (limit, limit)) |
| 108 | + except (ValueError, OSError) as exc: |
| 109 | + print(f"psrunner: could not apply memory limit: {exc}", file=sys.stderr) |
| 110 | + return False |
| 111 | + return True |
0 commit comments