diff --git a/libcontainer/process_linux.go b/libcontainer/process_linux.go index c8661ee69..6c885ace4 100644 --- a/libcontainer/process_linux.go +++ b/libcontainer/process_linux.go @@ -15,7 +15,6 @@ import ( "runtime" "strconv" "strings" - "sync" "syscall" "time" @@ -613,6 +612,8 @@ func (p *initProcess) goCreateMountSources(ctx context.Context) (mountSourceRequ responseCh := make(chan response) ctx, cancelFn := context.WithTimeout(ctx, 1*time.Minute) + context.AfterFunc(ctx, func() { close(requestCh) }) + go func() { // We lock this thread because we need to setns(2) here. There is no // UnlockOSThread() here, to ensure that the Go runtime will kill this @@ -675,8 +676,6 @@ func (p *initProcess) goCreateMountSources(ctx context.Context) (mountSourceRequ return nil, nil, err } - // TODO: Switch to context.AfterFunc when we switch to Go 1.21. - var requestChCloseOnce sync.Once requestFn := func(m *configs.Mount) (*mountSource, error) { var err error select { @@ -686,13 +685,13 @@ func (p *initProcess) goCreateMountSources(ctx context.Context) (mountSourceRequ if ok { return resp.src, resp.err } + err = fmt.Errorf("response channel closed unexpectedly") case <-ctx.Done(): err = fmt.Errorf("receive mount source context cancelled: %w", ctx.Err()) } case <-ctx.Done(): err = fmt.Errorf("send mount request cancelled: %w", ctx.Err()) } - requestChCloseOnce.Do(func() { close(requestCh) }) return nil, err } return requestFn, cancelFn, nil