Skip to content

Commit 42aba6e

Browse files
crosbymichaelthaJeztah
authored andcommitted
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 <[email protected]> (cherry picked from commit 2450522) Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent b0d7ef6 commit 42aba6e

3 files changed

Lines changed: 21 additions & 2 deletions

File tree

runtime/v1/linux/proc/exec.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func (e *execProcess) Delete(ctx context.Context) error {
106106
}
107107

108108
func (e *execProcess) delete(ctx context.Context) error {
109-
e.wg.Wait()
109+
waitTimeout(ctx, &e.wg, 2*time.Second)
110110
if e.io != nil {
111111
for _, c := range e.closers {
112112
c.Close()

runtime/v1/linux/proc/init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ func (p *Init) Delete(ctx context.Context) error {
278278
}
279279

280280
func (p *Init) delete(ctx context.Context) error {
281-
p.wg.Wait()
281+
waitTimeout(ctx, &p.wg, 2*time.Second)
282282
err := p.runtime.Delete(ctx, p.id, nil)
283283
// ignore errors if a runtime has already deleted the process
284284
// but we still hold metadata and pipes

runtime/v1/linux/proc/utils.go

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

2121
import (
22+
"context"
2223
"encoding/json"
2324
"io"
2425
"os"
@@ -117,3 +118,21 @@ func checkKillError(err error) error {
117118
func hasNoIO(r *CreateConfig) bool {
118119
return r.Stdin == "" && r.Stdout == "" && r.Stderr == ""
119120
}
121+
122+
// waitTimeout handles waiting on a waitgroup with a specified timeout.
123+
// this is commonly used for waiting on IO to finish after a process has exited
124+
func waitTimeout(ctx context.Context, wg *sync.WaitGroup, timeout time.Duration) error {
125+
ctx, cancel := context.WithTimeout(ctx, timeout)
126+
defer cancel()
127+
done := make(chan struct{}, 1)
128+
go func() {
129+
wg.Wait()
130+
close(done)
131+
}()
132+
select {
133+
case <-done:
134+
return nil
135+
case <-ctx.Done():
136+
return ctx.Err()
137+
}
138+
}

0 commit comments

Comments
 (0)