Skip to content

Commit 0a003e9

Browse files
net0wellclaude
andcommitted
fix(sdk): aplica o limite de memória no próprio interpretador também no Windows
No Windows o python.exe do venv é um lançador que cria o interpretador real como processo filho; o Job Object atribuído pelo .NET logo após o Process.Start podia não alcançar esse filho (teste AC-9 instável no CI). O psrunner agora cria um Job Object para o próprio processo via ctypes antes de importar o script, como já fazia com setrlimit no Linux. O Job Object do .NET continua encerrando a árvore de processos no timeout e no cancelamento. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent d315e69 commit 0a003e9

2 files changed

Lines changed: 99 additions & 20 deletions

File tree

Lines changed: 98 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,111 @@
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+
"""
27

38
from __future__ import annotations
49

510
import sys
11+
from typing import Any
612

13+
_MB = 1024 * 1024
14+
_keep_alive: list[Any] = []
715

8-
def apply_memory_limit(limit_mb: int) -> bool:
9-
"""Limits the address space of this process (POSIX only).
1016

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."""
1419
if limit_mb <= 0:
1520
return False
1621
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
1833

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:
19103
import resource
20104

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

‎python/psrunner/tests/test_host.py‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
from __future__ import annotations
44

5-
import sys
6-
75
import pytest
86
from conftest import RunHost
97

@@ -252,10 +250,7 @@ def run(inputs, ctx):
252250
assert run.outputs["gravavel"] is False
253251

254252

255-
@pytest.mark.skipif(
256-
sys.platform == "win32", reason="Windows limits memory through a Job Object in .NET"
257-
)
258-
def test_spec002_ac10_memory_limit_on_posix(run_host: RunHost) -> None:
253+
def test_spec002_ac10_memory_limit(run_host: RunHost) -> None:
259254
script = """
260255
from psrunner import stage
261256

0 commit comments

Comments
 (0)