Skip to content

The agent that grows with you

fix(tui_gateway): bound the shutdown drain sleep by the time left to it

briandevanse2391845217a
diff --git a/tests/tui_gateway/test_compute_host_phase1.py b/tests/tui_gateway/test_compute_host_phase1.py
index c7cb621ceca..07e1efd5649 100644
--- a/tests/tui_gateway/test_compute_host_phase1.py
+++ b/tests/tui_gateway/test_compute_host_phase1.py
@@ -8,7 +8,7 @@ from pathlib import Path
import pytest
-from tui_gateway import server
+from tui_gateway import compute_host, server
from tui_gateway.compute_host import ComputeHost, _default_workers
from tui_gateway.host_supervisor import (
MUTATOR_ROUTE_TABLE,
@@ -175,6 +175,7 @@ def test_shutdown_drains_in_flight_turn_before_finalizing_sessions(monkeypatch):
def test_shutdown_still_finalizes_when_the_drain_deadline_expires(monkeypatch):
+ wait = 1.0
events: list[str] = []
_record_finalize(monkeypatch, events)
@@ -191,7 +192,7 @@ def test_shutdown_still_finalizes_when_the_drain_deadline_expires(monkeypatch):
try:
started = time.monotonic()
- host.shutdown(reason="sigterm", wait=1.0)
+ host.shutdown(reason="sigterm", wait=wait)
elapsed = time.monotonic() - started
finally:
release.set()
@@ -200,4 +201,50 @@ def test_shutdown_still_finalizes_when_the_drain_deadline_expires(monkeypatch):
# supervisor's SIGKILL lands on the same deadline this budget comes from,
# so the drain has to stop short and leave the finalize room to run.
assert events == ["finalize:compute_host_sigterm"]
- assert elapsed < 1.0
+ assert elapsed < wait
+
+
+def test_shutdown_drain_sleep_never_overshoots_the_reserve(monkeypatch):
+ """The drain's per-tick sleep must be bounded by the time left to it.
+
+ A flat tick overshoots the drain deadline by up to one tick, eating the
+ reserve held back for ``flush_all_sessions``; for a small ``wait`` that is
+ the whole reserve. Asserting on the *requested* sleep totals rather than on
+ wall-clock keeps this deterministic: each sleep is clamped to the remaining
+ time, so the sum can never exceed the drain budget however the scheduler
+ interleaves.
+ """
+ wait = 0.34
+ drain_budget = wait - min(compute_host._FLUSH_RESERVE_SECS, wait / 2.0)
+
+ events: list[str] = []
+ _record_finalize(monkeypatch, events)
+
+ slept: list[float] = []
+ real_sleep = time.sleep
+
+ def _recording_sleep(seconds: float) -> None:
+ slept.append(seconds)
+ real_sleep(seconds)
+
+ monkeypatch.setattr(compute_host.time, "sleep", _recording_sleep)
+
+ host = ComputeHost(stdout=io.StringIO(), heartbeat_secs=0)
+ release = threading.Event()
+ running = threading.Event()
+
+ def _stuck_turn() -> None:
+ running.set()
+ release.wait(timeout=30.0)
+
+ _register_turn(host, _stuck_turn)
+ assert running.wait(timeout=5.0)
+
+ try:
+ host.shutdown(reason="sigterm", wait=wait)
+ finally:
+ release.set()
+
+ assert events == ["finalize:compute_host_sigterm"]
+ assert slept, "the drain loop should have ticked at least once"
+ assert sum(slept) <= drain_budget + 1e-6
diff --git a/tui_gateway/compute_host.py b/tui_gateway/compute_host.py
index e9fbe93ec03..ed951fa97a1 100644
--- a/tui_gateway/compute_host.py
+++ b/tui_gateway/compute_host.py
@@ -196,12 +196,18 @@ class ComputeHost:
self._closed.set()
budget = max(0.0, wait)
deadline = time.monotonic() + budget - min(_FLUSH_RESERVE_SECS, budget / 2.0)
- while time.monotonic() < deadline:
+ while True:
+ remaining = deadline - time.monotonic()
+ if remaining <= 0:
+ break
with self._turn_futures_lock:
pending = [f for f in self._turn_futures if not f.done()]
if not pending:
break
- time.sleep(0.05)
+ # Bounded by ``remaining``: a flat 0.05s sleep would overshoot the
+ # deadline and eat into the reserve it is there to protect, which
+ # for a small ``wait`` can be the whole of it.
+ time.sleep(min(0.05, remaining))
self.flush_all_sessions(reason=reason)
self._executor.shutdown(wait=False, cancel_futures=True)