Repository navigation
fix(instances): stop every forwarder at once before an in-place restart - #47884
bolichen97 wants to merge 1 commit into
Conversation
An in-place restart gives its pre-exec stops one 10 s bound. The instance tunnels' stop ended its forwarders one after another, and each forwarder that ignores SIGTERM is killed only after its stop's 5 s wait. With two such forwarders, the bound cut the stop while it waited on the second one, so it was never killed and kept its local port after the restart. The manager's shutdown now stops all its tunnels together, so every forwarder gets its SIGTERM at once and its SIGKILL about 5 s later, inside the bound however many there are. The instances spec says the bound now covers any number of such forwarders, and a test drives two tunnels whose stops each wait for the other through the restart's own pre-exec run. Closes #46147
UX Review (Opus 5.5) — ⏭️ skippedRevision |
First Principles Review (Opus 5.5) — ✅ PASSPremise-level review of First-Principles-Verdict: PASS Nothing blocks. The change fixes the mechanism behind #46147: tunnels were stopped one after another, so each SIGTERM-deaf forwarder used up its own share of the 10 s bound. What this change shipsInventory (3 items) — 3 justifiedIntent: after an in-place restart, no old instance forwarder is left running with its port held, however many forwarders ignore SIGTERM. This is a FIX.
[FIRST-PRINCIPLES-REVIEWED] e18fdf8 Description read[DESCRIPTION-READ] 297d4118720df606fc3fca7c604d0c9f341e89b0143c22861abf1362b5e28d42 That sha256 names the PR description this verdict read. Recompute it with |
Design Review (Opus 5.5) — ✅ PASSDesign-level review of Design-Verdict: PASS Nothing blocks: running each tunnel's existing [DESIGN-REVIEWED] e18fdf8 Description read[DESCRIPTION-READ] 2ca3501062d94c19e9381185a71984cec5dbf6b65a7e89c26fa2d88c1000eb81 That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with |
Opus 5.5 Review — ✅ no blocking findingsReviewed Verdict parsed from the review's SHA-scoped output markers for commit False positive or not applicable? A repository writer can comment: |
GPT 6.1 Review — ✅ no blocking findingsGPT 6.1 completed its review of This comment is updated in place on each push. Review detailsNo findings. False positive or not applicable? A repository writer can comment: |
Problem / Motivation
Goal: An in-place restart ends every instance forwarder before it execs, however many of them ignore SIGTERM.
Before an in-place restart execs, Kiro Crew runs its pre-exec stops under one 10 s bound. The "Instance tunnels" stop ended its forwarders one after another, and a forwarder that does not exit on SIGTERM is killed only after its stop's 5 s wait. With two such forwarders the bound cut the stop during the second one's wait: that forwarder was never killed and kept running after the restart with its local port still bound. With three, the third was never even signalled.
Why it matters
A user with two or more Remote Crew tunnels whose forwarder hangs on SIGTERM (a
session-manager-pluginclosing a session to a stopped machine does) is left, after each in-place restart, with a forwarder process the new gateway does not reclaim, holding a port the new gateway then routes around for as long as it runs.Not a goal
What changed (motivation → approach → change)
An in-place restart no longer leaves an instance forwarder running with its local port held: every forwarder now gets its SIGKILL about 5 s into the restart's teardown, inside the 10 s bound, however many of them ignore SIGTERM.
The cause:
SshTunnelManager.shutdown()stopped the tunnels one at a time, so each SIGTERM-deaf forwarder added its own 5 s wait, and the second wait ran past the bound.shutdown()(instances/ssh_tunnel_manager_lifecycle.py) now stops all its tunnels at once withasyncio.gather, each through the samestop()as before, and still drops a tunnel only after its own stop returns, so a stop the bound cuts is retried by the exit path's run. The instances spec (docs/system-specs/modules/instances.md) says the bound now covers any number of such forwarders, and no longer lists that case as an accepted leak.Backwards compatibility
Compatible: each tunnel stops through the same
stop()steps as before; only the order changes, from one after another to all at once.Removes nothing: no field, route, setting or behaviour goes away; the restart and the gateway's exit path run the same teardown.
Tests
test/test_restart_stops_instance_tunnels.py::TestTheForwardersStopTogether::test_every_forwarder_stop_is_under_way_at_once: two tunnels whose stops each wait for the other to begin, through the realSshTunnelManager.shutdownregistered as the "Instance tunnels" pre-exec stop. A teardown that stops them in turn never finishes and the run's bound cuts it (both read not stopped); one that stops them at once finishes at once.assert {'a': False, 'b': False} == {'a': True, 'b': True}).-n0; the whole file (26 tests) 10 of 10 green at-n 4, and green in three shuffled orders (seeds 46147, 1, 20261205).Proof: PROVEN test/test_restart_stops_instance_tunnels.py::TestTheForwardersStopTogether::test_every_forwarder_stop_is_under_way_at_once (head e18fdf8)
Manual verification
Two real gateways from each tree, a crew and a hub, each with its own fresh home. The hub's
sshwas a local stand-in that ignores SIGTERM and relays each forward to the crew's port; the token mint ran against the crew. On the hub, two instances were added (POST /api/instances) and connected (POST /api/instances/<id>/connect) until both readconnected, then the hub was restarted in place (POST /api/restart) and each old forwarder's process and port were checked once it answered again.Real-machine verification
Machine: Linux test machine, Ubuntu 24.04.5 LTS, kernel 7.0, x86_64, 8 vCPU, 30 GiB, Python 3.12.3, OpenSSH 9.6p1; real
SshTunnelManagerand_SshTunnelspawn path (ssh from PATH, under Kiro Crew's own group leader) with a localsshstand-in, then the restart's ownrun_pre_exec_stops()with the production 10 s bound and 5 s waitBefore (main 0d85f41): 2 forwarders that ignore SIGTERM: cut at 10.01 s with "did not finish in 10s", 1 still running with its port bound (
ss -ltnpshows it); 3 such: 2 still running with their ports bound; 2 that exit on SIGTERM: none left, 0.0 sAfter (head e18fdf8): 2 and 3 forwarders that ignore SIGTERM: none left, no port bound, "finished in 5.0s"; 2 that exit on SIGTERM: none left, 0.0 s
Cleanup: each run stopped its own gateways by PID and killed the forwarder groups it started; no stand-in process was left after either tree, and the test homes were deleted
Screenshots / video
N/A — no user-visible frontend change: the diff touches the instances backend, its spec and a backend test.
Related Issues
Closes #46147
Sibling sweep
Swept: git grep -n "register_pre_exec_stop(" -- src/kiro_crew (each hit registers one stop; the "Instance tunnels" one, src/kiro_crew/dashboard/server_runtime/instances_lifecycle.py:186, registers
SshTunnelManager.shutdown)Sibling: src/kiro_crew/instances/ssh_tunnel_manager_lifecycle.py:862 -- changed here
Sibling: src/kiro_crew/computer_use/driver_runtime/lifecycle.py:37 -- not affected -- it stops one shared driver, no per-child loop
Sibling: src/kiro_crew/dashboard/server_runtime/browser_lifecycle.py:71 -- not affected -- it stops one supervisor
Sibling: src/kiro_crew/dashboard/server_runtime/heartbeat.py:105 -- not affected -- a watchdog task, no child process
Sibling: src/kiro_crew/dashboard/server_runtime/prevent_sleep.py:205 -- not affected -- it releases one inhibitor helper
Sibling: src/kiro_crew/slack/gateway_runtime/service_boot.py:374 -- not affected -- a ledger flush, no child process
Pattern harvest
Not generalizable: the "Instance tunnels" stop is the only pre-exec stop that ends several children, each with its own SIGTERM grace, under the seam's one bound.
Checklist
feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...); PR Hygiene does not count commits