Merge pull request #4908 from ariel-anieli/context-after-func-libcontainer

libcontainer: switch `goCreateMountSources()` to `ctx.AfterFunc`
This commit is contained in:
Kir Kolyshkin
2025-10-02 19:29:44 -07:00
committed by GitHub

View File

@@ -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