diff --git a/daemon/monitor.go b/daemon/monitor.go index e0fdece14e..e8940db24c 100644 --- a/daemon/monitor.go +++ b/daemon/monitor.go @@ -103,8 +103,17 @@ func (m *containerMonitor) Start() error { exitStatus int ) + // this variable indicates that we under container.Lock + underLock := true + // ensure that when the monitor finally exits we release the networking and unmount the rootfs - defer m.Close() + defer func() { + if !underLock { + m.container.Lock() + defer m.container.Unlock() + } + m.Close() + }() // reset the restart count m.container.RestartCount = -1 @@ -136,6 +145,9 @@ func (m *containerMonitor) Start() error { log.Errorf("Error running container: %s", err) } + // here container.Lock is already lost + underLock = false + m.resetMonitor(err == nil && exitStatus == 0) if m.shouldRestart(exitStatus) {