Skip to content

Commit 2450522

Browse files
committed
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]>
1 parent 111b082 commit 2450522

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
@@ -284,7 +284,7 @@ func (p *Init) Delete(ctx context.Context) error {
284284
}
285285

286286
func (p *Init) delete(ctx context.Context) error {
287-
p.wg.Wait()
287+
waitTimeout(ctx, &p.wg, 2*time.Second)
288288
err := p.runtime.Delete(ctx, p.id, nil)
289289
// ignore errors if a runtime has already deleted the process
290290
// 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
"fmt"
2425
"io"
@@ -143,3 +144,21 @@ func (p *pidFile) Path() string {
143144
func (p *pidFile) Read() (int, error) {
144145
return runc.ReadPidFile(p.path)
145146
}
147+
148+
// waitTimeout handles waiting on a waitgroup with a specified timeout.
149+
// this is commonly used for waiting on IO to finish after a process has exited
150+
func waitTimeout(ctx context.Context, wg *sync.WaitGroup, timeout time.Duration) error {
151+
ctx, cancel := context.WithTimeout(ctx, timeout)
152+
defer cancel()
153+
done := make(chan struct{}, 1)
154+
go func() {
155+
wg.Wait()
156+
close(done)
157+
}()
158+
select {
159+
case <-done:
160+
return nil
161+
case <-ctx.Done():
162+
return ctx.Err()
163+
}
164+
}

0 commit comments

Comments
 (0)