File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -106,7 +106,7 @@ func (e *execProcess) Delete(ctx context.Context) error {
106106}
107107
108108func (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 ()
Original file line number Diff line number Diff line change @@ -284,7 +284,7 @@ func (p *Init) Delete(ctx context.Context) error {
284284}
285285
286286func (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
Original file line number Diff line number Diff line change 1919package proc
2020
2121import (
22+ "context"
2223 "encoding/json"
2324 "fmt"
2425 "io"
@@ -143,3 +144,21 @@ func (p *pidFile) Path() string {
143144func (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+ }
You can’t perform that action at this time.
0 commit comments