Skip to content

Commit

Permalink
Add timeout for I/O waitgroups
Browse files Browse the repository at this point in the history
Backport for containerd#3361

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 <crosbymichael@gmail.com>
  • Loading branch information
crosbymichael committed Sep 26, 2019
1 parent e9e200b commit d74e8a9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion linux/proc/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (e *execProcess) setExited(status int) {
}

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()
Expand Down
20 changes: 20 additions & 0 deletions linux/proc/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
package proc

import (
"context"
"encoding/json"
"io"
"os"
"strings"
"sync"
"time"

"github.com/containerd/containerd/errdefs"
Expand Down Expand Up @@ -104,3 +106,21 @@ func checkKillError(err error) error {
func hasNoIO(r *CreateConfig) bool {
return r.Stdin == "" && r.Stdout == "" && r.Stderr == ""
}

// 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()
}
}

0 comments on commit d74e8a9

Please sign in to comment.