Skip to content

Commit 6a506a3

Browse files
sourcehawkclaude
andcommitted
fix(skills/bounding-heavy-commands): space the lines of the memory wait by real time
BOUNDED_RUN_MEMORY_WAIT_SECONDS is now the time between two "still waiting" lines, but the wait added up the sleep time that it asked for. Under memory pressure a sleep can take much longer, so the lines came further apart and understated the time waited. The wait now reads a monotonic clock. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 7ce94ff commit 6a506a3

2 files changed

Lines changed: 20 additions & 7 deletions

File tree

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -474,15 +474,16 @@ def wait_for_memory(
474474
sleep: Callable[[float], None] = time.sleep,
475475
lock: Callable[[], None] = lambda: None,
476476
unlock: Callable[[], None] = lambda: None,
477+
clock: Callable[[], float] = time.monotonic,
477478
) -> None:
478479
"""Returns when the free memory, less the unused budgets of running commands and the headroom, holds the budget.
479480
480481
There is no time limit, so the call can block for as long as the machine is full. Returns at once when
481482
the free memory cannot be read. read_outstanding gives None where the unused budgets cannot be counted.
482483
The call returns with lock held: the caller calls unlock once the new command counts in read_outstanding.
483484
"""
484-
waited = 0.0
485-
next_line = 0.0
485+
started = clock()
486+
next_line: Optional[float] = None
486487
while True:
487488
lock()
488489
# A running command that grows or shrinks during the reads moves both values. The larger of the two unused
@@ -499,12 +500,12 @@ def wait_for_memory(
499500
if available - (outstanding or 0) - headroom_mib >= budget_mib:
500501
log(state + "; starting")
501502
return
502-
if waited >= next_line:
503-
log(state + ("; waiting" if waited == 0 else "; still waiting after %ds" % waited))
503+
waited = clock() - started
504+
if next_line is None or waited >= next_line:
505+
log(state + ("; waiting" if next_line is None else "; still waiting after %ds" % waited))
504506
next_line = waited + interval_seconds
505507
unlock()
506508
sleep(poll_seconds)
507-
waited += poll_seconds
508509

509510

510511
def cap_prefix(budget_mib: int, cpus: Optional[int], unit: str, oom_policy: bool = True) -> List[str]:

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -757,12 +757,18 @@ def setUp(self):
757757
self.log = patcher.start()
758758
self.addCleanup(patcher.stop)
759759

760-
def wait(self, values, limit, outstanding=None, headroom=0):
760+
def wait(self, values, limit, outstanding=None, headroom=0, oversleep=1):
761761
readings = list(values)
762762
sleeps = []
763+
now = [0.0]
764+
765+
def sleep(seconds):
766+
sleeps.append(seconds)
767+
now[0] += seconds * oversleep
768+
763769
bounded_run.wait_for_memory(
764770
4096, limit, 5.0, headroom_mib=headroom, read_available=lambda: readings.pop(0),
765-
read_outstanding=lambda: outstanding, sleep=sleeps.append,
771+
read_outstanding=lambda: outstanding, sleep=sleep, clock=lambda: now[0],
766772
)
767773
return sleeps
768774

@@ -790,6 +796,12 @@ def test_the_time_limit_is_the_time_between_two_lines_of_the_wait(self):
790796
self.assertEqual(len(waiting), 4, waiting)
791797
self.assertIn("still waiting", waiting[1])
792798

799+
def test_the_time_between_two_lines_is_real_time(self):
800+
self.wait([1000] * 3 + [5000], 100, oversleep=10)
801+
waiting = [line for line in self.lines() if "waiting" in line]
802+
self.assertEqual(len(waiting), 2, waiting)
803+
self.assertTrue(waiting[1].endswith("; still waiting after 100s"), waiting)
804+
793805
def test_starts_when_the_memory_is_not_readable(self):
794806
self.assertEqual(self.wait([None], 300), [])
795807
self.assertEqual(self.lines(), ["cannot read the free memory; starting"])

0 commit comments

Comments
 (0)