Skip to content

Commit 1122644

Browse files
authored
Merge pull request containerd#3543 from apostasie/lock-compose
Global compose lock
2 parents d55f71e + fa39963 commit 1122644

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

pkg/cmd/compose/compose.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,31 @@ import (
3535
"github.com/containerd/nerdctl/v2/pkg/composer/serviceparser"
3636
"github.com/containerd/nerdctl/v2/pkg/imgutil"
3737
"github.com/containerd/nerdctl/v2/pkg/ipfs"
38+
"github.com/containerd/nerdctl/v2/pkg/lockutil"
3839
"github.com/containerd/nerdctl/v2/pkg/netutil"
3940
"github.com/containerd/nerdctl/v2/pkg/referenceutil"
4041
"github.com/containerd/nerdctl/v2/pkg/signutil"
4142
"github.com/containerd/nerdctl/v2/pkg/strutil"
4243
)
4344

45+
//nolint:unused
46+
var locked *os.File
47+
4448
// New returns a new *composer.Composer.
4549
func New(client *containerd.Client, globalOptions types.GlobalCommandOptions, options composer.Options, stdout, stderr io.Writer) (*composer.Composer, error) {
50+
// Compose right now cannot be made safe to use concurrently, as we shell out to nerdctl for multiple operations,
51+
// preventing us from using the lock mechanisms from the API.
52+
// This here imposes a global lock, effectively preventing multiple compose commands from being run in parallel and
53+
// preventing some of the problems with concurrent execution.
54+
// This should be removed once we have better, in-depth solutions to make this concurrency safe.
55+
// Note that we do not close the lock explicitly. Instead, the lock will get released when the `locked` global
56+
// variable will get collected and the file descriptor closed (eg: when the binary exits).
57+
var err error
58+
locked, err = lockutil.Lock(globalOptions.DataRoot)
59+
if err != nil {
60+
return nil, err
61+
}
62+
4663
cniEnv, err := netutil.NewCNIEnv(globalOptions.CNIPath, globalOptions.CNINetConfPath, netutil.WithNamespace(globalOptions.Namespace), netutil.WithDefaultNetwork())
4764
if err != nil {
4865
return nil, err

0 commit comments

Comments
 (0)