From 4404cdf94b5ca193e143b653c6b3685c0b4469ce Mon Sep 17 00:00:00 2001 From: Ariel Otilibili Date: Thu, 2 Oct 2025 11:10:08 +0200 Subject: [PATCH] libcontainer: switch goCreateMountSources() to ctx.AfterFunc ba0b5e26 ("libcontainer: remove all mount logic from nsexec") introduced a request function that handles two tasks: - the exchanges with the request and response channels - the closing of the request channel. From 1.21, the closing of the request channel may be done with context.AfterFunc(). Moreover, context.AfterFunc() is guaranteed to run once. Link: https://pkg.go.dev/context#AfterFunc Suggested-by: Aleksa Sarai Signed-off-by: Ariel Otilibili --- libcontainer/process_linux.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) 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