Skip to content

Commit 0bb67bd

Browse files
moyprayruncom
authored andcommitted
When daemon is in startup process, could not start container
Description: When docker is in startup process and containerd sends an "process exit" event to docker. If the container config '--restart=always', restartmanager will start this container very soon. But some initialization is not done, e.g. `daemon.netController`,when visit, docker would panic. Signed-off-by: Wentao Zhang <zhangwentao234@huawei.com>
1 parent ce9c6dd commit 0bb67bd

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

daemon/daemon.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ type Daemon struct {
110110
seccompProfile []byte
111111
seccompProfilePath string
112112

113-
hosts map[string]bool // hosts stores the addresses the daemon is listening on
113+
hosts map[string]bool // hosts stores the addresses the daemon is listening on
114+
startupDone chan struct{}
114115
}
115116

116117
// StoreHosts stores the addresses the daemon is listening on
@@ -542,7 +543,10 @@ func NewDaemon(config *Config, registryService registry.Service, containerdRemot
542543
}
543544
os.Setenv("TMPDIR", realTmp)
544545

545-
d := &Daemon{configStore: config}
546+
d := &Daemon{
547+
configStore: config,
548+
startupDone: make(chan struct{}),
549+
}
546550
// Ensure the daemon is properly shutdown if there is a failure during
547551
// initialization
548552
defer func() {
@@ -726,6 +730,7 @@ func NewDaemon(config *Config, registryService registry.Service, containerdRemot
726730
if err := d.restore(); err != nil {
727731
return nil, err
728732
}
733+
close(d.startupDone)
729734

730735
// FIXME: this method never returns an error
731736
info, _ := d.SystemInfo()
@@ -752,6 +757,10 @@ func NewDaemon(config *Config, registryService registry.Service, containerdRemot
752757
return d, nil
753758
}
754759

760+
func (daemon *Daemon) waitForStartupDone() {
761+
<-daemon.startupDone
762+
}
763+
755764
func (daemon *Daemon) shutdownContainer(c *container.Container) error {
756765
stopTimeout := c.StopTimeout()
757766
// TODO(windows): Handle docker restart with paused containers

daemon/monitor.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ func (daemon *Daemon) StateChanged(id string, e libcontainerd.StateInfo) error {
5656
go func() {
5757
err := <-wait
5858
if err == nil {
59+
// daemon.netController is initialized when daemon is restoring containers.
60+
// But containerStart will use daemon.netController segment.
61+
// So to avoid panic at startup process, here must wait util daemon restore done.
62+
daemon.waitForStartupDone()
5963
if err = daemon.containerStart(c, "", "", false); err != nil {
6064
logrus.Debugf("failed to restart container: %+v", err)
6165
}

0 commit comments

Comments
 (0)