fix(m0): never leave a process paused when a resume fails under the GPU cap

A resume that failed was only counted, and the process stayed suspended
after the cap ended. DutyCap now keeps what it still owes per process,
does not stack another suspend on it, retries the resume every period
and at the end, treats a raising call as a failure, and reports any
process it could not resume as left_paused (an error for the row).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
javis-bot
2026-11-10 23:56:57 +09:00
parent d0938c10f9
commit ba662ae199
3 changed files with 107 additions and 9 deletions

View File

@@ -1151,3 +1151,58 @@ def test_cap_without_the_engine_process_is_an_error_not_a_quiet_uncapped_row(tmp
r, _, paused = _tune_with_cap(tmp_path, monkeypatch, fake_server, [])
assert r["status"] == "partial" and not paused
assert any("no engine process" in e["error"] for e in r["errors"])
class _CountingProcs:
"""NtSuspendProcess/NtResumeProcess on fake processes: each suspend adds one to the process's
suspend count and each successful resume takes one off; ``fail`` says when a resume fails."""
def __init__(self, fail):
self.count: dict[int, int] = {}
self.fail = fail
self.resumes = 0
def suspend(self, pid):
self.count[pid] = self.count.get(pid, 0) + 1
return True
def resume(self, pid):
self.resumes += 1
if self.fail(pid, self.resumes):
return False
self.count[pid] -= 1
return True
def test_a_resume_that_fails_once_is_retried_and_nothing_is_left_paused():
import time as _time
procs = _CountingProcs(fail=lambda pid, n: pid == 1 and n == 1)
with gpucap.DutyCap([1, 2], 0.9, period=0.02, suspend=procs.suspend, resume=procs.resume) as d:
_time.sleep(0.2)
s = d.summary()
assert all(v == 0 for v in procs.count.values())
assert s["failed_processes"] == 1 and s["left_paused"] == 0
def test_a_process_that_cannot_be_resumed_is_reported_as_left_paused():
import time as _time
procs = _CountingProcs(fail=lambda pid, n: pid == 1)
with gpucap.DutyCap([1, 2], 0.9, period=0.02, suspend=procs.suspend, resume=procs.resume) as d:
_time.sleep(0.2)
assert procs.count[1] == 1 and procs.count[2] == 0 # paused once, never stacked
assert d.summary()["left_paused"] == 1
def test_a_suspend_or_resume_that_raises_does_not_end_the_cap_with_processes_paused():
import time as _time
procs = _CountingProcs(fail=lambda pid, n: False)
def resume(pid):
if procs.resumes == 0:
procs.resumes += 1
raise OSError("access denied")
return procs.resume(pid)
with gpucap.DutyCap([1], 0.9, period=0.02, suspend=procs.suspend, resume=resume) as d:
_time.sleep(0.2)
assert procs.count[1] == 0 and d.summary()["left_paused"] == 0