fix: bound docker events shutdown in failover and rollout profiles

stop_stream terminates the events CLI, waits 10 s and kills it after
that. The rollout profile starts k6 inside the try, so a k6 start
failure no longer leaves the project events stream running.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-13 18:23:23 +03:00
co-authored by Claude Opus 5.5
parent 1758790e5c
commit 8be58ce046
+24 -10
View File
@@ -787,6 +787,17 @@ def sh(*args: str, check: bool = True) -> str:
return proc.stdout.strip() return proc.stdout.strip()
def stop_stream(proc: subprocess.Popen[bytes]) -> None:
"""Остановить поток `docker events`: SIGTERM, 10 с на выход, затем
SIGKILL — зависший CLI не держит уборку стенда."""
proc.terminate()
try:
proc.wait(timeout=10)
except subprocess.TimeoutExpired:
proc.kill()
proc.wait()
def human(n: int) -> str: def human(n: int) -> str:
"""10000 → 10k, 1000000 → 1M.""" """10000 → 10k, 1000000 → 1M."""
if n >= 1_000_000 and n % 1_000_000 == 0: if n >= 1_000_000 and n % 1_000_000 == 0:
@@ -4410,7 +4421,10 @@ class Bench:
duration = p.failover_before_s + p.failover_down_s + p.failover_after_s duration = p.failover_before_s + p.failover_down_s + p.failover_after_s
log_name = f"{phase}.log" log_name = f"{phase}.log"
# события цели — потоком с начала фазы, не из буфера демона после # события цели — потоком с начала фазы, не из буфера демона после
# неё (решение 9 design change fix-known-issues) # неё (решение 9 design change fix-known-issues). Без --since:
# подписка CLI — доли секунды, а действие — через запуск k6 и
# failover_before_s (≥ 5 с); --since потребовал бы часов хоста
# против часов ВМ, а опоздавшая подписка — громкий отказ ниже
events_path = stand.work_dir / f"{phase}-events.log" events_path = stand.work_dir / f"{phase}-events.log"
stream = stand.container_events(target_id, events_path) stream = stand.container_events(target_id, events_path)
try: try:
@@ -4479,8 +4493,7 @@ class Bench:
f"phase {phase}: replica not back: {exc}", f"phase {phase}: replica not back: {exc}",
) from exc ) from exc
finally: finally:
stream.terminate() stop_stream(stream)
stream.wait()
down = stand.down_storages() down = stand.down_storages()
if down: if down:
raise BenchError( raise BenchError(
@@ -4719,6 +4732,12 @@ class Bench:
started = time.time() started = time.time()
events_path = stand.work_dir / "events.log" events_path = stand.work_dir / "events.log"
events = stand.project_events(events_path) events = stand.project_events(events_path)
script_out = stand.work_dir / "rollout-script.out"
script_s = None
script: subprocess.Popen[bytes] | None = None
load: subprocess.Popen[bytes] | None = None
try:
# k6 — внутри try: при его сбое finally останавливает поток
load = stand.k6_console( load = stand.k6_console(
"failover.js", "failover.js",
{ {
@@ -4734,10 +4753,6 @@ class Bench:
}, },
log_name, log_name,
) )
script_out = stand.work_dir / "rollout-script.out"
script_s = None
script: subprocess.Popen[bytes] | None = None
try:
time.sleep(p.rollout_before_s) time.sleep(p.rollout_before_s)
self.say("rollout: scripts/rolling-update.sh started") self.say("rollout: scripts/rolling-update.sh started")
began = time.monotonic() began = time.monotonic()
@@ -4760,7 +4775,7 @@ class Bench:
) )
load.wait(timeout=duration + 120) load.wait(timeout=duration + 120)
finally: finally:
if load.poll() is None: if load is not None and load.poll() is None:
load.kill() load.kill()
# Ctrl+C достаётся и скрипту (та же группа процессов): он # Ctrl+C достаётся и скрипту (та же группа процессов): он
# доводит текущий шаг; стенд удаляется только после него # доводит текущий шаг; стенд удаляется только после него
@@ -4769,8 +4784,7 @@ class Bench:
script.wait(timeout=60) script.wait(timeout=60)
except subprocess.TimeoutExpired: except subprocess.TimeoutExpired:
script.kill() script.kill()
events.terminate() stop_stream(events)
events.wait(timeout=10)
text = ( text = (
script_out.read_text(encoding="utf-8", errors="replace") script_out.read_text(encoding="utf-8", errors="replace")
if script_out.exists() if script_out.exists()