From 313e7f972e887c715b8feaad332ffe505653c496 Mon Sep 17 00:00:00 2001
From: xiadanni1 <xiadanni1@huawei.com>
Date: Tue, 3 Mar 2020 06:31:18 +0800
Subject: [PATCH] containerd:add timeout for delete command

Change-Id: I620d2f19a8ac9086b5c83792a6fe49b0389da87d
Signed-off-by: xiadanni1 <xiadanni1@huawei.com>
---
 runtime/v1/linux/task.go                        |  2 +-
 runtime/v1/shim/reaper.go                       | 23 +--------------
 vendor/github.com/containerd/go-runc/monitor.go | 37 +++++++++++++++++++++++--
 vendor/github.com/containerd/go-runc/runc.go    |  3 +-
 4 files changed, 38 insertions(+), 27 deletions(-)

diff --git a/runtime/v1/linux/task.go b/runtime/v1/linux/task.go
index d2bbb76..d200e9d 100644
--- a/runtime/v1/linux/task.go
+++ b/runtime/v1/linux/task.go
@@ -91,7 +91,7 @@ func (t *Task) Namespace() string {
 func (t *Task) delete(ctx context.Context, force bool, pid uint32) (*runtime.Exit, error) {
 	rsp, err := t.shim.Delete(ctx, empty)
 	if err != nil {
-		log.G(ctx).WithError(err).Error("failed to delete container, force=%t", force)
+		log.G(ctx).WithError(err).Errorf("failed to delete container, force=%t", force)
 	}
 	t.tasks.Delete(ctx, t.id)
 	if err := t.shim.KillShim(ctx); err != nil {
diff --git a/runtime/v1/shim/reaper.go b/runtime/v1/shim/reaper.go
index d8e8274..f5f8096 100644
--- a/runtime/v1/shim/reaper.go
+++ b/runtime/v1/shim/reaper.go
@@ -19,11 +19,7 @@
 package shim
 
 import (
-	"io/ioutil"
 	"os/exec"
-	"path/filepath"
-	"strconv"
-	"strings"
 	"sync"
 	"syscall"
 	"time"
@@ -122,7 +118,7 @@ func (m *Monitor) WaitTimeout(c *exec.Cmd, ec chan runc.Exit, sec int64) (int, e
 	}()
 	select {
 	case <-time.After(time.Duration(sec) * time.Second):
-		if SameProcess(c, c.Process.Pid) {
+		if runc.SameProcess(c, c.Process.Pid) {
 			logrus.Devour(syscall.Kill(c.Process.Pid, syscall.SIGKILL))
 		}
 		return 0, errors.Errorf("timeout %ds for cmd(pid=%d): %s, %s", sec, c.Process.Pid, c.Path, c.Args)
@@ -149,20 +145,3 @@ func (m *Monitor) Unsubscribe(c chan runc.Exit) {
 	close(c)
 	m.Unlock()
 }
-
-func SameProcess(cmd *exec.Cmd, pid int) bool {
-	bytes, err := ioutil.ReadFile(filepath.Join("/proc", strconv.Itoa(pid), "cmdline"))
-	if err != nil {
-		return false
-	}
-	for i := range bytes {
-		if bytes[i] == 0 {
-			bytes[i] = 32
-		}
-	}
-	cmdline := string(bytes)
-	if strings.EqualFold(cmdline, strings.Join(cmd.Args, " ")+" ") {
-		return true
-	}
-	return false
-}
diff --git a/vendor/github.com/containerd/go-runc/monitor.go b/vendor/github.com/containerd/go-runc/monitor.go
index 2c184d2..bb8bbab 100644
--- a/vendor/github.com/containerd/go-runc/monitor.go
+++ b/vendor/github.com/containerd/go-runc/monitor.go
@@ -20,6 +20,13 @@ import (
 	"os/exec"
 	"syscall"
 	"time"
+	"io/ioutil"
+	"path/filepath"
+	"strconv"
+	"strings"
+
+	"github.com/pkg/errors"
+	"github.com/sirupsen/logrus"
 )
 
 var Monitor ProcessMonitor = &defaultMonitor{}
@@ -77,6 +84,30 @@ func (m *defaultMonitor) Wait(c *exec.Cmd, ec chan Exit) (int, error) {
 }
 
 func (m *defaultMonitor) WaitTimeout(c *exec.Cmd, ec chan Exit, sec int64) (int, error) {
-	e := <-ec
-	return e.Status, nil
-}
\ No newline at end of file
+	select {
+	case <-time.After(time.Duration(sec) * time.Second):
+		if SameProcess(c, c.Process.Pid) {
+			logrus.Devour(syscall.Kill(c.Process.Pid, syscall.SIGKILL))
+		}
+		return 0, errors.Errorf("timeout %ds for cmd(pid=%d): %s, %s", sec, c.Process.Pid, c.Path, c.Args)
+	case e := <-ec:
+		return e.Status, nil
+	}
+}
+
+func SameProcess(cmd *exec.Cmd, pid int) bool {
+	bytes, err := ioutil.ReadFile(filepath.Join("/proc", strconv.Itoa(pid), "cmdline"))
+	if err != nil {
+		return false
+	}
+	for i := range bytes {
+		if bytes[i] == 0 {
+			bytes[i] = 32
+		}
+	}
+	cmdline := string(bytes)
+	if strings.EqualFold(cmdline, strings.Join(cmd.Args, " ")+" ") {
+		return true
+	}
+	return false
+}
diff --git a/vendor/github.com/containerd/go-runc/runc.go b/vendor/github.com/containerd/go-runc/runc.go
index c1748ff..1c96317 100644
--- a/vendor/github.com/containerd/go-runc/runc.go
+++ b/vendor/github.com/containerd/go-runc/runc.go
@@ -57,6 +57,7 @@ const (
 	defaultTimeout = 30
 	startTimeout   = 120
 	updateTimeout  = 60
+	deleteTimeout  = 120
 )
 
 var (
@@ -318,7 +319,7 @@ func (r *Runc) Delete(context context.Context, id string, opts *DeleteOpts) erro
 	if opts != nil {
 		args = append(args, opts.args()...)
 	}
-	return r.runOrError(r.command(id, context, append(args, id)...))
+	return r.runOrErrorTimeout(r.command(id, context, append(args, id)...), deleteTimeout)
 }
 
 // KillOpts specifies options for killing a container and its processes
-- 
1.8.3.1

