Free orphaned proxy port on stop and rm#5394
Open
gkatz2 wants to merge 1 commit into
Open
Conversation
When a workload's status file is missing, thv stop and thv rm left the proxy process running and holding the workload's port. The proxy-stop path terminates the proxy by the PID recorded in the status file, so with the file gone nothing was killed. On stop the surviving supervisor then restarted the container, so the workload would not stay stopped; on rm the orphaned proxy kept the port, so it could not be reused without killing the process by hand. Fixes stacklok#5393 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Signed-off-by: Greg Katz <gkatz@indeed.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #5394 +/- ##
==========================================
+ Coverage 68.83% 68.84% +0.01%
==========================================
Files 628 628
Lines 63900 63911 +11
==========================================
+ Hits 43985 44001 +16
- Misses 16658 16665 +7
+ Partials 3257 3245 -12 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When a workload's status file is missing,
thv stopandthv rmreport success but leave the workload's proxy process running and holding its port. The proxy-stop path kills the proxy by the PID recorded in the status file, so with the file gone nothing is killed:thv stop, the surviving supervisor restarts the container, so the workload returns torunningon its own.thv rm, the container is removed but the orphaned proxy keeps holding the port, so it cannot be reused without killing the process by hand.This makes stop and rm fall back to the existing port-based cleanup when the PID-based stop finds no proxy to kill, so the proxy is terminated and the port freed even when the status file is missing. The fallback reuses
freePortHolderIfNeeded(already used on the restart path), which only kills a process verified to be this workload's own proxy.stopProcess/stopProxyIfNeededreport whether a tracked proxy was actually killed.runConfiginto the container stop/delete paths so the fallback knows the proxy port.Fixes #5393
Type of change
Test plan
task test)task test-e2e)task lint-fix)Manual testing on macOS + OrbStack with a real container workload (
fetch):thv stopleft the supervisor process alive (verified by PID) and it recreated the container — a new container ID and newStartedAt— returning the workload torunning;thv rmleft the orphaned proxy holding the port.thv stopandthv rmterminate the proxy (PID gone) and free the port.The added unit tests fail without the fix and pass with it.
Does this introduce a user-facing change?
Yes.
thv stopandthv rmnow reliably stop the workload's proxy and free its port even when the workload's status file is missing, instead of leaving an orphaned proxy that holds the port (and, forstop, restarts the container).Special notes for reviewers
freePortHolderIfNeeded→process.IsToolHiveProxyForWorkloadconfirms the process on the port is this workload'sthv start <name>proxy before killing it, so it cannot touch an unrelated process or another workload's proxy.runner.LoadStateitself fails (the run config is gone, not just the status file), the proxy port cannot be recovered. In the reported scenario only the status file is missing, soLoadStatesucceeds and the port is recoverable.Generated with Claude Code