refactor(compose): hold per-service state on composeService instead of package globals

The daemon's swarm mode cache, the provider variable-injection lock and
the log-stream drain bound now live on composeService, so several
services in one process (SDK users, tests) no longer share them.
startMx stays global on purpose: it protects port allocation in the
daemon, which instances sharing a daemon must still serialize.

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2026-10-10 14:07:01 +02:00 • committed by Guillaume Lours
parent e5b13e7695
commit 57fd13053e
5 changed files with 94 additions and 19 deletions

View file

@ -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.

View file

@ -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)
}

View file

@ -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 {

View file

@ -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,

View file

@ -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()