From 2e01216dd69e7dc5cb87a7f09ca6c731e9d539f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jeffry=20W=C3=BCrmli?= Date: Thu, 12 Nov 2026 13:29:34 +0100 Subject: [PATCH] fix: make --dry-run report protected and invisible targets and respect stderr TTY Dry run now names the protection reason for PID 1 and self instead of claiming it would execute, reports an invisible owner as a hint without a kill-failure exit, and the confirmation prompt requires stdin and stderr (where it is written) to be terminals instead of stdout. --- internal/cli/run.go | 9 +++++-- internal/cli/run_test.go | 51 +++++++++++++++++++++++++++++++---- internal/kill/kill.go | 17 ++++++++++-- internal/kill/process_test.go | 23 ++++++++++++++++ 4 files changed, 91 insertions(+), 9 deletions(-) diff --git a/internal/cli/run.go b/internal/cli/run.go index 0403d5b..d1bada9 100644 --- a/internal/cli/run.go +++ b/internal/cli/run.go @@ -82,7 +82,7 @@ func stopAll(ctx context.Context, d Deps, reports []report.PortReport, flags Fla Yes: flags.Yes, Force: flags.Force, DryRun: flags.DryRun, - Interactive: d.IsTTY && d.StdinIsTTY, + Interactive: d.StdinIsTTY && d.StderrIsTTY, In: d.Stdin, Out: d.Stderr, } @@ -104,7 +104,12 @@ func stopAll(ctx context.Context, d Deps, reports []report.PortReport, flags Fla for _, r := range reports { for _, f := range r.Findings { if f.Explanation.Terminator == nil { - line(render.KillFailed, r.Port, "cannot stop it: the owning process is not visible; try again with sudo") + const hint = "cannot stop it: the owning process is not visible; try again with sudo" + if flags.DryRun { + _, _ = fmt.Fprintln(d.Stderr, render.KillLine(render.KillFailed, r.Port, hint, ropts)) + continue + } + line(render.KillFailed, r.Port, hint) failed = true continue } diff --git a/internal/cli/run_test.go b/internal/cli/run_test.go index 88d710b..c37279b 100644 --- a/internal/cli/run_test.go +++ b/internal/cli/run_test.go @@ -405,8 +405,7 @@ func TestRun_KillWithoutTTYAndWithoutYesExit3(t *testing.T) { func TestRun_KillNeedsStdinTTYToo(t *testing.T) { ft := &fakeTerminator{describe: "x"} d, _ := killDeps(ft, &freeingSockets{}) - d.IsTTY = true - d.StdinIsTTY = false + d.StdinIsTTY, d.StderrIsTTY = false, true d.Stdin = strings.NewReader("y\n") if got := Run(context.Background(), d, []string{"3000"}, Flags{Kill: true}); got != 3 { @@ -421,7 +420,7 @@ func TestRun_KillPromptAccepted(t *testing.T) { c := &freeingSockets{} ft := &fakeTerminator{describe: "x", onTerm: func() { c.freed = true }} d, _ := killDeps(ft, c) - d.IsTTY, d.StdinIsTTY = true, true + d.StdinIsTTY, d.StderrIsTTY = true, true d.Stdin = strings.NewReader("y\n") if got := Run(context.Background(), d, []string{"3000"}, Flags{Kill: true}); got != 0 { @@ -435,7 +434,7 @@ func TestRun_KillPromptAccepted(t *testing.T) { func TestRun_KillDeclinedIsNotAFailure(t *testing.T) { ft := &fakeTerminator{describe: "x"} d, _ := killDeps(ft, &freeingSockets{}) - d.IsTTY, d.StdinIsTTY = true, true + d.StdinIsTTY, d.StderrIsTTY = true, true d.Stdin = strings.NewReader("n\n") if got := Run(context.Background(), d, []string{"3000"}, Flags{Kill: true}); got != 0 { @@ -640,7 +639,7 @@ func TestRun_KillReportsStatusLinesAndAResultBox(t *testing.T) { func TestRun_KillDeclinedShowsLeftUntouched(t *testing.T) { d, stderr := killDeps(&fakeTerminator{describe: "x"}, &freeingSockets{}) - d.IsTTY, d.StdinIsTTY = true, true + d.StdinIsTTY, d.StderrIsTTY = true, true d.Stdin = strings.NewReader("n\n") Run(context.Background(), d, []string{"3000"}, Flags{Kill: true}) @@ -685,3 +684,45 @@ func TestRootCmd_ExpandedFlagIsParsed(t *testing.T) { t.Errorf("--expanded not applied:\n%s", stdout.String()) } } + +func TestRun_KillNeedsStderrTTYForPrompt(t *testing.T) { + ft := &fakeTerminator{describe: "x"} + d, _ := killDeps(ft, &freeingSockets{}) + d.IsTTY, d.StdinIsTTY, d.StderrIsTTY = true, true, false + d.Stdin = strings.NewReader("y\n") + + if got := Run(context.Background(), d, []string{"3000"}, Flags{Kill: true}); got != 3 { + t.Errorf("exit = %d, want 3", got) + } + if ft.calls != 0 { + t.Errorf("Terminate calls = %d, want 0", ft.calls) + } +} + +func TestRun_KillPromptDoesNotNeedStdoutTTY(t *testing.T) { + c := &freeingSockets{} + ft := &fakeTerminator{describe: "x", onTerm: func() { c.freed = true }} + d, _ := killDeps(ft, c) + d.IsTTY, d.StdinIsTTY, d.StderrIsTTY = false, true, true + d.Stdin = strings.NewReader("y\n") + + if got := Run(context.Background(), d, []string{"3000"}, Flags{Kill: true}); got != 0 { + t.Errorf("exit = %d, want 0", got) + } + if ft.calls != 1 { + t.Errorf("Terminate calls = %d, want 1", ft.calls) + } +} + +func TestRun_KillDryRunInvisibleOwnerIsAHintNotAFailure(t *testing.T) { + d, stderr := killDeps(nil, &freeingSockets{}) + + got := Run(context.Background(), d, []string{"3000"}, Flags{Kill: true, DryRun: true}) + + if got != 0 { + t.Errorf("exit = %d, want 0 (busy and explained, no kill failure)", got) + } + if !strings.Contains(stderr.String(), "not visible") { + t.Errorf("stderr = %q, want a hint about the invisible owner", stderr.String()) + } +} diff --git a/internal/kill/kill.go b/internal/kill/kill.go index d61800e..97114b8 100644 --- a/internal/kill/kill.go +++ b/internal/kill/kill.go @@ -95,8 +95,7 @@ func Run(ctx context.Context, t Terminator, port uint16, o Options, check PortCh t = configure(t, o) if o.DryRun { - _, err := fmt.Fprintf(o.Out, "Dry run, would execute: %s\n", t.Describe()) - return err + return dryRun(ctx, t, o) } if p, ok := t.(preflighter); ok { if err := p.preflight(ctx); err != nil { @@ -122,6 +121,20 @@ func Run(ctx context.Context, t Terminator, port uint16, o Options, check PortCh return verifyFree(ctx, port, o, check) } +// dryRun reports what a real run would do without signalling. A protected +// target is named as such instead of "would execute", and is not an error: +// nothing is executed either way. +func dryRun(ctx context.Context, t Terminator, o Options) error { + if p, ok := t.(preflighter); ok { + if err := p.preflight(ctx); errors.Is(err, ErrProtected) { + _, werr := fmt.Fprintf(o.Out, "Dry run, would not execute: %v\n", err) + return werr + } + } + _, err := fmt.Fprintf(o.Out, "Dry run, would execute: %s\n", t.Describe()) + return err +} + // verifyFree polls check until the port is free or the verify timeout passes. func verifyFree(ctx context.Context, port uint16, o Options, check PortChecker) error { if check == nil { diff --git a/internal/kill/process_test.go b/internal/kill/process_test.go index 483deb1..88e718a 100644 --- a/internal/kill/process_test.go +++ b/internal/kill/process_test.go @@ -477,3 +477,26 @@ func TestProcessTerminator_HostileNameCannotSpoofPromptOrErrors(t *testing.T) { err = Run(context.Background(), mk(4242, &fakeSignaler{}), 3000, withYes(o), freePort) noControl("still running message", err.Error()) } + +func TestRun_DryRunNamesProtectionReason(t *testing.T) { + for _, pid := range []int{1, 99} { + p := proc.Process{PID: pid, Name: "init", Started: started} + sig := &fakeSignaler{} + term := NewProcessTerminator(p, fakeReader{p: p}, 99, sig) + o, out := options("", false) + o.DryRun = true + + if err := Run(context.Background(), term, 3000, o, busyPort); err != nil { + t.Fatalf("pid %d: err = %v, want nil", pid, err) + } + if strings.Contains(out.String(), "would execute") { + t.Errorf("pid %d: output %q claims it would execute", pid, out.String()) + } + if !strings.Contains(out.String(), "refusing to stop a protected process") { + t.Errorf("pid %d: output %q does not name the protection", pid, out.String()) + } + if len(sig.sent) != 0 { + t.Errorf("pid %d: signals sent: %v", pid, sig.sent) + } + } +}