From 3f3c04d7222b32a9242166cb6ecb0cd8605327b3 Mon Sep 17 00:00:00 2001 From: Robert Krawitz Date: Tue, 1 May 2018 19:38:12 -0400 Subject: [PATCH] WIP: Correct kill logic for cgroup processes --- pkg/kubelet/cm/pod_container_manager_linux.go | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/pkg/kubelet/cm/pod_container_manager_linux.go b/pkg/kubelet/cm/pod_container_manager_linux.go index a9a873691cb03..7757c8945f3e2 100644 --- a/pkg/kubelet/cm/pod_container_manager_linux.go +++ b/pkg/kubelet/cm/pod_container_manager_linux.go @@ -123,6 +123,26 @@ func (m *podContainerManagerImpl) GetPodContainerName(pod *v1.Pod) (CgroupName, return cgroupName, cgroupfsName } +// Kill one process ID +func (m *podContainerManagerImpl) killOnePid(pid int) error { + // os.FindProcess never returns an error on POSIX + // https://go-review.googlesource.com/c/go/+/19093 + p, _ := os.FindProcess(pid) + if err := p.Kill(); err != nil { + // If the process already exited, that's fine. + if strings.Contains(err.Error(), "process already finished") { + // Hate parsing strings, but + // vendor/github.com/opencontainers/runc/libcontainer/ + // also does this. + glog.V(3).Infof("process with pid %v no longer exists", pid) + return nil + } else { + return err + } + } + return nil +} + // Scan through the whole cgroup directory and kill all processes either // attached to the pod cgroup or to a container cgroup under the pod cgroup func (m *podContainerManagerImpl) tryKillingCgroupProcesses(podCgroup CgroupName) error { @@ -141,13 +161,8 @@ func (m *podContainerManagerImpl) tryKillingCgroupProcesses(podCgroup CgroupName } errlist = []error{} for _, pid := range pidsToKill { - p, err := os.FindProcess(pid) - if err != nil { - // Process not running anymore, do nothing - continue - } glog.V(3).Infof("Attempt to kill process with pid: %v", pid) - if err := p.Kill(); err != nil { + if err := m.killOnePid(pid); err != nil { glog.V(3).Infof("failed to kill process with pid: %v", pid) errlist = append(errlist, err) }