From 1d2208fed90da9ab72ded3b1c65bb2a71b66ce93 Mon Sep 17 00:00:00 2001 From: Zhang Wei Date: Mon, 25 Jan 2016 23:38:03 +0800 Subject: [PATCH] Forbid exec a restarting container Currently if we exec a restarting container, client will fail silently, and daemon will print error that container can't be found which is not a very meaningful prompt to user. This commit will stop user from exec a restarting container and gives more explicit error message. Signed-off-by: Zhang Wei --- container/state.go | 8 ++++++++ daemon/exec.go | 6 ++++++ errors/daemon.go | 9 +++++++++ 3 files changed, 23 insertions(+) diff --git a/container/state.go b/container/state.go index 138d79874f..4a923aa968 100644 --- a/container/state.go +++ b/container/state.go @@ -247,6 +247,14 @@ func (s *State) IsPaused() bool { return res } +// IsRestarting returns whether the container is restarting or not. +func (s *State) IsRestarting() bool { + s.Lock() + res := s.Restarting + s.Unlock() + return res +} + // SetRemovalInProgress sets the container state as being removed. func (s *State) SetRemovalInProgress() error { s.Lock() diff --git a/daemon/exec.go b/daemon/exec.go index 4dba7c9d47..7772790d6e 100644 --- a/daemon/exec.go +++ b/daemon/exec.go @@ -52,6 +52,9 @@ func (d *Daemon) getExecConfig(name string) (*exec.Config, error) { if container.IsPaused() { return nil, derr.ErrorCodeExecPaused.WithArgs(container.ID) } + if container.IsRestarting() { + return nil, derr.ErrorCodeExecRestarting.WithArgs(container.ID) + } return ec, nil } } @@ -76,6 +79,9 @@ func (d *Daemon) getActiveContainer(name string) (*container.Container, error) { if container.IsPaused() { return nil, derr.ErrorCodeExecPaused.WithArgs(name) } + if container.IsRestarting() { + return nil, derr.ErrorCodeExecRestarting.WithArgs(name) + } return container, nil } diff --git a/errors/daemon.go b/errors/daemon.go index a2caabb16b..78c4422ab1 100644 --- a/errors/daemon.go +++ b/errors/daemon.go @@ -733,6 +733,15 @@ var ( HTTPStatusCode: http.StatusConflict, }) + // ErrorCodeExecRestarting is generated when we try to start an exec + // but the container is restarting. + ErrorCodeExecRestarting = errcode.Register(errGroup, errcode.ErrorDescriptor{ + Value: "EXECRESTARTING", + Message: "Container %s is restarting, wait until the container is running", + Description: "An attempt to start an 'exec' was made, but the owning container is restarting", + HTTPStatusCode: http.StatusConflict, + }) + // ErrorCodeExecRunning is generated when we try to start an exec // but its already running. ErrorCodeExecRunning = errcode.Register(errGroup, errcode.ErrorDescriptor{