Skip to content

Commit a02633d

Browse files
sourcehawkclaude
andcommitted
fix(skills/bounding-heavy-commands): end the wait for a scope on a real-time deadline
The wait added up the sleep time that it asked for. On a machine under memory pressure a sleep can take much longer, so the memory lock, which blocks every wrapper, could stay held far past its 5 seconds. The wait now ends at a monotonic deadline. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent f047dac commit a02633d

2 files changed

Lines changed: 15 additions & 4 deletions

File tree

‎skills/bounding-heavy-commands/scripts/bounded_run.py‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -612,17 +612,19 @@ def _scope_counts(path: str) -> bool:
612612
return limit.isdigit()
613613

614614

615-
def wait_for_scope(parent: str, unit: str, limit_seconds: float, sleep: Callable[[float], None] = time.sleep) -> bool:
615+
def wait_for_scope(
616+
parent: str, unit: str, limit_seconds: float,
617+
sleep: Callable[[float], None] = time.sleep, clock: Callable[[], float] = time.monotonic,
618+
) -> bool:
616619
"""Returns True when the scope of the unit is in parent with its memory limit, as unused_budget_mib counts it,
617620
or False when it is not there after limit_seconds."""
618621
path = os.path.join(parent, unit + ".scope")
619-
waited = 0.0
622+
deadline = clock() + limit_seconds
620623
# systemd makes the directory of the scope before it writes the limit.
621624
while not _scope_counts(path):
622-
if waited >= limit_seconds:
625+
if clock() >= deadline:
623626
return False
624627
sleep(0.02)
625-
waited += 0.02
626628
return True
627629

628630

‎skills/bounding-heavy-commands/scripts/tests/test_bounded_run.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,6 +943,15 @@ def test_no_slice_without_a_cgroup_version_2_line(self):
943943
self.assertIsNone(bounded_run.scope_parent_directory("", "/cg"))
944944
self.assertIsNone(bounded_run.scope_parent_directory("4:memory:/user.slice/unit-1-probe.scope\n", "/cg"))
945945

946+
def test_the_wait_for_a_scope_ends_after_its_time_limit_of_real_time(self):
947+
now = [0.0]
948+
949+
def slow_sleep(seconds):
950+
now[0] += 10 * seconds
951+
952+
self.assertFalse(bounded_run.wait_for_scope(self.slice, "bounded-run-1-000000", 1.0, sleep=slow_sleep, clock=lambda: now[0]))
953+
self.assertLessEqual(now[0], 1.2)
954+
946955
def test_a_running_command_that_uses_little_of_a_large_budget_holds_back_the_next(self):
947956
self.scope("bounded-run-10-aaaaaa.scope", str(6144 * 1024 ** 2), str(1024 * 1024 ** 2))
948957
sleeps = []

0 commit comments

Comments
 (0)