Skip to content

Commit 4404cdf

Browse files
committed
libcontainer: switch goCreateMountSources() to ctx.AfterFunc
ba0b5e2 ("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 <[email protected]> Signed-off-by: Ariel Otilibili <[email protected]>
1 parent 00aec12 commit 4404cdf

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

libcontainer/process_linux.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"runtime"
1616
"strconv"
1717
"strings"
18-
"sync"
1918
"syscall"
2019
"time"
2120

@@ -613,6 +612,8 @@ func (p *initProcess) goCreateMountSources(ctx context.Context) (mountSourceRequ
613612
responseCh := make(chan response)
614613

615614
ctx, cancelFn := context.WithTimeout(ctx, 1*time.Minute)
615+
context.AfterFunc(ctx, func() { close(requestCh) })
616+
616617
go func() {
617618
// We lock this thread because we need to setns(2) here. There is no
618619
// UnlockOSThread() here, to ensure that the Go runtime will kill this
@@ -675,8 +676,6 @@ func (p *initProcess) goCreateMountSources(ctx context.Context) (mountSourceRequ
675676
return nil, nil, err
676677
}
677678

678-
// TODO: Switch to context.AfterFunc when we switch to Go 1.21.
679-
var requestChCloseOnce sync.Once
680679
requestFn := func(m *configs.Mount) (*mountSource, error) {
681680
var err error
682681
select {
@@ -686,13 +685,13 @@ func (p *initProcess) goCreateMountSources(ctx context.Context) (mountSourceRequ
686685
if ok {
687686
return resp.src, resp.err
688687
}
688+
err = fmt.Errorf("response channel closed unexpectedly")
689689
case <-ctx.Done():
690690
err = fmt.Errorf("receive mount source context cancelled: %w", ctx.Err())
691691
}
692692
case <-ctx.Done():
693693
err = fmt.Errorf("send mount request cancelled: %w", ctx.Err())
694694
}
695-
requestChCloseOnce.Do(func() { close(requestCh) })
696695
return nil, err
697696
}
698697
return requestFn, cancelFn, nil

0 commit comments

Comments
 (0)