Move call to Daemon.registerLinks out of Daemon.setHostConfig

The call from Daemon.create -> Daemon.setHostConfig acquired
container.Lock, but didn't need to because the container is
newly created and solely owned by the caller. The call from
Daemon.restore did not acquire the lock.

Signed-off-by: Rob Murray <rob.murray@docker.com>
This commit is contained in:
Rob Murray
2025-11-14 11:27:52 +00:00
parent 92b4902b8d
commit e757bbb4ea
5 changed files with 9 additions and 12 deletions

View File

@@ -221,11 +221,6 @@ func (daemon *Daemon) setHostConfig(container *container.Container, hostConfig *
container.Lock() container.Lock()
defer container.Unlock() defer container.Unlock()
// Register any links from the host config before starting the container
if err := daemon.registerLinks(container, hostConfig); err != nil {
return err
}
if hostConfig != nil && hostConfig.NetworkMode == "" { if hostConfig != nil && hostConfig.NetworkMode == "" {
hostConfig.NetworkMode = networktypes.NetworkDefault hostConfig.NetworkMode = networktypes.NetworkDefault
} }

View File

@@ -247,7 +247,9 @@ func (daemon *Daemon) create(ctx context.Context, daemonCfg *config.Config, opts
if err := daemon.setHostConfig(ctr, opts.params.HostConfig, opts.params.DefaultReadOnlyNonRecursive); err != nil { if err := daemon.setHostConfig(ctr, opts.params.HostConfig, opts.params.DefaultReadOnlyNonRecursive); err != nil {
return nil, err return nil, err
} }
if err := daemon.registerLinks(ctr); err != nil {
return nil, err
}
if err := daemon.createContainerOSSpecificSettings(ctx, ctr, opts.params.Config, opts.params.HostConfig); err != nil { if err := daemon.createContainerOSSpecificSettings(ctx, ctr, opts.params.Config, opts.params.HostConfig); err != nil {
return nil, err return nil, err
} }

View File

@@ -592,7 +592,7 @@ func (daemon *Daemon) restore(ctx context.Context, cfg *configStore, containers
go func(c *container.Container) { go func(c *container.Container) {
_ = sem.Acquire(context.Background(), 1) _ = sem.Acquire(context.Background(), 1)
if err := daemon.registerLinks(c, c.HostConfig); err != nil { if err := daemon.registerLinks(c); err != nil {
log.G(ctx).WithField("container", c.ID).WithError(err).Error("failed to register link for container") log.G(ctx).WithField("container", c.ID).WithError(err).Error("failed to register link for container")
} }

View File

@@ -1504,12 +1504,12 @@ func getUnmountOnShutdownPath(config *config.Config) string {
// registerLinks registers network links between container and other containers // registerLinks registers network links between container and other containers
// with the daemon using the specification in hostConfig. // with the daemon using the specification in hostConfig.
func (daemon *Daemon) registerLinks(container *container.Container, hostConfig *containertypes.HostConfig) error { func (daemon *Daemon) registerLinks(ctr *container.Container) error {
if hostConfig == nil || hostConfig.NetworkMode.IsUserDefined() { if ctr.HostConfig == nil || ctr.HostConfig.NetworkMode.IsUserDefined() {
return nil return nil
} }
for _, l := range hostConfig.Links { for _, l := range ctr.HostConfig.Links {
name, alias, err := opts.ParseLink(l) name, alias, err := opts.ParseLink(l)
if err != nil { if err != nil {
return err return err
@@ -1542,7 +1542,7 @@ func (daemon *Daemon) registerLinks(container *container.Container, hostConfig *
if child.HostConfig.NetworkMode.IsHost() { if child.HostConfig.NetworkMode.IsHost() {
return cerrdefs.ErrInvalidArgument.WithMessage("conflicting options: host type networking can't be used with links. This would result in undefined behavior") return cerrdefs.ErrInvalidArgument.WithMessage("conflicting options: host type networking can't be used with links. This would result in undefined behavior")
} }
if err := daemon.registerLink(container, child, alias); err != nil { if err := daemon.registerLink(ctr, child, alias); err != nil {
return err return err
} }
} }

View File

@@ -464,7 +464,7 @@ func initBridgeDriver(controller *libnetwork.Controller, config config.BridgeCon
// registerLinks sets up links between containers and writes the // registerLinks sets up links between containers and writes the
// configuration out for persistence. As of Windows TP4, links are not supported. // configuration out for persistence. As of Windows TP4, links are not supported.
func (daemon *Daemon) registerLinks(container *container.Container, hostConfig *containertypes.HostConfig) error { func (daemon *Daemon) registerLinks(container *container.Container) error {
return nil return nil
} }