@@ -22,6 +22,7 @@ import (
2222 "os"
2323 "os/exec"
2424 "path/filepath"
25+ "runtime"
2526 "strconv"
2627 "strings"
2728 "time"
@@ -655,16 +656,17 @@ func killProcess(path string) error {
655656 return nil
656657}
657658
658- // trySigKillProcess takes a PID as argument and tries to SIGKILL it.
659- // It performs an ownership check of the pid,
660- // before trying to send a sigkill signal to it
659+ // trySigKillProcess attempts to terminate a process by PID in a cross-platform way.
660+ // It first confirms the pid belongs to a minikube process (best-effort heuristic).
661+ // On Windows, if os.Process.Kill fails (common for some exited or protected processes),
662+ // it falls back to invoking taskkill.
661663func trySigKillProcess (pid int ) error {
662664 itDoes , err := isMinikubeProcess (pid )
663665 if err != nil {
664666 return err
665667 }
666-
667668 if ! itDoes {
669+ // Not a minikube process; report as stale (kept as error for existing caller semantics)
668670 return fmt .Errorf ("stale pid: %d" , pid )
669671 }
670672
@@ -675,6 +677,25 @@ func trySigKillProcess(pid int) error {
675677
676678 klog .Infof ("Killing pid %d ..." , pid )
677679 if err := proc .Kill (); err != nil {
680+ // Benign / already gone cases (Unix & Go 1.20+)
681+ lower := strings .ToLower (err .Error ())
682+ if errors .Is (err , os .ErrProcessDone ) ||
683+ strings .Contains (lower , "no such process" ) ||
684+ strings .Contains (lower , "already finished" ) {
685+ klog .Infof ("process %d already exited" , pid )
686+ return nil
687+ }
688+
689+ // Windows fallback: taskkill (handles some cases where Kill fails)
690+ if runtime .GOOS == "windows" {
691+ klog .Infof ("os.Process.Kill failed for %d with %v - attempting taskkill" , pid , err )
692+ cmd := exec .Command ("taskkill" , "/PID" , strconv .Itoa (pid ), "/F" , "/T" )
693+ if tkErr := cmd .Run (); tkErr == nil {
694+ return nil
695+ }
696+ return errors .Wrapf (err , "failed killing pid %d (taskkill also failed)" , pid )
697+ }
698+
678699 klog .Infof ("Kill failed with %v - removing probably stale pid..." , err )
679700 return errors .Wrapf (err , "removing likely stale unkillable pid: %d" , pid )
680701 }
0 commit comments