Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make handling of stale mount pid files more robust #4191

Merged
merged 1 commit into from
May 3, 2019
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
2 changes: 1 addition & 1 deletion cmd/minikube/cmd/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ itself, leaving all files intact. The cluster can be started again with the "sta
}

if err := cmdUtil.KillMountProcess(); err != nil {
exit.WithError("Unable to kill mount process", err)
console.OutStyle("warning", "Unable to kill mount process: %s", err)
}
},
}
Expand Down
46 changes: 40 additions & 6 deletions cmd/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ limitations under the License.
package util

import (
"fmt"
"io/ioutil"
"net"
"os"
"path/filepath"
"strconv"

"github.com/golang/glog"
ps "github.com/mitchellh/go-ps"
"github.com/pkg/errors"
"k8s.io/minikube/pkg/minikube/constants"
)
Expand All @@ -45,19 +48,50 @@ func GetPort() (int, error) {

// KillMountProcess kills the mount process, if it is running
func KillMountProcess() error {
out, err := ioutil.ReadFile(filepath.Join(constants.GetMinipath(), constants.MountProcessFileName))
pidPath := filepath.Join(constants.GetMinipath(), constants.MountProcessFileName)
if _, err := os.Stat(pidPath); os.IsNotExist(err) {
return nil
}

glog.Infof("Found %s ...", pidPath)
out, err := ioutil.ReadFile(pidPath)
if err != nil {
return nil // no mount process to kill
return errors.Wrap(err, "ReadFile")
}
glog.Infof("pidfile contents: %s", out)
pid, err := strconv.Atoi(string(out))
if err != nil {
return errors.Wrap(err, "error converting mount string to pid")
return errors.Wrap(err, "error parsing pid")
}
// os.FindProcess does not check if pid is running :(
entry, err := ps.FindProcess(pid)
if err != nil {
return errors.Wrap(err, "ps.FindProcess")
}
mountProc, err := os.FindProcess(pid)
if entry == nil {
glog.Infof("Stale pid: %d", pid)
if err := os.Remove(pidPath); err != nil {
return errors.Wrap(err, "Removing stale pid")
}
return nil
}

// We found a process, but it still may not be ours.
glog.Infof("Found process %d: %s", pid, entry.Executable())
proc, err := os.FindProcess(pid)
if err != nil {
return errors.Wrap(err, "error converting mount string to pid")
return errors.Wrap(err, "os.FindProcess")
}

glog.Infof("Killing pid %d ...", pid)
if err := proc.Kill(); err != nil {
glog.Infof("Kill failed with %v - removing probably stale pid...", err)
if err := os.Remove(pidPath); err != nil {
return errors.Wrap(err, "Removing likely stale unkillable pid")
}
return errors.Wrap(err, fmt.Sprintf("Kill(%d/%s)", pid, entry.Executable()))
}
return mountProc.Kill()
return nil
}

// GetKubeConfigPath gets the path to the first kubeconfig
Expand Down