Repository navigation
fix: wait out the port release before reporting a backend alive - #30595
Conversation
UX Review (Opus 5.5) — ⏭️ skippedRevision |
Design Review (Opus 5.5) — ✅ PASSDesign-level review of Design-Verdict: PASS Nothing blocks: this fixes the cause (the stop waits only on the sandbox wrapper), the wait is bounded, and the port is reported free only on positive evidence. Any uncertainty still reports the port. [DESIGN-REVIEWED] 4fca18f Description read[DESCRIPTION-READ] 9efe533668d0ce451b43d6141a76b026706e00cfae7943c3f42e981bd9c9b390 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 |
First Principles Review (Opus 5.5) — ✅ PASSPremise-level review of First-Principles-Verdict: PASS The fix is aimed at the observer, not at the stop that returns early; the description names that cause and declares it out of goal, so this is a note. Notes
What this change shipsInventory (4 items) — 3 justifiedIntent: let an operator disable, un-trust or uninstall a sandboxed app without a false "backend still running" warning or a false
[FIRST-PRINCIPLES-REVIEWED] 4fca18f Description read[DESCRIPTION-READ] ae6070979507f9abc955bf3fe452136a6a827180b443f70bd7e3a45b542278be That sha256 names the PR description this verdict read. 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: |
7b0df40 to
07f9d5e
Compare
|
07f9d5e to
a14561b
Compare
|
Rebased: main moved in src/kiro_crew/apps/backend.py, docs/system-specs/modules/app-kit-platform.md (and same-dir specs) since this head went green (d775742 -> b764e20); CI re-running. The rate-limited lanes on 07f9d5e (installation |
Disable, trust withdrawal and uninstall probed the backend's port once, right after stop_app_backend returned. That stop signals the whole process group but waits only on the root it spawned, and a sandboxed backend's root is the sandbox wrapper, which exits before the server inside it closes its listening socket. The single probe caught the socket still bound and reported "backend still running ... not ours to stop" for a backend that was already exiting; on trust withdrawal that turned into a 409 refusal. stop_backend_and_observe now re-probes a still-listening port for a bounded grace (2 s, 50 ms polls) and returns a port only when it is still listening at the end. A listener that outlives the grace is reported exactly where it was, so withdrawn trust still refuses. Part of #26967
a14561b to
4fca18f
Compare
|
Rebased: main moved in docs/system-specs/modules/app-kit-platform.md, src/kiro_crew/apps/registry.py, src/kiro_crew/apps/registry_pipeline/catalog.py and 13 other same-dir/import neighbours since this head went green (b764e20 -> 7e52ec8); CI re-running. The five AI review lanes on a14561b (Design, First Principles, GPT 6.1, Opus 5.5, UX) produced no verdict: each failed closed on |
Disable, trust withdrawal and uninstall probed the backend's port once, right after stop_app_backend returned. That stop signals the whole process group but waits only on the root it spawned, and a sandboxed backend's root is the sandbox wrapper, which exits before the server inside it closes its listening socket. The single probe caught the socket still bound and reported "backend still running ... not ours to stop" for a backend that was already exiting; on trust withdrawal that turned into a 409 refusal. stop_backend_and_observe now re-probes a still-listening port for a bounded grace (2 s, 50 ms polls) and returns a port only when it is still listening at the end. A listener that outlives the grace is reported exactly where it was, so withdrawn trust still refuses. Part of #26967 (cherry picked from commit 7443c94)
Disable, trust withdrawal and uninstall probed the backend's port once, right after stop_app_backend returned. That stop signals the whole process group but waits only on the root it spawned, and a sandboxed backend's root is the sandbox wrapper, which exits before the server inside it closes its listening socket. The single probe caught the socket still bound and reported "backend still running ... not ours to stop" for a backend that was already exiting; on trust withdrawal that turned into a 409 refusal. stop_backend_and_observe now re-probes a still-listening port for a bounded grace (2 s, 50 ms polls) and returns a port only when it is still listening at the end. A listener that outlives the grace is reported exactly where it was, so withdrawn trust still refuses. Part of #26967 (cherry picked from commit 7443c94)
Problem / Motivation
Goal: Disabling, un-trusting or uninstalling an app whose backend runs in the sandbox no longer reports a false "backend still running ... not ours to stop" warning when the backend did stop.
Found by the #26967 former built-in smoke run and reproduced on a plain harness gateway with the wheel's file-explorer (no adoption involved): 7 of 11 enable then disable cycles answered
200with"warnings": ["backend still running on port 9103 — the gateway stopped every process it was tracking, so this one is not ours to stop"], while a TCP connect from the client right after each response found the port already free.Why it matters
The warning tells the operator a process the gateway cannot stop is still running, so they go hunting for a leaked server that does not exist. On trust withdrawal the same observation is a teardown failure, so
DELETE /api/security/trusted-apps/<app>answers409 teardown_incompleteand leaves the grant in place for a backend that had in fact exited. This hits every sandboxed app backend, built-in or installed from a repository, on the disable, revoke and uninstall paths.Not a goal
stop_app_backendsignals or waits (the kill path inbackend_runtime/termination.py), or the ceiling sweep's own post-stop health checks.What changed (motivation → approach → change)
Symptom → cause:
teardown.stop_backend_and_observeprobed the port once, right afterstop_app_backendreturned. That stop signals the backend's process group but waits only on the root it spawned. For a sandboxed backend the root is the sandbox wrapper, which exits before the server inside it has closed its listening socket, so the single probe often caught a socket that was about to close.Change: when the first probe finds the port listening,
stop_backend_and_observeprobes it again every 50 ms for up to 2 s (PORT_RELEASE_GRACE_SECONDS,PORT_RELEASE_POLL_SECONDS). It returns the port only if it is still listening when that time runs out. A port that is already free costs one probe and no wait, the same as before. Disable, trust withdrawal and uninstall all go through this one function, so all three are fixed together. The same port hint as before (or the manifest fallback) is used on every probe. The uninstall step inapp-kit-platform.mdnow describes the grace.Release needs evidence: once a probe has seen the port listening, a later probe that misses it ends the wait only when the new
teardown.port_releasedconfirms the port is free. The probe also answers "not listening" on a connect timeout, and a listener that stops accepting turns repeated probes into timeouts once its backlog fills, so a miss alone is not a release.port_releasedcounts a refused connect, or, when the connect times out (a full backlog, or Windows, which reports a refused loopback connect only after retries), a port->PID lookup that completes with no LISTEN socket. A lookup that cannot complete keeps the port reported.Security reading: the evidence and the decision stay the same, so no trust is widened. Each probe looks at the same gateway-recorded port, and a listener that outlives the grace still lands in
failures, which trust withdrawal refuses on. Enforcement under a withdrawn ceiling is still the supervision sweep's job.Backwards compatibility
Compatible: the return shape and every message are unchanged; the only new behaviour is up to 2 s of extra probing, and only when a port is still listening after the stop.
Removes nothing: no path, key, record or check is removed or tightened; a surviving listener is still reported and still refuses trust withdrawal.
Tests
test/test_app_teardown_port_release_grace.py(new) uses an injectedManualClockon the teardown module, so nothing sleeps on the wall clock, and a stand-in forport_released. It covers: a port that frees during the grace is not reported, the hint is kept on every re-probe, and the release is confirmed once on that port; a probe that loses sight of the listener without a confirmed release still reports it after the whole grace; a listener that outlives the grace is still reported; a free port gets one probe, no sleep and no confirmation; the manifest fallback gets the same grace; andteardown_app_runtimefor both disable and trust withdrawal. Withdrawal does not fail on a port that frees, and it still fails withbackend still running on port 9137on one that never frees and on one the probe stops seeing unconfirmed.TestPortReleasedin the same file: an accepting loopback listener is not released; a port bound with no listener is (bind 0, no literal port); and on a connect timeout the answer follows the PID lookup: a LISTEN socket keeps it, a completed empty lookup releases it, a failed lookup keeps it.test_trusted_apps_api.pyandtest_apps_uninstall_backend_stop.py: the tests whose probe always reports a live port now run the grace on aManualClock. Without it each would take 2 real seconds.test_revoke_probes_the_port_even_when_the_stop_reported_successnow asserts the probe ran, and only for this app, instead of exactly once, because re-probing is the new contract. What it guards (the probe still runs when the stop returnsTrue) is unchanged.-n0, 10/10 at-n 4 --dist loadgroupalongside its two neighbour files, and in a shuffled node-id order.test_flake_pattern_ratchet.pypasses. The 543 tests in the three changed test files plustest_app_backend_composition_contract.pypass.port_releaseddefined) turns 4 tests red, the new unconfirmed-listener test and both trust-withdrawal cases among them.prove.pycannot express that mutation: reverting the loop hunk also deletesport_released, which the tests patch, so the run ends inconclusive.Proof: not provable -- other: prove.py reverting the loop hunk also deletes port_released, which the tests patch, so its run is inconclusive; the hand mutation above turns 4 tests red
Manual verification
End to end on an isolated harness gateway (
spawn_feature_gateway("minimal"), throwawayKIROCREW_HOME, loopback only, stub ACP), drivingPOST /api/apps/file-explorer/enablethenPOST /api/apps/file-explorer/disablein a loop:origin/main@ 16ff876: 3/5 and then 4/6 disables carried the "not ours to stop" warning. In every cycle a client TCP connect right after the response found the port already free.Not run locally: semgrep (not installed on this host; CI's SAST job runs the same rules). The model-pinned local review lanes were not dispatched from this session either.
Related Issues
Part of #26967
Sibling sweep
Swept: git grep -nE "_port_is_listening(|unstopped_backend_port(|_health_probe(ap.port" -- src/ and git grep -n "stop_app_backend(" -- src/kiro_crew
Sibling: src/kiro_crew/apps/teardown.py:175 -- changed here
Sibling: src/kiro_crew/apps/routes_lifecycle.py:190 -- not affected -- delegates to teardown.stop_backend_and_observe, so the uninstall path gets the grace and the confirmation
Sibling: src/kiro_crew/apps/backend_runtime/termination.py:376 -- not affected -- an HTTP health GET that a server already shutting down on SIGTERM does not answer, unlike a bare TCP connect the kernel backlog accepts; a refusal there also re-sweeps on the next interval
Sibling: src/kiro_crew/apps/backend_runtime/ports.py:115 -- not affected -- spawn survival check while the process is starting, not a probe after a stop; the post-stop first probe at :404 in the same file is unchanged, its None documented as "nothing observed", and only the re-probes this PR adds can fill a backlog
Pattern harvest
Rule candidate: review-prompt
Pattern: liveness is judged from one probe taken right after a stop that waited only on the root process, while the process group it signalled may still be exiting
Checklist
feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)