Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Keep the killed-by-signal exit status when release() fails (#9072)

Daniel Han530799241698
diff --git a/.github/scripts/kaggle_t4_ci/launch.py b/.github/scripts/kaggle_t4_ci/launch.py
index cfdd01d92cb..afa5f2f9e21 100644
--- a/.github/scripts/kaggle_t4_ci/launch.py
+++ b/.github/scripts/kaggle_t4_ci/launch.py
@@ -936,7 +936,26 @@ def _install_release_handlers(release: Callable[[], None]) -> None:
def _release_and_die(signum, _frame):
_log(f"received signal {signum}; deleting kernels before exiting")
- release()
+ try:
+ release()
+ except BaseException as exc: # noqa: BLE001
+ # A raise here used to propagate into the main thread, where main()'s
+ # `except BaseException` caught it and finish() called release() again.
+ # A transient first failure (the observed one: an OSError from a
+ # subprocess spawn on a loaded runner) therefore ended in main
+ # RETURNING 0, and the cancelled job read as completed. Deleting is
+ # best effort, the exit status is not. Retried once, since the kernel
+ # is billing meanwhile.
+ _log(f"release() failed under signal {signum}: {type(exc).__name__}: {exc}")
+ try:
+ release()
+ except BaseException as retry_exc: # noqa: BLE001
+ # Nothing more to try in-process: the slug stays in the registry
+ # for the next launch's orphan sweep, as after a kill -9.
+ _log(
+ f"release() failed again: {type(retry_exc).__name__}: {retry_exc}. "
+ f"The kernels stay in the registry for the next launcher's sweep"
+ )
# Die of the original signal rather than exiting 0, so the status
# still reads "killed by signal N". A CI system treats a 0 from a
# cancelled job as a completed one.
diff --git a/tests/kaggle/test_launch_cleanup.py b/tests/kaggle/test_launch_cleanup.py
index 304d78b5807..594ceb55743 100644
--- a/tests/kaggle/test_launch_cleanup.py
+++ b/tests/kaggle/test_launch_cleanup.py
@@ -75,6 +75,18 @@ def _runner(tmp_path: Path, body: str) -> subprocess.Popen:
)
+# A guard against a launcher that never dies, not a latency target: four of these
+# subprocess tests now run at once on a four-core runner, where the SIGINT case
+# was observed to need over 30 seconds purely to be scheduled.
+_DEATH_BUDGET_SEC = 120
+
+# How long the launcher stalls while a test signals it. Must OUTLAST the budget
+# above: a handler that swallows its signal leaves the process asleep and then
+# resuming, so a shorter stall lets it wake, run finish(), delete through the
+# ordinary path and exit inside the wait, passing every deletion test.
+_STALL_SEC = 900
+
+
def _await_ready(proc: subprocess.Popen) -> None:
"""Wait for the runner to say it is where the test wants it, past whatever
the launcher logged on the way there."""
@@ -364,7 +376,7 @@ def _waiting_launcher(outdir: Path) -> str:
" launch.push = _push",
" def _wait(*a, **kw):",
" print('READY', flush=True)",
- " time.sleep(60)",
+ " time.sleep(%d)" % _STALL_SEC,
" launch.wait = _wait",
" sys.argv = ['launch.py', '--notebook', 'a.ipynb', '--user', 'me',",
f" '--outdir', {str(outdir)!r}]",
@@ -382,7 +394,7 @@ def test_a_signalled_launcher_deletes_its_kernels(tmp_path, signame):
try:
_await_ready(proc)
proc.send_signal(getattr(signal, signame))
- proc.wait(timeout = 30)
+ proc.wait(timeout = _DEATH_BUDGET_SEC)
finally:
if proc.poll() is None:
proc.kill()
@@ -391,6 +403,12 @@ def test_a_signalled_launcher_deletes_its_kernels(tmp_path, signame):
), f"{signame} left the kernel behind; it would bill to its ceiling"
# And the registry agrees, so no later sweep chases a kernel that is gone.
assert json.loads((tmp_path / "inflight.json").read_text()) == []
+ # On the signal path, not on the way out of an ordinary run: finish() satisfies
+ # the deletion above on its own, so a swallowed signal would pass without this.
+ assert proc.returncode == -getattr(signal, signame), (
+ f"the kernel was deleted, but the launcher exited {proc.returncode} rather than "
+ f"dying of {signame}, so nothing here says the signal is what did it"
+ )
def test_the_exit_status_still_says_it_was_killed(tmp_path):
@@ -400,7 +418,7 @@ def test_the_exit_status_still_says_it_was_killed(tmp_path):
try:
_await_ready(proc)
proc.send_signal(signal.SIGTERM)
- proc.wait(timeout = 30)
+ proc.wait(timeout = _DEATH_BUDGET_SEC)
finally:
if proc.poll() is None:
proc.kill()
@@ -409,6 +427,64 @@ def test_the_exit_status_still_says_it_was_killed(tmp_path):
), f"expected death by SIGTERM, got returncode {proc.returncode}"
+def test_the_exit_status_survives_a_release_that_fails(tmp_path):
+ """The way the status above was actually observed to come back 0.
+
+ A delete raising inside the handler propagated into the main thread, where
+ main() catches BaseException and reaches release() by RETURNING. A first
+ delete that failed and a second that worked -- a transient OSError from a
+ subprocess spawn on a loaded runner -- therefore turned a cancelled run into
+ `exit 0`. The retry still deletes the kernel; what this pins is that the exit
+ status does not depend on the delete having worked.
+ """
+ proc = _runner(
+ tmp_path,
+ _waiting_launcher(tmp_path / "out").replace(
+ " launch.main()",
+ "\n".join(
+ [
+ " _calls = []",
+ " _real_delete = launch.delete_kernel",
+ " def _flaky_delete(slug):",
+ " _calls.append(slug)",
+ " if len(_calls) == 1:",
+ " raise OSError(11, 'Resource temporarily unavailable')",
+ " return _real_delete(slug)",
+ " launch.delete_kernel = _flaky_delete",
+ " launch.main()",
+ ]
+ ),
+ ),
+ )
+ try:
+ _await_ready(proc)
+ proc.send_signal(signal.SIGTERM)
+ proc.wait(timeout = _DEATH_BUDGET_SEC)
+ finally:
+ if proc.poll() is None:
+ proc.kill()
+ assert proc.returncode == -signal.SIGTERM, (
+ f"a release() that raised turned SIGTERM into returncode {proc.returncode}; "
+ f"a cancelled job would read as a completed one"
+ )
+ # And the retry still did the budget control the handler exists for.
+ assert any("me/k-1" in c for c in _deletions(tmp_path))
+
+
+def test_the_stall_outlasts_the_death_budget():
+ """The relationship the signal tests rest on, asserted rather than assumed.
+
+ If the sleep is the shorter of the two, a launcher that ignored its signal
+ wakes up, finishes normally and exits inside the wait, so the tests pass on
+ the behaviour they forbid. Tuning one number without the other is silent.
+ """
+ assert _STALL_SEC > _DEATH_BUDGET_SEC, (
+ f"a launcher that swallows its signal wakes after {_STALL_SEC}s and exits "
+ f"normally inside the {_DEATH_BUDGET_SEC}s wait, so the signal tests would "
+ f"pass without any signal handling at all"
+ )
+
+
def test_an_unhandled_exception_still_deletes(tmp_path):
"""atexit covers the path no signal handler sees.
@@ -430,7 +506,7 @@ def test_an_unhandled_exception_still_deletes(tmp_path):
raise RuntimeError("boom")
""",
)
- proc.wait(timeout = 30)
+ proc.wait(timeout = _DEATH_BUDGET_SEC)
assert any("me/k-1" in c for c in _deletions(tmp_path))
assert json.loads((tmp_path / "inflight.json").read_text()) == []
@@ -444,13 +520,13 @@ def test_kill_9_leaves_it_for_the_sweep(tmp_path):
f"""
launch._inflight_add("me/k-9")
print("READY", flush=True)
- time.sleep(60)
+ time.sleep({_STALL_SEC})
""",
)
try:
assert proc.stdout.readline().strip() == "READY"
proc.send_signal(signal.SIGKILL)
- proc.wait(timeout = 30)
+ proc.wait(timeout = _DEATH_BUDGET_SEC)
finally:
if proc.poll() is None:
proc.kill()
@@ -569,7 +645,7 @@ def test_a_kernel_pushed_before_the_signal_is_still_deleted(tmp_path):
" if len(calls) == 1:",
" return {'ok': True, 'slug': slug, 'attempts': attempted}",
" print('READY', flush=True)",
- " time.sleep(60)",
+ " time.sleep(%d)" % _STALL_SEC,
" launch.push = _push",
" sys.argv = ['launch.py', '--notebook', 'a.ipynb', '--notebook', 'b.ipynb',",
f" '--user', 'me', '--outdir', {str(tmp_path / 'out')!r}]",
@@ -580,7 +656,7 @@ def test_a_kernel_pushed_before_the_signal_is_still_deleted(tmp_path):
try:
_await_ready(proc)
proc.send_signal(signal.SIGTERM)
- proc.wait(timeout = 30)
+ proc.wait(timeout = _DEATH_BUDGET_SEC)
finally:
if proc.poll() is None:
proc.kill()