Skip to content

Commit d74e8a9

Browse files
committed
Add timeout for I/O waitgroups
Backport for #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 <[email protected]>
1 parent e9e200b commit d74e8a9

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)