Skip to content

Commit 0e6a215

Browse files
sourcehawkclaude
andcommitted
fix(skills/bounding-heavy-commands): keep the first failed exit code for each stop signal between the rows
The last fix covered SIGINT only. SIGTERM, SIGHUP, or SIGQUIT while a later row waited in the queue still ended the wrapper with the default action of the signal, and the failure of an earlier row was lost. --measure-rows now handles the three signals for the whole list and returns the first failed code, or 128 plus the signal. The handlers of the caller come back when the list ends. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 4e4f754 commit 0e6a215

2 files changed

Lines changed: 44 additions & 3 deletions

File tree

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

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,9 +1010,29 @@ def parse_arguments(argv: Sequence[str]) -> Options:
10101010
return options
10111011

10121012

1013+
class _Stopped(Exception):
1014+
def __init__(self, signum: int) -> None:
1015+
super().__init__(signum)
1016+
self.signum = signum
1017+
1018+
1019+
def _raise_stopped(signum: int, frame: object) -> None:
1020+
raise _Stopped(signum)
1021+
1022+
10131023
def bounded(options: Options, environ: Mapping[str, str]) -> int:
10141024
if not options.rows:
10151025
return bounded_once(options, environ)[0]
1026+
# run_command sets its own handlers while a row runs and puts these back after it.
1027+
previous = {signum: signal.signal(signum, _raise_stopped) for signum in (signal.SIGTERM, signal.SIGHUP, signal.SIGQUIT)}
1028+
try:
1029+
return _bounded_rows(options, environ)
1030+
finally:
1031+
for signum, handler in previous.items():
1032+
signal.signal(signum, handler)
1033+
1034+
1035+
def _bounded_rows(options: Options, environ: Mapping[str, str]) -> int:
10161036
memory = options.memory
10171037
result = 0
10181038
for cpus in options.rows:
@@ -1021,10 +1041,10 @@ def bounded(options: Options, environ: Mapping[str, str]) -> int:
10211041
memory, cpus, options.exclusive, True, options.label, options.command)
10221042
try:
10231043
code, suggested = bounded_once(row, environ)
1024-
except KeyboardInterrupt:
1044+
except (KeyboardInterrupt, _Stopped) as stop:
10251045
# A signal while a row runs comes back as its exit code. A signal while it waits in the queue raises.
1026-
log("interrupted while waiting; the rows from --cpus %d are not measured" % cpus)
1027-
return result or 130
1046+
log("stopped by a signal while waiting; the rows from --cpus %d are not measured" % cpus)
1047+
return result or 128 + (stop.signum if isinstance(stop, _Stopped) else int(signal.SIGINT))
10281048
if code in STOP_CODES:
10291049
log("stopped by a signal; the rows after --cpus %d are not measured" % cpus)
10301050
return result or code

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1822,6 +1822,27 @@ def test_a_stop_signal_after_a_failed_row_keeps_the_first_exit_code(self):
18221822
self.assertEqual(code, 1)
18231823
self.assertEqual(len(self.runs), 2)
18241824

1825+
def test_a_stop_signal_while_a_later_row_waits_keeps_the_first_exit_code(self):
1826+
for failure, expected in ((1, 1), (0, 128 + signal.SIGTERM)):
1827+
with self.subTest(failure=failure):
1828+
real_once = bounded_run.bounded_once
1829+
calls = []
1830+
1831+
def once(options, environ):
1832+
calls.append(options.cpus)
1833+
if len(calls) == 2:
1834+
os.kill(os.getpid(), signal.SIGTERM)
1835+
time.sleep(5)
1836+
return real_once(options, environ)
1837+
1838+
before = signal.getsignal(signal.SIGTERM)
1839+
self.runs = []
1840+
with mock.patch.object(bounded_run, "bounded_once", once):
1841+
code = self.run_bounded(["--measure-rows", "8,4,2"], [(failure, 1000)])
1842+
self.assertEqual(code, expected)
1843+
self.assertEqual(calls, [8, 4])
1844+
self.assertIs(signal.getsignal(signal.SIGTERM), before)
1845+
18251846
def test_an_interrupt_while_a_later_row_waits_keeps_the_first_exit_code(self):
18261847
real_once = bounded_run.bounded_once
18271848
calls = []

0 commit comments

Comments
 (0)