From 18c76025a179855b2f474c06fd1c6c3d6a6a66e2 Mon Sep 17 00:00:00 2001 From: Kenfe-Mickael Laventure Date: Mon, 12 Sep 2016 11:22:48 -0700 Subject: [PATCH] Wait for all exec process child before exiting from shim Signed-off-by: Kenfe-Mickael Laventure --- containerd-shim/main.go | 5 ++++- osutils/reaper.go | 8 ++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/containerd-shim/main.go b/containerd-shim/main.go index fbfc686..85d5f1e 100644 --- a/containerd-shim/main.go +++ b/containerd-shim/main.go @@ -106,7 +106,7 @@ func start(log *os.File) error { case s := <-signals: switch s { case syscall.SIGCHLD: - exits, _ := osutils.Reap() + exits, _ := osutils.Reap(false) for _, e := range exits { // check to see if runtime is one of the processes that has exited if e.Pid == p.pid() { @@ -117,6 +117,9 @@ func start(log *os.File) error { } // runtime has exited so the shim can also exit if exitShim { + // Wait for all the childs this process may have created + // (only needed for exec, but it won't hurt when done on init) + osutils.Reap(true) // Let containerd take care of calling the runtime delete f.Close() p.Wait() diff --git a/osutils/reaper.go b/osutils/reaper.go index ab7d24d..6a80335 100644 --- a/osutils/reaper.go +++ b/osutils/reaper.go @@ -12,13 +12,17 @@ type Exit struct { // Reap reaps all child processes for the calling process and returns their // exit information -func Reap() (exits []Exit, err error) { +func Reap(wait bool) (exits []Exit, err error) { var ( ws syscall.WaitStatus rus syscall.Rusage ) + flag := syscall.WNOHANG + if wait { + flag = 0 + } for { - pid, err := syscall.Wait4(-1, &ws, syscall.WNOHANG, &rus) + pid, err := syscall.Wait4(-1, &ws, flag, &rus) if err != nil { if err == syscall.ECHILD { return exits, nil