-
Notifications
You must be signed in to change notification settings - Fork 651
[FIX] CNIEnv concurrency issue #4202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
AkihiroSuda
merged 1 commit into
containerd:main
from
apostasie:2025-05-cni-concurrency-issue
May 10, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
Copyright The containerd Authors. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package netutil | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/containernetworking/cni/libcni" | ||
|
||
"github.com/containerd/errdefs" | ||
|
||
"github.com/containerd/nerdctl/v2/pkg/lockutil" | ||
) | ||
|
||
// NOTE: libcni is not safe to use concurrently - or at least delegates concurrency management to the consumer. | ||
// Furthermore, CNIEnv (prior to this) is assuming the filesystem is ACID and other TOCTOU faults. | ||
// This small set of methods here are meant to isolate CNIEnv entirely from the filesystem. | ||
// This is NOT proper - we should instead use the Store implementation, which is the generic abstraction for ACID | ||
// operations - but for now that will do, waiting for a full rewrite of CNIEnv. | ||
|
||
func fsEnsureRoot(e *CNIEnv, namespace string) error { | ||
path := e.NetconfPath | ||
if namespace != "" { | ||
path = filepath.Join(e.NetconfPath, namespace) | ||
} | ||
return os.MkdirAll(path, 0755) | ||
} | ||
|
||
func fsRemove(e *CNIEnv, net *NetworkConfig) error { | ||
fn := func() error { | ||
if err := os.RemoveAll(net.File); err != nil { | ||
return err | ||
} | ||
return net.clean() | ||
} | ||
return lockutil.WithDirLock(filepath.Join(e.NetconfPath, ".nerdctl.lock"), fn) | ||
} | ||
|
||
func fsExists(e *CNIEnv, name string) (bool, error) { | ||
fi, err := os.Stat(getConfigPathForNetworkName(e, name)) | ||
return !os.IsNotExist(err) && !fi.IsDir(), err | ||
} | ||
|
||
func fsWrite(e *CNIEnv, net *NetworkConfig) error { | ||
filename := getConfigPathForNetworkName(e, net.Name) | ||
// FIXME: note that this is still problematic. | ||
// Concurrent access may independently first figure out that a given network is missing, and while the lock | ||
// here will prevent concurrent writes, one of the routines will fail. | ||
// Consuming code MUST account for that scenario. | ||
return lockutil.WithDirLock(filepath.Join(e.NetconfPath, ".nerdctl.lock"), func() error { | ||
if _, err := os.Stat(filename); err == nil { | ||
return errdefs.ErrAlreadyExists | ||
} | ||
return os.WriteFile(filename, net.Bytes, 0644) | ||
}) | ||
} | ||
|
||
func fsRead(e *CNIEnv) ([]*NetworkConfig, error) { | ||
var nc []*NetworkConfig | ||
var err error | ||
err = lockutil.WithDirLock(filepath.Join(e.NetconfPath, ".nerdctl.lock"), func() error { | ||
namespaced := []string{} | ||
var common []string | ||
common, err = libcni.ConfFiles(e.NetconfPath, []string{".conf", ".conflist", ".json"}) | ||
if err != nil { | ||
return err | ||
} | ||
if e.Namespace != "" { | ||
namespaced, err = libcni.ConfFiles(filepath.Join(e.NetconfPath, e.Namespace), []string{".conf", ".conflist", ".json"}) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
nc, err = cniLoad(append(common, namespaced...)) | ||
return err | ||
}) | ||
return nc, err | ||
} | ||
|
||
func getConfigPathForNetworkName(e *CNIEnv, netName string) string { | ||
if netName == DefaultNetworkName || e.Namespace == "" { | ||
return filepath.Join(e.NetconfPath, "nerdctl-"+netName+".conflist") | ||
} | ||
return filepath.Join(e.NetconfPath, e.Namespace, "nerdctl-"+netName+".conflist") | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.nerdctl.lock
is used to lock cni conf file manipulation. This lock here is a global locking mechanism meant to prevent cni plugins from acting concurrently.This lock is enforced for almost the entirety of the ocihook flow.
It was previously using the same lock, as we were not locking the file operations in cnienv that are used in oci-hooks.
Since we do now, it can no longer use the same lock.
The note around line 100 explains some of that.