Skip to content

Commit 83c2de0

Browse files
sourcehawkclaude
andcommitted
fix(skills/bounding-heavy-commands): catch a stop signal anywhere in the list of rows
The handlers of --measure-rows raise at any point of the list, but the rows loop caught the signal only while a row waited, and main() caught SIGINT only. A SIGTERM, SIGHUP, or SIGQUIT between two rows, or after the list before the handlers were put back, ended the wrapper with a traceback. The loop now catches it for the whole list and keeps the first failed code, and main() catches it after the list. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 4924c83 commit 83c2de0

2 files changed

Lines changed: 28 additions & 17 deletions

File tree

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

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,24 +1035,26 @@ def bounded(options: Options, environ: Mapping[str, str]) -> int:
10351035
def _bounded_rows(options: Options, environ: Mapping[str, str]) -> int:
10361036
memory = options.memory
10371037
result = 0
1038-
for cpus in options.rows:
1039-
row = Options()
1040-
row.memory, row.cpus, row.exclusive, row.measure, row.label, row.command = (
1041-
memory, cpus, options.exclusive, True, options.label, options.command)
1042-
try:
1038+
measured = 0
1039+
try:
1040+
for cpus in options.rows:
1041+
row = Options()
1042+
row.memory, row.cpus, row.exclusive, row.measure, row.label, row.command = (
1043+
memory, cpus, options.exclusive, True, options.label, options.command)
10431044
code, suggested = bounded_once(row, environ)
1044-
except (KeyboardInterrupt, _Stopped) as stop:
1045-
# A signal while a row runs comes back as its exit code. A signal while it waits in the queue raises.
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))
1048-
if code in STOP_CODES:
1049-
log("stopped by a signal; the rows after --cpus %d are not measured" % cpus)
1050-
return result or code
1051-
if code == 0:
1052-
memory = "%dM" % suggested
1053-
else:
1054-
# A run that stopped early has a peak that is too small to be the budget of a smaller command.
1055-
result = result or code
1045+
measured += 1
1046+
if code in STOP_CODES:
1047+
log("stopped by a signal; the rows after --cpus %d are not measured" % cpus)
1048+
return result or code
1049+
if code == 0:
1050+
memory = "%dM" % suggested
1051+
else:
1052+
# A run that stopped early has a peak that is too small to be the budget of a smaller command.
1053+
result = result or code
1054+
except (KeyboardInterrupt, _Stopped) as stop:
1055+
# A signal while a row runs comes back as its exit code. A signal at any other time in the list raises.
1056+
log("stopped by a signal; %d of %d rows are measured" % (measured, len(options.rows)))
1057+
return result or 128 + (stop.signum if isinstance(stop, _Stopped) else int(signal.SIGINT))
10561058
return result
10571059

10581060

@@ -1204,6 +1206,10 @@ def main(argv: Optional[Sequence[str]] = None, environ: Optional[Mapping[str, st
12041206
except KeyboardInterrupt:
12051207
log("interrupted while waiting")
12061208
return 130
1209+
except _Stopped as stop:
1210+
# The handlers of --measure-rows can fire after its list ends, before they are put back.
1211+
log("stopped by a signal")
1212+
return 128 + stop.signum
12071213
except WrapperError as error:
12081214
log(str(error))
12091215
return EXIT_WRAPPER

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1843,6 +1843,11 @@ def once(options, environ):
18431843
self.assertEqual(calls, [8, 4])
18441844
self.assertIs(signal.getsignal(signal.SIGTERM), before)
18451845

1846+
def test_a_stop_signal_outside_a_row_ends_main_with_its_code(self):
1847+
options = ["--measure-rows", "8,4", "--", sys.executable, "-c", "pass"]
1848+
with mock.patch.object(bounded_run, "bounded", side_effect=bounded_run._Stopped(int(signal.SIGTERM))):
1849+
self.assertEqual(bounded_run.main(options, self.environ), 128 + signal.SIGTERM)
1850+
18461851
def test_an_interrupt_while_a_later_row_waits_keeps_the_first_exit_code(self):
18471852
real_once = bounded_run.bounded_once
18481853
calls = []

0 commit comments

Comments
 (0)