From 245052243d23c8de21fcc95bbf47fb1dbc731ab4 Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Thu, 20 Jun 2019 16:13:51 -0400 Subject: [PATCH] Add timeout for I/O waitgroups Closes #3286 This and a combination of a couple Docker changes are needed to fully resolve the issue on the Docker side. However, this ensures that after processes exit, we still leave some time for the I/O to fully flush before closing. Without this timeout, the delete methods would block forever. Signed-off-by: Michael Crosby --- runtime/v1/linux/proc/exec.go | 2 +- runtime/v1/linux/proc/init.go | 2 +- runtime/v1/linux/proc/utils.go | 19 +++++++++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/runtime/v1/linux/proc/exec.go b/runtime/v1/linux/proc/exec.go index 5ab232ae7..1779725d6 100644 --- a/runtime/v1/linux/proc/exec.go +++ b/runtime/v1/linux/proc/exec.go @@ -106,7 +106,7 @@ func (e *execProcess) Delete(ctx context.Context) error { } func (e *execProcess) delete(ctx context.Context) error { - e.wg.Wait() + waitTimeout(ctx, &e.wg, 2*time.Second) if e.io != nil { for _, c := range e.closers { c.Close() diff --git a/runtime/v1/linux/proc/init.go b/runtime/v1/linux/proc/init.go index c6a09575a..36e6f1674 100644 --- a/runtime/v1/linux/proc/init.go +++ b/runtime/v1/linux/proc/init.go @@ -284,7 +284,7 @@ func (p *Init) Delete(ctx context.Context) error { } func (p *Init) delete(ctx context.Context) error { - p.wg.Wait() + waitTimeout(ctx, &p.wg, 2*time.Second) err := p.runtime.Delete(ctx, p.id, nil) // ignore errors if a runtime has already deleted the process // but we still hold metadata and pipes diff --git a/runtime/v1/linux/proc/utils.go b/runtime/v1/linux/proc/utils.go index 2d085e62f..a2b236f65 100644 --- a/runtime/v1/linux/proc/utils.go +++ b/runtime/v1/linux/proc/utils.go @@ -19,6 +19,7 @@ package proc import ( + "context" "encoding/json" "fmt" "io" @@ -143,3 +144,21 @@ func (p *pidFile) Path() string { func (p *pidFile) Read() (int, error) { return runc.ReadPidFile(p.path) } + +// waitTimeout handles waiting on a waitgroup with a specified timeout. +// this is commonly used for waiting on IO to finish after a process has exited +func waitTimeout(ctx context.Context, wg *sync.WaitGroup, timeout time.Duration) error { + ctx, cancel := context.WithTimeout(ctx, timeout) + defer cancel() + done := make(chan struct{}, 1) + go func() { + wg.Wait() + close(done) + }() + select { + case <-done: + return nil + case <-ctx.Done(): + return ctx.Err() + } +}