diff --git a/pkg/compose/compose.go b/pkg/compose/compose.go index 73710bedc..4851de852 100644 --- a/pkg/compose/compose.go +++ b/pkg/compose/compose.go @@ -24,6 +24,7 @@ import ( "strconv" "strings" "sync" + "time" "github.com/compose-spec/compose-go/v2/types" "github.com/docker/cli/cli/command" @@ -70,6 +71,8 @@ func NewComposeService(dockerCli command.Cli, options ...Option) (api.Compose, e dockerCli: dockerCli, maxConcurrency: -1, dryRun: false, + + logStreamDrainTimeout: defaultLogStreamDrainTimeout, } // Look the config file up lazily: WithDryRun replaces s.dockerCli. s.auth = registry.NewDesktopAuthProvider(registry.AuthProviderFunc(func(registryHostname string) (clitypes.AuthConfig, error) { @@ -297,6 +300,17 @@ type composeService struct { auth registry.AuthProvider runtimeAPIVersion runtimeVersionCache + + // swarm caches the daemon's swarm mode, see isSwarmEnabled. + swarm swarmState + + // pluginMu guards the project's services while concurrent provider runs + // inject their variables into the services depending on them, see runPlugin. + pluginMu sync.Mutex + + // logStreamDrainTimeout bounds the shutdown drain of re-attached log + // streams, see drainTimeout. + logStreamDrainTimeout time.Duration } // Close releases any connections/resources held by the underlying clients. @@ -575,26 +589,30 @@ func (s *composeService) actualNetworks(ctx context.Context, projectName string) return actual, nil } -var swarmEnabled = struct { +// swarmState caches whether the daemon this service talks to runs in swarm +// mode. The answer belongs to the daemon behind the service's client, so it is +// held per composeService rather than shared by every instance in the process. +type swarmState struct { once sync.Once val bool err error -}{} +} func (s *composeService) isSwarmEnabled(ctx context.Context) (bool, error) { - swarmEnabled.once.Do(func() { + st := &s.swarm + st.once.Do(func() { res, err := s.apiClient().Info(ctx, client.InfoOptions{}) if err != nil { - swarmEnabled.err = err + st.err = err } switch res.Info.Swarm.LocalNodeState { case swarm.LocalNodeStateInactive, swarm.LocalNodeStateLocked: - swarmEnabled.val = false + st.val = false default: - swarmEnabled.val = true + st.val = true } }) - return swarmEnabled.val, swarmEnabled.err + return st.val, st.err } // runtimeVersionCache caches a version string after a successful lookup. diff --git a/pkg/compose/compose_test.go b/pkg/compose/compose_test.go index 71eec918f..81f478e73 100644 --- a/pkg/compose/compose_test.go +++ b/pkg/compose/compose_test.go @@ -21,6 +21,9 @@ import ( "testing" "time" + "github.com/moby/moby/api/types/swarm" + "github.com/moby/moby/client" + "go.uber.org/mock/gomock" "gotest.tools/v3/assert" ) @@ -52,3 +55,45 @@ func TestNewLimitedErrgroup_NonPositiveIsUnlimited(t *testing.T) { }) } } + +func swarmInfo(state swarm.LocalNodeState) client.SystemInfoResult { + res := client.SystemInfoResult{} + res.Info.Swarm.LocalNodeState = state + return res +} + +// TestIsSwarmEnabled_PerInstance guards that the swarm answer is cached per +// service: two services talking to different daemons must each report their +// own daemon's mode instead of sharing the first answer given in the process. +func TestIsSwarmEnabled_PerInstance(t *testing.T) { + inactive, inactiveClient := newTestService(t) + inactiveClient.EXPECT().Info(gomock.Any(), gomock.Any()).Times(1). + Return(swarmInfo(swarm.LocalNodeStateInactive), nil) + active, activeClient := newTestService(t) + activeClient.EXPECT().Info(gomock.Any(), gomock.Any()).Times(1). + Return(swarmInfo(swarm.LocalNodeStateActive), nil) + + for range 2 { // second round is served from each instance's own cache + enabled, err := inactive.isSwarmEnabled(t.Context()) + assert.NilError(t, err) + assert.Assert(t, !enabled) + + enabled, err = active.isSwarmEnabled(t.Context()) + assert.NilError(t, err) + assert.Assert(t, enabled) + } +} + +// TestDrainTimeout guards the per-instance log drain bound and its fallback +// for a composeService not built by NewComposeService. +func TestDrainTimeout(t *testing.T) { + svc, _ := newTestService(t) + assert.Equal(t, svc.drainTimeout(), defaultLogStreamDrainTimeout) + + svc.logStreamDrainTimeout = time.Millisecond + other, _ := newTestService(t) + assert.Equal(t, svc.drainTimeout(), time.Millisecond) + assert.Equal(t, other.drainTimeout(), defaultLogStreamDrainTimeout) + + assert.Equal(t, (&composeService{}).drainTimeout(), defaultLogStreamDrainTimeout) +} diff --git a/pkg/compose/plugins.go b/pkg/compose/plugins.go index 50aef6d71..595eaa13c 100644 --- a/pkg/compose/plugins.go +++ b/pkg/compose/plugins.go @@ -131,8 +131,6 @@ type pluginVariables struct { endpoints map[int]string } -var mux sync.Mutex - func (s *composeService) runPlugin(ctx context.Context, project *types.Project, service types.ServiceConfig, command string, extraArgs ...string) error { provider := *service.Provider @@ -167,7 +165,7 @@ func (s *composeService) runPlugin(ctx context.Context, project *types.Project, // in ensureServiceRelay does not: holding the lock across it would make // every concurrent provider wait on the slowest one (image pull // included), so the relay is deployed after the lock is released. - mux.Lock() + s.pluginMu.Lock() for name, s := range project.Services { if _, ok := s.DependsOn[service.Name]; ok { prefix := strings.ToUpper(service.Name) + "_" @@ -187,7 +185,7 @@ func (s *composeService) runPlugin(ctx context.Context, project *types.Project, if deployRelay { networkKeys = relayNetworks(project, service) } - mux.Unlock() + s.pluginMu.Unlock() if isUp { if deployRelay { diff --git a/pkg/compose/service_containers.go b/pkg/compose/service_containers.go index a004f9d26..17c332067 100644 --- a/pkg/compose/service_containers.go +++ b/pkg/compose/service_containers.go @@ -399,6 +399,12 @@ func (s *composeService) createContainer(ctx context.Context, project *types.Pro // concurrent starts can be assigned the same port. Every code path calling // ContainerStart on a service container must hold it (the plan executor's // execStartContainer and the imperative startServiceContainer both do). +// +// It deliberately stays a package-level variable rather than a composeService +// field: the race lives in the daemon, not in a service instance, so two +// instances (e.g. SDK users) driving the same daemon must still exclude each +// other. A per-instance lock would silently reintroduce the duplicate-port +// assignment between them. var startMx sync.Mutex func (s *composeService) createMobyContainer(ctx context.Context, project *types.Project, service types.ServiceConfig, diff --git a/pkg/compose/up.go b/pkg/compose/up.go index 0df95ad41..2826ff9f8 100644 --- a/pkg/compose/up.go +++ b/pkg/compose/up.go @@ -208,7 +208,7 @@ func (s *composeService) runInteractiveUp(ctx context.Context, project *types.Pr }() select { case <-drained: - case <-time.After(logStreamDrainTimeout): + case <-time.After(u.drainTimeout()): case <-globalCtx.Done(): } // cancel the global context to terminate signal-handler goroutines @@ -426,15 +426,23 @@ func (u *upSession) captureExitCodeFrom() api.ContainerEventListener { } } +// defaultLogStreamDrainTimeout is the default bound on the shutdown drain of the +// streams opened by followStartedContainers: EOF is guaranteed once the containers exited, the bound only protects against a +// wedged daemon holding the connection open. It is a composeService field so +// tests can shrink it without touching package state. +const defaultLogStreamDrainTimeout = 5 * time.Second + +// drainTimeout returns the configured drain bound, falling back to the default +// for a composeService not built by NewComposeService. +func (s *composeService) drainTimeout() time.Duration { + if s.logStreamDrainTimeout > 0 { + return s.logStreamDrainTimeout + } + return defaultLogStreamDrainTimeout +} + // followStartedContainers streams logs of containers (re)started after `up`, // so they are followed like the initially attached ones. -// -// logStreamDrainTimeout bounds the shutdown drain of these streams: EOF is -// guaranteed once the containers exited, the bound only protects against a -// wedged daemon holding the connection open. A variable so tests can shrink -// it. -var logStreamDrainTimeout = 5 * time.Second - func (u *upSession) followStartedContainers(attached []string) api.ContainerEventListener { runEnds := newRunEndTracker() cursors := newLogCursors()