Skip to content

Commit 61b96a8

Browse files
authored
Merge pull request #3522 from apostasie/cni-lock
Enforce global lock in oci hooks
2 parents 7eaaecb + 93fb53b commit 61b96a8

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

pkg/ocihook/ocihook.go

+21
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939
"github.com/containerd/nerdctl/v2/pkg/bypass4netnsutil"
4040
"github.com/containerd/nerdctl/v2/pkg/dnsutil/hostsstore"
4141
"github.com/containerd/nerdctl/v2/pkg/labels"
42+
"github.com/containerd/nerdctl/v2/pkg/lockutil"
4243
"github.com/containerd/nerdctl/v2/pkg/namestore"
4344
"github.com/containerd/nerdctl/v2/pkg/netutil"
4445
"github.com/containerd/nerdctl/v2/pkg/netutil/nettype"
@@ -92,6 +93,26 @@ func Run(stdin io.Reader, stderr io.Writer, event, dataStore, cniPath, cniNetcon
9293
}
9394
}()
9495

96+
// FIXME: CNI plugins are not safe to use concurrently
97+
// See
98+
// https://github.com/containerd/nerdctl/issues/3518
99+
// https://github.com/containerd/nerdctl/issues/2908
100+
// and likely others
101+
// Fixing these issues would require a lot of work, possibly even stopping using individual cni binaries altogether
102+
// or at least being very mindful in what operation we call inside CNIEnv at what point, with filesystem locking.
103+
// This below is a stopgap solution that just enforces a global lock
104+
// Note this here is probably not enough, as concurrent CNI operations may happen outside of the scope of ocihooks
105+
// through explicit calls to Remove, etc.
106+
err = os.MkdirAll(cniNetconfPath, 0o700)
107+
if err != nil {
108+
return err
109+
}
110+
lock, err := lockutil.Lock(cniNetconfPath)
111+
if err != nil {
112+
return err
113+
}
114+
defer lockutil.Unlock(lock)
115+
95116
opts, err := newHandlerOpts(&state, dataStore, cniPath, cniNetconfPath)
96117
if err != nil {
97118
return err

0 commit comments

Comments
 (0)