Skip to content

Commit 5b7f3ab

Browse files
authored
Merge pull request #3691 from crosbymichael/waitgroup
[release/1.1] Add timeout for I/O waitgroups
2 parents ed1b4ef + d74e8a9 commit 5b7f3ab

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

linux/proc/exec.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func (e *execProcess) setExited(status int) {
9494
}
9595

9696
func (e *execProcess) delete(ctx context.Context) error {
97-
e.wg.Wait()
97+
waitTimeout(ctx, &e.wg, 2*time.Second)
9898
if e.io != nil {
9999
for _, c := range e.closers {
100100
c.Close()

linux/proc/utils.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
package proc
2020

2121
import (
22+
"context"
2223
"encoding/json"
2324
"io"
2425
"os"
2526
"strings"
27+
"sync"
2628
"time"
2729

2830
"github.com/containerd/containerd/errdefs"
@@ -104,3 +106,21 @@ func checkKillError(err error) error {
104106
func hasNoIO(r *CreateConfig) bool {
105107
return r.Stdin == "" && r.Stdout == "" && r.Stderr == ""
106108
}
109+
110+
// waitTimeout handles waiting on a waitgroup with a specified timeout.
111+
// this is commonly used for waiting on IO to finish after a process has exited
112+
func waitTimeout(ctx context.Context, wg *sync.WaitGroup, timeout time.Duration) error {
113+
ctx, cancel := context.WithTimeout(ctx, timeout)
114+
defer cancel()
115+
done := make(chan struct{}, 1)
116+
go func() {
117+
wg.Wait()
118+
close(done)
119+
}()
120+
select {
121+
case <-done:
122+
return nil
123+
case <-ctx.Done():
124+
return ctx.Err()
125+
}
126+
}

0 commit comments

Comments
 (0)