Skip to content

Commit

Permalink
BACKPORT: Re-export container state's ExitCode and Error fields
Browse files Browse the repository at this point in the history
Upstream reference: moby#26158
Fix BZ#1413954

Those are needed in order to reload their value upon docker daemon
restart.

Add integration test to check persistence of exitcode and error message

Signed-off-by: Kenfe-Mickael Laventure <mickael.laventure@gmail.com>
  • Loading branch information
mlaventure authored and runcom committed Jan 18, 2017
1 parent f499e8b commit 037a2f5
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 21 deletions.
61 changes: 43 additions & 18 deletions container/state.go
Expand Up @@ -24,14 +24,40 @@ type State struct {
RemovalInProgress bool // Not need for this to be persistent on disk.
Dead bool
Pid int
exitCode int
error string // contains last known error when starting the container
ExitCodeValue int `json:"ExitCode"`
ErrorMsg string `json:"Error"` // contains last known error when starting the container
StartedAt time.Time
FinishedAt time.Time
waitChan chan struct{}
Health *Health
}

// StateStatus is used to return an error type implementing both
// exec.ExitCode and error.
// This type is needed as State include a sync.Mutex field which make
// copying it unsafe.
type StateStatus struct {
exitCode int
error string
}

func newStateStatus(ec int, err string) *StateStatus {
return &StateStatus{
exitCode: ec,
error: err,
}
}

// ExitCode returns current exitcode for the state.
func (ss *StateStatus) ExitCode() int {
return ss.exitCode
}

// Error returns current error for the state.
func (ss *StateStatus) Error() string {
return ss.error
}

