Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion internal/cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
54 changes: 54 additions & 0 deletions internal/cli/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
Loading