Skip to content

fix(instances): stop every forwarder at once before an in-place restart - #47884

Open
bolichen97 wants to merge 1 commit into
mainfrom
fix/46147-tunnel-stop-sigkill
Open

bolichen97 wants to merge 1 commit into
mainfrom
fix/46147-tunnel-stop-sigkill

Conversation

@bolichen97

Copy link
Copy Markdown
Contributor

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-plugin closing 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

  • A restart that lands while a connect holds the manager's lock across a slow tunnel start (the instances spec names that as separate work).
  • Changing how one tunnel stops (SIGTERM, the 5 s wait, then SIGKILL) or the 10 s bound.

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 with asyncio.gather, each through the same stop() 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 real SshTunnelManager.shutdown registered 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.
  • Red before: on main with only this test file added, the new test fails by its assertion once the restart's 10 s bound cuts the teardown (assert {'a': False, 'b': False} == {'a': True, 'b': True}).
  • Determinism (testing-conventions D12): the new test 20 of 20 runs green at -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 ssh was 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 read connected, then the hub was restarted in place (POST /api/restart) and each old forwarder's process and port were checked once it answered again.

  • main (0d85f41): the hub logged "Instance tunnels stop before restart did not finish in 10s; restarting anyway" and the restart took 15.3 s; afterwards the second instance's old forwarder was still running and still held its local port.
  • this branch (e18fdf8): no warning, the tunnels were shut down 5 s into the restart, restart 10.6 s; neither old forwarder was running, and the first instance's port was held by the new forwarder the restarted hub started when it reconnected.

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 SshTunnelManager and _SshTunnel spawn path (ssh from PATH, under Kiro Crew's own group leader) with a local ssh stand-in, then the restart's own run_pre_exec_stops() with the production 10 s bound and 5 s wait
Before (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 -ltnp shows it); 3 such: 2 still running with their ports bound; 2 that exit on SIGTERM: none left, 0.0 s
After (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

  • A Conventional Commits title (feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...); PR Hygiene does not count commits
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable)
  • No secrets, credentials, or internal references in the diff

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
@github-actions

github-actions Bot commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5) — ⏭️ skipped

Revision e18fdf87ec7a6990cac91ec45536d838ea185b25 touches no user-facing surface (no changes under website/ or committed screenshots), so the UX review was skipped. Does not block merge.

@github-actions

github-actions Bot commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5) — ✅ PASS

Premise-level review of e18fdf87ec7a6990cac91ec45536d838ea185b25 — why this exists and whether the shipped surface is the smallest honest version. Updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

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 ships

Inventory (3 items) — 3 justified

Intent: 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.

  1. Shutdown stops all instance tunnels at the same moment, not one by one, on both the restart path and the exit path — justified
  2. The instances spec now says the bound covers any number of such forwarders and drops that case from the accepted leak — justified
  3. A new test pins that every stop is under way at once, through the real pre-exec stop path — justified

[FIRST-PRINCIPLES-REVIEWED] e18fdf8

Description read

[DESCRIPTION-READ] 297d4118720df606fc3fca7c604d0c9f341e89b0143c22861abf1362b5e28d42

That sha256 names the PR description this verdict read. Recompute it with .github/scripts/pr-description-capture.sh; if it differs, the description changed after this verdict was formed and any finding drawn from the description is unproven.

@github-actions

github-actions Bot commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5) — ✅ PASS

Design-level review of e18fdf87ec7a6990cac91ec45536d838ea185b25 — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

Design-Verdict: PASS

Nothing blocks: running each tunnel's existing stop() at the same time fixes the cause, and changes neither the 10 s bound nor the way a single tunnel stops.

[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 .github/scripts/pr-description-capture.sh over the same inputs; if it differs, the description or that evidence changed after this verdict was formed and any finding drawn from either is unproven.

@github-actions

github-actions Bot commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

Opus 5.5 Review — ✅ no blocking findings

Reviewed e18fdf87ec7a6990cac91ec45536d838ea185b25 — this comment is updated in place on each push.

Review details

No findings.

[OPUS-REVIEWED] e18fdf8

Verdict parsed from the review's SHA-scoped output markers for commit e18fdf87ec7a6990cac91ec45536d838ea185b25.

False positive or not applicable? A repository writer can comment:
/ai-review override fable e18fdf87ec7a6990cac91ec45536d838ea185b25 false-positive: <reason, 20 to 500 characters>

@github-actions

github-actions Bot commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

GPT 6.1 Review — ✅ no blocking findings

GPT 6.1 completed its review of e18fdf87ec7a6990cac91ec45536d838ea185b25 and found no blocking issues.

This comment is updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] e18fdf8

False positive or not applicable? A repository writer can comment:
/ai-review override gpt e18fdf87ec7a6990cac91ec45536d838ea185b25 false-positive: <reason, 20 to 500 characters>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

readiness: checking Automated validation is still running

Projects

None yet

Development

Successfully merging this pull request may close these issues.

An in-place restart can leave an instance forwarder running when two forwarders ignore SIGTERM

1 participant