// NewState creates a default state object with a fresh channel for state changes.
func NewState() *State {
return &State{
Expand All @@ -46,7 +72,7 @@ func (s *State) String() string {
return fmt.Sprintf("Up %s (Paused)", units.HumanDuration(time.Now().UTC().Sub(s.StartedAt)))
}
if s.Restarting {
return fmt.Sprintf("Restarting (%d) %s ago", s.exitCode, units.HumanDuration(time.Now().UTC().Sub(s.FinishedAt)))
return fmt.Sprintf("Restarting (%d) %s ago", s.ExitCodeValue, units.HumanDuration(time.Now().UTC().Sub(s.FinishedAt)))
}

if h := s.Health; h != nil {
Expand All @@ -71,7 +97,7 @@ func (s *State) String() string {
return ""
}

return fmt.Sprintf("Exited (%d) %s ago", s.exitCode, units.HumanDuration(time.Now().UTC().Sub(s.FinishedAt)))
return fmt.Sprintf("Exited (%d) %s ago", s.ExitCodeValue, units.HumanDuration(time.Now().UTC().Sub(s.FinishedAt)))
}

// StateString returns a single string to describe state
Expand Down Expand Up @@ -129,7 +155,7 @@ func wait(waitChan <-chan struct{}, timeout time.Duration) error {
func (s *State) WaitStop(timeout time.Duration) (int, error) {
s.Lock()
if !s.Running {
exitCode := s.exitCode
exitCode := s.ExitCodeValue
s.Unlock()
return exitCode, nil
}
Expand All @@ -149,24 +175,24 @@ func (s *State) WaitWithContext(ctx context.Context) error {
// todo(tonistiigi): make other wait functions use this
s.Lock()
if !s.Running {
state := *s
state := newStateStatus(s.ExitCode(), s.Error())
defer s.Unlock()
if state.exitCode == 0 {
if state.ExitCode() == 0 {
return nil
}
return &state
return state
}
waitChan := s.waitChan
s.Unlock()
select {
case <-waitChan:
s.Lock()
state := *s
state := newStateStatus(s.ExitCode(), s.Error())
s.Unlock()
if state.exitCode == 0 {
if state.ExitCode() == 0 {
return nil
}
return &state
return state
case <-ctx.Done():
return ctx.Err()
}
Expand All @@ -191,23 +217,22 @@ func (s *State) GetPID() int {
// ExitCode returns current exitcode for the state. Take lock before if state
// may be shared.
func (s *State) ExitCode() int {
res := s.exitCode
return res
return s.ExitCodeValue
}

// SetExitCode sets current exitcode for the state. Take lock before if state
// may be shared.
func (s *State) SetExitCode(ec int) {
s.exitCode = ec
s.ExitCodeValue = ec
}

// SetRunning sets the state of the container to "running".
func (s *State) SetRunning(pid int, initial bool) {
s.error = ""
s.ErrorMsg = ""
s.Running = true
s.Paused = false
s.Restarting = false
s.exitCode = 0
s.ExitCodeValue = 0
s.Pid = pid
if initial {
s.StartedAt = time.Now().UTC()
Expand Down Expand Up @@ -259,7 +284,7 @@ func (s *State) SetRestarting(exitStatus *ExitStatus) {
// know the error that occurred when container transits to another state
// when inspecting it
func (s *State) SetError(err error) {
s.error = err.Error()
s.ErrorMsg = err.Error()
}

// IsPaused returns whether the container is paused or not.
Expand Down Expand Up @@ -306,5 +331,5 @@ func (s *State) SetDead() {

// Error returns current error for the state.
func (s *State) Error() string {
return s.error
return s.ErrorMsg
}
2 changes: 1 addition & 1 deletion container/state_solaris.go
Expand Up @@ -3,5 +3,5 @@ package container
// setFromExitStatus is a platform specific helper function to set the state
// based on the ExitStatus structure.
func (s *State) setFromExitStatus(exitStatus *ExitStatus) {
s.exitCode = exitStatus.ExitCode
s.ExitCodeValue = exitStatus.ExitCode
}
2 changes: 1 addition & 1 deletion container/state_unix.go
Expand Up @@ -5,6 +5,6 @@ package container
// setFromExitStatus is a platform specific helper function to set the state
// based on the ExitStatus structure.
func (s *State) setFromExitStatus(exitStatus *ExitStatus) {
s.exitCode = exitStatus.ExitCode
s.ExitCodeValue = exitStatus.ExitCode
s.OOMKilled = exitStatus.OOMKilled
}
2 changes: 1 addition & 1 deletion container/state_windows.go
Expand Up @@ -3,5 +3,5 @@ package container
// setFromExitStatus is a platform specific helper function to set the state
// based on the ExitStatus structure.
func (s *State) setFromExitStatus(exitStatus *ExitStatus) {
s.exitCode = exitStatus.ExitCode
s.ExitCodeValue = exitStatus.ExitCode
}
38 changes: 38 additions & 0 deletions integration-cli/docker_cli_daemon_test.go
Expand Up @@ -2749,3 +2749,41 @@ func (s *DockerDaemonSuite) TestRestartPolicyWithLiveRestore(c *check.C) {
}
}
}

func (s *DockerDaemonSuite) TestDaemonRestartSaveContainerExitCode(c *check.C) {
err := s.d.StartWithBusybox()
c.Assert(err, checker.IsNil)

containerName := "error-values"
runError := "oci runtime error: exec: \"toto\": executable file not found in $PATH"
// Make a container with both a non 0 exit code and an error message
out, err := s.d.Cmd("run", "--name", containerName, "busybox", "toto")
c.Assert(err, checker.NotNil)
c.Assert(out, checker.Contains, runError)

// Check that those values were saved on disk
out, err = s.d.Cmd("inspect", "-f", "{{.State.ExitCode}}", containerName)
out = strings.TrimSpace(out)
c.Assert(err, checker.IsNil)
c.Assert(out, checker.Equals, "127")

out, err = s.d.Cmd("inspect", "-f", "{{.State.Error}}", containerName)
out = strings.TrimSpace(out)
c.Assert(err, checker.IsNil)
c.Assert(out, checker.Equals, runError)

// now restart daemon
err = s.d.Restart()
c.Assert(err, checker.IsNil)

// Check that those values are still around
out, err = s.d.Cmd("inspect", "-f", "{{.State.ExitCode}}", containerName)
out = strings.TrimSpace(out)
c.Assert(err, checker.IsNil)
c.Assert(out, checker.Equals, "127")

out, err = s.d.Cmd("inspect", "-f", "{{.State.Error}}", containerName)
out = strings.TrimSpace(out)
c.Assert(err, checker.IsNil)
c.Assert(out, checker.Equals, runError)
}

0 comments on commit 037a2f5

Please sign in to comment.