From 1d919b6667213bc95cd061e93df57aa4cd6e2e46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jeffry=20W=C3=BCrmli?= Date: Thu, 12 Nov 2026 13:27:12 +0100 Subject: [PATCH] fix: skip remaining findings once a stop has freed the port --- internal/cli/run.go | 14 ++++++++++- internal/cli/run_test.go | 54 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/internal/cli/run.go b/internal/cli/run.go index 0403d5b..1d72d06 100644 --- a/internal/cli/run.go +++ b/internal/cli/run.go @@ -102,7 +102,12 @@ func stopAll(ctx context.Context, d Deps, reports []report.PortReport, flags Fla }() for _, r := range reports { - for _, f := range r.Findings { + for i, f := range r.Findings { + if i > 0 && !flags.DryRun && portIsFree(ctx, check, r.Port) { + // An earlier stop already freed the port; the remaining findings + // belong to processes that are gone. The stop line was printed. + break + } if f.Explanation.Terminator == nil { line(render.KillFailed, r.Port, "cannot stop it: the owning process is not visible; try again with sudo") failed = true @@ -132,6 +137,13 @@ func stopAll(ctx context.Context, d Deps, reports []report.PortReport, flags Fla return failed } +// portIsFree reports whether the port is verifiably free. A failing check +// counts as busy so a stop is never skipped on doubt. +func portIsFree(ctx context.Context, check kill.PortChecker, port uint16) bool { + free, err := check(ctx, port) + return err == nil && free +} + // portFree returns a kill.PortChecker that treats any listening socket on the // port, including one whose owner is not visible, as busy. func portFree(c socket.Collector) kill.PortChecker { diff --git a/internal/cli/run_test.go b/internal/cli/run_test.go index 88d710b..0e165f0 100644 --- a/internal/cli/run_test.go +++ b/internal/cli/run_test.go @@ -502,6 +502,60 @@ func TestRun_KillTerminatesSharedCauseOnce(t *testing.T) { } } +func TestRun_KillSkipsLaterFindingsOnceFirstFreedPort(t *testing.T) { + c := &freeingSockets{} + first := &fakeTerminator{describe: "brew services stop php", onTerm: func() { c.freed = true }} + second := &fakeTerminator{describe: "kill -TERM 43"} + d, stderr := killDeps(first, c) + d.Sockets = twoPIDSockets{c} + d.Detector = perPIDDetector{42: first, 43: second} + + got := Run(context.Background(), d, []string{"3000"}, Flags{Kill: true, Yes: true}) + + if got != 0 { + t.Errorf("exit = %d, want 0", got) + } + if first.calls != 1 || second.calls != 0 { + t.Errorf("calls = %d/%d, want 1/0", first.calls, second.calls) + } + if n := strings.Count(stderr.String(), "Port 3000 is free."); n != 1 { + t.Errorf("free lines = %d, want 1; stderr = %q", n, stderr.String()) + } +} + +func TestRun_KillHandlesLaterFindingWhilePortStaysBusy(t *testing.T) { + c := &freeingSockets{} + first := &fakeTerminator{describe: "kill -TERM 42"} + second := &fakeTerminator{describe: "kill -TERM 43", onTerm: func() { c.freed = true }} + d, _ := killDeps(first, c) + d.Sockets = twoPIDSockets{c} + d.Detector = perPIDDetector{42: first, 43: second} + + Run(context.Background(), d, []string{"3000"}, Flags{Kill: true, Yes: true}) + + if first.calls != 1 || second.calls != 1 { + t.Errorf("calls = %d/%d, want 1/1", first.calls, second.calls) + } +} + +// twoPIDSockets lists two different processes on port 3000 until freed. +type twoPIDSockets struct{ c *freeingSockets } + +func (s twoPIDSockets) Sockets(ctx context.Context) ([]socket.Socket, error) { + all, err := s.c.Sockets(ctx) + if len(all) == 0 { + return all, err + } + return []socket.Socket{listener(3000, 42), listener(3000, 43)}, err +} + +// perPIDDetector explains a socket with the terminator of its owning PID. +type perPIDDetector map[int]kill.Terminator + +func (p perPIDDetector) Detect(_ context.Context, s socket.Socket, _ *proc.Process) (*detect.Explanation, bool) { + return &detect.Explanation{Source: detect.SourceProcess, Title: "node", Terminator: p[s.PID]}, true +} + // twoSockets lists the same process twice (IPv4 and IPv6) until freed. type twoSockets struct{ c *freeingSockets }