From 2e143a25ff02800afb569352c407cf71a9c0312b Mon Sep 17 00:00:00 2001
From: lujingxiao <lujingxiao@huawei.com>
Date: Wed, 23 Jan 2019 14:56:19 +0800
Subject: [PATCH 07/27] shim:  Increase reaper buffer size and
 non-blocking send

reason: Fixes #2709

This increases the buffer size for process exit subscribers. It also
implements a non-blocking send on the subscriber channel.  It is better
to drop an exit even than it is to block a shim for one slow subscriber.

Cherry-pick from upstream 232a063496

Change-Id: Ibf9f06cc82945a8592fb02a87816d69d5dac2b6b
Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
Signed-off-by: lujingxiao <lujingxiao@huawei.com>
---
 runtime/v1/shim/reaper.go      | 14 +++++++++++---
 runtime/v2/shim/reaper_unix.go | 14 +++++++++++---
 2 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/runtime/v1/shim/reaper.go b/runtime/v1/shim/reaper.go
index 2937f1a..10d5c30 100644
--- a/runtime/v1/shim/reaper.go
+++ b/runtime/v1/shim/reaper.go
@@ -26,12 +26,13 @@ import (
 	"github.com/containerd/containerd/sys"
 	runc "github.com/containerd/go-runc"
 	"github.com/pkg/errors"
+	"github.com/sirupsen/logrus"
 )
 
 // ErrNoSuchProcess is returned when the process no longer exists
 var ErrNoSuchProcess = errors.New("no such process")
 
-const bufferSize = 32
+const bufferSize = 2048
 
 // Reap should be called when the process receives an SIGCHLD.  Reap will reap
 // all exited processes and close their wait channels
@@ -41,13 +42,20 @@ func Reap() error {
 	Default.Lock()
 	for c := range Default.subscribers {
 		for _, e := range exits {
-			c <- runc.Exit{
+			select {
+			case c <- runc.Exit{
 				Timestamp: now,
 				Pid:       e.Pid,
 				Status:    e.Status,
+			}:
+			default:
+				logrus.WithFields(logrus.Fields{
+					"subscriber": c,
+					"pid":        e.Pid,
+					"status":     e.Status,
+				}).Warn("failed to send exit to subscriber")
 			}
 		}
-
 	}
 	Default.Unlock()
 	return err
diff --git a/runtime/v2/shim/reaper_unix.go b/runtime/v2/shim/reaper_unix.go
index 2937f1a..10d5c30 100644
--- a/runtime/v2/shim/reaper_unix.go
+++ b/runtime/v2/shim/reaper_unix.go
@@ -26,12 +26,13 @@ import (
 	"github.com/containerd/containerd/sys"
 	runc "github.com/containerd/go-runc"
 	"github.com/pkg/errors"
+	"github.com/sirupsen/logrus"
 )
 
 // ErrNoSuchProcess is returned when the process no longer exists
 var ErrNoSuchProcess = errors.New("no such process")
 
-const bufferSize = 32
+const bufferSize = 2048
 
 // Reap should be called when the process receives an SIGCHLD.  Reap will reap
 // all exited processes and close their wait channels
@@ -41,13 +42,20 @@ func Reap() error {
 	Default.Lock()
 	for c := range Default.subscribers {
 		for _, e := range exits {
-			c <- runc.Exit{
+			select {
+			case c <- runc.Exit{
 				Timestamp: now,
 				Pid:       e.Pid,
 				Status:    e.Status,
+			}:
+			default:
+				logrus.WithFields(logrus.Fields{
+					"subscriber": c,
+					"pid":        e.Pid,
+					"status":     e.Status,
+				}).Warn("failed to send exit to subscriber")
 			}
 		}
-
 	}
 	Default.Unlock()
 	return err
-- 
2.7.4.3

