2018-02-05 21:05:59 +00:00
|
|
|
package container // import "github.com/docker/docker/container"
|
2013-01-19 00:13:39 +00:00
|
|
|
|
|
|
|
import (
|
2017-03-30 20:52:40 +00:00
|
|
|
"errors"
|
2013-03-12 12:36:37 +00:00
|
|
|
"fmt"
|
2013-04-09 16:09:54 +00:00
|
|
|
"sync"
|
2013-01-28 22:30:05 +00:00
|
|
|
"time"
|
2014-05-12 23:40:19 +00:00
|
|
|
|
2016-06-14 02:52:49 +00:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
2016-07-15 18:21:19 +00:00
|
|
|
"github.com/docker/docker/api/types"
|
2015-12-16 17:26:49 +00:00
|
|
|
"github.com/docker/go-units"
|
2013-01-19 00:13:39 +00:00
|
|
|
)
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
// State holds the current container state, and has methods to get and
|
|
|
|
// set the state. Container has an embed, which allows all of the
|
|
|
|
// functions defined against State to run against Container.
|
2013-01-19 00:13:39 +00:00
|
|
|
type State struct {
|
2014-08-28 11:39:27 +00:00
|
|
|
sync.Mutex
|
2017-05-19 10:29:54 +00:00
|
|
|
// Note that `Running` and `Paused` are not mutually exclusive:
|
|
|
|
// When pausing a container (on Linux), the cgroups freezer is used to suspend
|
|
|
|
// all processes in the container. Freezing the process requires the process to
|
|
|
|
// be running. As a result, paused containers are both `Running` _and_ `Paused`.
|
2015-03-12 19:26:17 +00:00
|
|
|
Running bool
|
|
|
|
Paused bool
|
|
|
|
Restarting bool
|
|
|
|
OOMKilled bool
|
2015-11-12 19:55:17 +00:00
|
|
|
RemovalInProgress bool // Not need for this to be persistent on disk.
|
2015-03-12 19:26:17 +00:00
|
|
|
Dead bool
|
|
|
|
Pid int
|
2016-08-30 16:44:06 +00:00
|
|
|
ExitCodeValue int `json:"ExitCode"`
|
Windows: Pass back system errors on container exit
Signed-off-by: John Howard <jhoward@microsoft.com>
While debugging #32838, it was found (https://github.com/moby/moby/issues/32838#issuecomment-356005845) that the utility VM in some circumstances was crashing. Unfortunately, this was silently thrown away, and as far as the build step (also applies to docker run) was concerned, the exit code was zero and the error was thrown away. Windows containers operate differently to containers on Linux, and there can be legitimate system errors during container shutdown after the init process exits. This PR handles this and passes the error all the way back to the client, and correctly causes a build step running a container which hits a system error to fail, rather than blindly trying to keep going, assuming all is good, and get a subsequent failure on a commit.
With this change, assuming an error occurs, here's an example of a failure which previous was reported as a commit error:
```
The command 'powershell -Command $ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue'; Install-WindowsFeature -Name Web-App-Dev ; Install-WindowsFeature -Name ADLDS; Install-WindowsFeature -Name Web-Mgmt-Compat; Install-WindowsFeature -Name Web-Mgmt-Service; Install-WindowsFeature -Name Web-Metabase; Install-WindowsFeature -Name Web-Lgcy-Scripting; Install-WindowsFeature -Name Web-WMI; Install-WindowsFeature -Name Web-WHC; Install-WindowsFeature -Name Web-Scripting-Tools; Install-WindowsFeature -Name Web-Net-Ext45; Install-WindowsFeature -Name Web-ASP; Install-WindowsFeature -Name Web-ISAPI-Ext; Install-WindowsFeature -Name Web-ISAPI-Filter; Install-WindowsFeature -Name Web-Default-Doc; Install-WindowsFeature -Name Web-Dir-Browsing; Install-WindowsFeature -Name Web-Http-Errors; Install-WindowsFeature -Name Web-Static-Content; Install-WindowsFeature -Name Web-Http-Redirect; Install-WindowsFeature -Name Web-DAV-Publishing; Install-WindowsFeature -Name Web-Health; Install-WindowsFeature -Name Web-Http-Logging; Install-WindowsFeature -Name Web-Custom-Logging; Install-WindowsFeature -Name Web-Log-Libraries; Install-WindowsFeature -Name Web-Request-Monitor; Install-WindowsFeature -Name Web-Http-Tracing; Install-WindowsFeature -Name Web-Stat-Compression; Install-WindowsFeature -Name Web-Dyn-Compression; Install-WindowsFeature -Name Web-Security; Install-WindowsFeature -Name Web-Windows-Auth; Install-WindowsFeature -Name Web-Basic-Auth; Install-WindowsFeature -Name Web-Url-Auth; Install-WindowsFeature -Name Web-WebSockets; Install-WindowsFeature -Name Web-AppInit; Install-WindowsFeature -Name NET-WCF-HTTP-Activation45; Install-WindowsFeature -Name NET-WCF-Pipe-Activation45; Install-WindowsFeature -Name NET-WCF-TCP-Activation45;' returned a non-zero code: 4294967295: container shutdown failed: container ba9c65054d42d4830fb25ef55e4ab3287550345aa1a2bb265df4e5bfcd79c78a encountered an error during WaitTimeout: failure in a Windows system call: The compute system exited unexpectedly. (0xc0370106)
```
Without this change, it would be incorrectly reported such as in this comment: https://github.com/moby/moby/issues/32838#issuecomment-309621097
```
Step 3/8 : ADD buildtools C:/buildtools
re-exec error: exit status 1: output: time="2017-06-20T11:37:38+10:00" level=error msg="hcsshim::ImportLayer failed in Win32: The system cannot find the path specified. (0x3) layerId=\\\\?\\C:\\ProgramData\\docker\\windowsfilter\\b41d28c95f98368b73fc192cb9205700e21
6691495c1f9ac79b9b04ec4923ea2 flavour=1 folder=C:\\Windows\\TEMP\\hcs232661915"
hcsshim::ImportLayer failed in Win32: The system cannot find the path specified. (0x3) layerId=\\?\C:\ProgramData\docker\windowsfilter\b41d28c95f98368b73fc192cb9205700e216691495c1f9ac79b9b04ec4923ea2 flavour=1 folder=C:\Windows\TEMP\hcs232661915
```
2018-01-09 19:46:29 +00:00
|
|
|
ErrorMsg string `json:"Error"` // contains last known error during container start, stop, or remove
|
2015-03-12 19:26:17 +00:00
|
|
|
StartedAt time.Time
|
|
|
|
FinishedAt time.Time
|
2016-04-18 09:48:13 +00:00
|
|
|
Health *Health
|
2017-03-30 20:52:40 +00:00
|
|
|
|
|
|
|
waitStop chan struct{}
|
|
|
|
waitRemove chan struct{}
|
2014-06-06 11:28:12 +00:00
|
|
|
}
|
|
|
|
|
2017-03-30 20:52:40 +00:00
|
|
|
// StateStatus is used to return container wait results.
|
|
|
|
// Implements exec.ExitCode interface.
|
2016-08-30 16:44:06 +00:00
|
|
|
// This type is needed as State include a sync.Mutex field which make
|
|
|
|
// copying it unsafe.
|
|
|
|
type StateStatus struct {
|
|
|
|
exitCode int
|
2017-03-30 20:52:40 +00:00
|
|
|
err error
|
2016-08-30 16:44:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ExitCode returns current exitcode for the state.
|
2017-03-31 03:01:41 +00:00
|
|
|
func (s StateStatus) ExitCode() int {
|
|
|
|
return s.exitCode
|
2016-08-30 16:44:06 +00:00
|
|
|
}
|
|
|
|
|
2017-03-30 20:52:40 +00:00
|
|
|
// Err returns current error for the state. Returns nil if the container had
|
|
|
|
// exited on its own.
|
2017-03-31 03:01:41 +00:00
|
|
|
func (s StateStatus) Err() error {
|
|
|
|
return s.err
|
2016-08-30 16:44:06 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
// NewState creates a default state object with a fresh channel for state changes.
|
2014-06-06 11:28:12 +00:00
|
|
|
func NewState() *State {
|
|
|
|
return &State{
|
2017-03-30 20:52:40 +00:00
|
|
|
waitStop: make(chan struct{}),
|
|
|
|
waitRemove: make(chan struct{}),
|
2014-06-06 11:28:12 +00:00
|
|
|
}
|
2013-01-19 00:13:39 +00:00
|
|
|
}
|
|
|
|
|
2013-01-27 23:42:42 +00:00
|
|
|
// String returns a human-readable description of the state
|
|
|
|
func (s *State) String() string {
|
|
|
|
if s.Running {
|
2014-05-21 21:06:18 +00:00
|
|
|
if s.Paused {
|
|
|
|
return fmt.Sprintf("Up %s (Paused)", units.HumanDuration(time.Now().UTC().Sub(s.StartedAt)))
|
|
|
|
}
|
2014-08-11 18:35:18 +00:00
|
|
|
if s.Restarting {
|
2016-08-30 16:44:06 +00:00
|
|
|
return fmt.Sprintf("Restarting (%d) %s ago", s.ExitCodeValue, units.HumanDuration(time.Now().UTC().Sub(s.FinishedAt)))
|
2014-08-11 18:35:18 +00:00
|
|
|
}
|
2014-08-11 18:07:37 +00:00
|
|
|
|
2016-04-18 09:48:13 +00:00
|
|
|
if h := s.Health; h != nil {
|
|
|
|
return fmt.Sprintf("Up %s (%s)", units.HumanDuration(time.Now().UTC().Sub(s.StartedAt)), h.String())
|
|
|
|
}
|
2016-07-15 18:21:19 +00:00
|
|
|
|
2014-05-12 23:40:19 +00:00
|
|
|
return fmt.Sprintf("Up %s", units.HumanDuration(time.Now().UTC().Sub(s.StartedAt)))
|
2013-01-27 23:42:42 +00:00
|
|
|
}
|
2014-08-11 18:07:37 +00:00
|
|
|
|
2015-11-12 19:55:17 +00:00
|
|
|
if s.RemovalInProgress {
|
2015-03-12 19:26:17 +00:00
|
|
|
return "Removal In Progress"
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.Dead {
|
|
|
|
return "Dead"
|
|
|
|
}
|
|
|
|
|
2015-05-20 21:51:58 +00:00
|
|
|
if s.StartedAt.IsZero() {
|
|
|
|
return "Created"
|
|
|
|
}
|
|
|
|
|
2014-03-19 03:05:54 +00:00
|
|
|
if s.FinishedAt.IsZero() {
|
|
|
|
return ""
|
|
|
|
}
|
2014-08-11 18:07:37 +00:00
|
|
|
|
2016-08-30 16:44:06 +00:00
|
|
|
return fmt.Sprintf("Exited (%d) %s ago", s.ExitCodeValue, units.HumanDuration(time.Now().UTC().Sub(s.FinishedAt)))
|
2013-01-27 23:42:42 +00:00
|
|
|
}
|
|
|
|
|
2016-07-15 18:21:19 +00:00
|
|
|
// IsValidHealthString checks if the provided string is a valid container health status or not.
|
|
|
|
func IsValidHealthString(s string) bool {
|
|
|
|
return s == types.Starting ||
|
|
|
|
s == types.Healthy ||
|
|
|
|
s == types.Unhealthy ||
|
|
|
|
s == types.NoHealthcheck
|
|
|
|
}
|
|
|
|
|
2014-09-26 23:25:50 +00:00
|
|
|
// StateString returns a single string to describe state
|
|
|
|
func (s *State) StateString() string {
|
|
|
|
if s.Running {
|
|
|
|
if s.Paused {
|
|
|
|
return "paused"
|
|
|
|
}
|
|
|
|
if s.Restarting {
|
|
|
|
return "restarting"
|
|
|
|
}
|
|
|
|
return "running"
|
|
|
|
}
|
2015-03-12 19:26:17 +00:00
|
|
|
|
2016-08-15 11:33:56 +00:00
|
|
|
if s.RemovalInProgress {
|
|
|
|
return "removing"
|
|
|
|
}
|
|
|
|
|
2015-03-12 19:26:17 +00:00
|
|
|
if s.Dead {
|
|
|
|
return "dead"
|
|
|
|
}
|
|
|
|
|
2015-05-20 21:51:58 +00:00
|
|
|
if s.StartedAt.IsZero() {
|
|
|
|
return "created"
|
|
|
|
}
|
|
|
|
|
2014-09-26 23:25:50 +00:00
|
|
|
return "exited"
|
|
|
|
}
|
|
|
|
|
2015-11-12 19:55:17 +00:00
|
|
|
// IsValidStateString checks if the provided string is a valid container state or not.
|
|
|
|
func IsValidStateString(s string) bool {
|
2015-06-30 21:40:27 +00:00
|
|
|
if s != "paused" &&
|
|
|
|
s != "restarting" &&
|
2016-08-15 11:33:56 +00:00
|
|
|
s != "removing" &&
|
2015-06-30 21:40:27 +00:00
|
|
|
s != "running" &&
|
|
|
|
s != "dead" &&
|
|
|
|
s != "created" &&
|
|
|
|
s != "exited" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-03-31 03:01:41 +00:00
|
|
|
// WaitCondition is an enum type for different states to wait for.
|
|
|
|
type WaitCondition int
|
|
|
|
|
|
|
|
// Possible WaitCondition Values.
|
|
|
|
//
|
|
|
|
// WaitConditionNotRunning (default) is used to wait for any of the non-running
|
|
|
|
// states: "created", "exited", "dead", "removing", or "removed".
|
|
|
|
//
|
|
|
|
// WaitConditionNextExit is used to wait for the next time the state changes
|
|
|
|
// to a non-running state. If the state is currently "created" or "exited",
|
|
|
|
// this would cause Wait() to block until either the container runs and exits
|
|
|
|
// or is removed.
|
|
|
|
//
|
|
|
|
// WaitConditionRemoved is used to wait for the container to be removed.
|
|
|
|
const (
|
|
|
|
WaitConditionNotRunning WaitCondition = iota
|
|
|
|
WaitConditionNextExit
|
|
|
|
WaitConditionRemoved
|
|
|
|
)
|
2014-06-06 11:28:12 +00:00
|
|
|
|
2017-06-01 11:21:01 +00:00
|
|
|
// Wait waits until the container is in a certain state indicated by the given
|
2017-03-31 03:01:41 +00:00
|
|
|
// condition. A context must be used for cancelling the request, controlling
|
|
|
|
// timeouts, and avoiding goroutine leaks. Wait must be called without holding
|
|
|
|
// the state lock. Returns a channel from which the caller will receive the
|
|
|
|
// result. If the container exited on its own, the result's Err() method will
|
2017-05-21 23:24:07 +00:00
|
|
|
// be nil and its ExitCode() method will return the container's exit code,
|
2017-03-31 03:01:41 +00:00
|
|
|
// otherwise, the results Err() method will return an error indicating why the
|
|
|
|
// wait operation failed.
|
|
|
|
func (s *State) Wait(ctx context.Context, condition WaitCondition) <-chan StateStatus {
|
2017-03-30 20:52:40 +00:00
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
|
2017-03-31 03:01:41 +00:00
|
|
|
if condition == WaitConditionNotRunning && !s.Running {
|
|
|
|
// Buffer so we can put it in the channel now.
|
|
|
|
resultC := make(chan StateStatus, 1)
|
2017-03-30 20:52:40 +00:00
|
|
|
|
2017-03-31 03:01:41 +00:00
|
|
|
// Send the current status.
|
|
|
|
resultC <- StateStatus{
|
|
|
|
exitCode: s.ExitCode(),
|
|
|
|
err: s.Err(),
|
|
|
|
}
|
2017-03-30 20:52:40 +00:00
|
|
|
|
|
|
|
return resultC
|
2016-11-17 03:08:37 +00:00
|
|
|
}
|
2017-03-30 20:52:40 +00:00
|
|
|
|
2017-03-31 03:01:41 +00:00
|
|
|
// If we are waiting only for removal, the waitStop channel should
|
|
|
|
// remain nil and block forever.
|
2017-03-30 20:52:40 +00:00
|
|
|
var waitStop chan struct{}
|
2017-03-31 03:01:41 +00:00
|
|
|
if condition < WaitConditionRemoved {
|
2017-03-30 20:52:40 +00:00
|
|
|
waitStop = s.waitStop
|
2014-06-06 11:28:12 +00:00
|
|
|
}
|
|
|
|
|
2017-03-30 20:52:40 +00:00
|
|
|
// Always wait for removal, just in case the container gets removed
|
|
|
|
// while it is still in a "created" state, in which case it is never
|
|
|
|
// actually stopped.
|
|
|
|
waitRemove := s.waitRemove
|
|
|
|
|
2017-03-31 03:01:41 +00:00
|
|
|
resultC := make(chan StateStatus)
|
2017-03-30 20:52:40 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
// Context timeout or cancellation.
|
2017-03-31 03:01:41 +00:00
|
|
|
resultC <- StateStatus{
|
|
|
|
exitCode: -1,
|
|
|
|
err: ctx.Err(),
|
|
|
|
}
|
2017-03-30 20:52:40 +00:00
|
|
|
return
|
|
|
|
case <-waitStop:
|
|
|
|
case <-waitRemove:
|
2016-06-14 02:52:49 +00:00
|
|
|
}
|
2017-03-30 20:52:40 +00:00
|
|
|
|
2016-06-14 18:11:43 +00:00
|
|
|
s.Lock()
|
2017-03-31 03:01:41 +00:00
|
|
|
result := StateStatus{
|
|
|
|
exitCode: s.ExitCode(),
|
|
|
|
err: s.Err(),
|
|
|
|
}
|
2016-06-14 02:52:49 +00:00
|
|
|
s.Unlock()
|
2017-03-30 20:52:40 +00:00
|
|
|
|
|
|
|
resultC <- result
|
|
|
|
}()
|
|
|
|
|
|
|
|
return resultC
|
2016-06-14 02:52:49 +00:00
|
|
|
}
|
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
// IsRunning returns whether the running flag is set. Used by Container to check whether a container is running.
|
2013-11-21 20:21:03 +00:00
|
|
|
func (s *State) IsRunning() bool {
|
2014-08-28 11:39:27 +00:00
|
|
|
s.Lock()
|
2014-06-06 11:28:12 +00:00
|
|
|
res := s.Running
|
2014-08-28 11:39:27 +00:00
|
|
|
s.Unlock()
|
2014-06-06 11:28:12 +00:00
|
|
|
return res
|
|
|
|
}
|
2013-11-21 20:21:03 +00:00
|
|
|
|
2015-07-30 21:01:53 +00:00
|
|
|
// GetPID holds the process id of a container.
|
2015-11-02 23:25:26 +00:00
|
|
|
func (s *State) GetPID() int {
|
2014-08-28 11:39:27 +00:00
|
|
|
s.Lock()
|
2014-06-06 11:28:12 +00:00
|
|
|
res := s.Pid
|
2014-08-28 11:39:27 +00:00
|
|
|
s.Unlock()
|
2014-06-06 11:28:12 +00:00
|
|
|
return res
|
2013-11-21 20:21:03 +00:00
|
|
|
}
|
|
|
|
|
2016-06-14 18:11:43 +00:00
|
|
|
// ExitCode returns current exitcode for the state. Take lock before if state
|
|
|
|
// may be shared.
|
|
|
|
func (s *State) ExitCode() int {
|
2016-08-30 16:44:06 +00:00
|
|
|
return s.ExitCodeValue
|
2013-11-21 20:21:03 +00:00
|
|
|
}
|
|
|
|
|
2016-07-03 12:47:39 +00:00
|
|
|
// SetExitCode sets current exitcode for the state. Take lock before if state
|
2016-06-14 18:11:43 +00:00
|
|
|
// may be shared.
|
|
|
|
func (s *State) SetExitCode(ec int) {
|
2016-08-30 16:44:06 +00:00
|
|
|
s.ExitCodeValue = ec
|
2016-06-14 18:11:43 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 19:55:17 +00:00
|
|
|
// SetRunning sets the state of the container to "running".
|
2016-03-18 18:50:19 +00:00
|
|
|
func (s *State) SetRunning(pid int, initial bool) {
|
2016-08-30 16:44:06 +00:00
|
|
|
s.ErrorMsg = ""
|
2017-09-22 13:52:41 +00:00
|
|
|
s.Paused = false
|
2014-07-29 07:14:10 +00:00
|
|
|
s.Running = true
|
2014-08-13 21:56:35 +00:00
|
|
|
s.Restarting = false
|
2017-07-12 08:24:28 +00:00
|
|
|
if initial {
|
|
|
|
s.Paused = false
|
|
|
|
}
|
2016-08-30 16:44:06 +00:00
|
|
|
s.ExitCodeValue = 0
|
2014-07-29 07:14:10 +00:00
|
|
|
s.Pid = pid
|
2016-03-18 18:50:19 +00:00
|
|
|
if initial {
|
|
|
|
s.StartedAt = time.Now().UTC()
|
|
|
|
}
|
2013-01-19 00:13:39 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 19:55:17 +00:00
|
|
|
// SetStopped sets the container state to "stopped" without locking.
|
2016-03-18 18:50:19 +00:00
|
|
|
func (s *State) SetStopped(exitStatus *ExitStatus) {
|
2014-07-29 07:14:10 +00:00
|
|
|
s.Running = false
|
2016-03-18 18:50:19 +00:00
|
|
|
s.Paused = false
|
2014-08-13 21:56:35 +00:00
|
|
|
s.Restarting = false
|
2014-07-29 07:14:10 +00:00
|
|
|
s.Pid = 0
|
2017-09-22 13:52:41 +00:00
|
|
|
if exitStatus.ExitedAt.IsZero() {
|
|
|
|
s.FinishedAt = time.Now().UTC()
|
|
|
|
} else {
|
|
|
|
s.FinishedAt = exitStatus.ExitedAt
|
|
|
|
}
|
|
|
|
s.ExitCodeValue = exitStatus.ExitCode
|
|
|
|
s.OOMKilled = exitStatus.OOMKilled
|
|
|
|
close(s.waitStop) // fire waiters for stop
|
2017-03-30 20:52:40 +00:00
|
|
|
s.waitStop = make(chan struct{})
|
2013-01-19 00:13:39 +00:00
|
|
|
}
|
2014-05-21 21:06:18 +00:00
|
|
|
|
2016-09-20 11:09:18 +00:00
|
|
|
// SetRestarting sets the container state to "restarting" without locking.
|
2015-11-12 19:55:17 +00:00
|
|
|
// It also sets the container PID to 0.
|
2016-03-18 18:50:19 +00:00
|
|
|
func (s *State) SetRestarting(exitStatus *ExitStatus) {
|
2014-08-13 21:56:35 +00:00
|
|
|
// we should consider the container running when it is restarting because of
|
|
|
|
// all the checks in docker around rm/stop/etc
|
|
|
|
s.Running = true
|
|
|
|
s.Restarting = true
|
2017-06-27 20:14:58 +00:00
|
|
|
s.Paused = false
|
2014-08-13 21:56:35 +00:00
|
|
|
s.Pid = 0
|
|
|
|
s.FinishedAt = time.Now().UTC()
|
2017-09-22 13:52:41 +00:00
|
|
|
s.ExitCodeValue = exitStatus.ExitCode
|
|
|
|
s.OOMKilled = exitStatus.OOMKilled
|
|
|
|
close(s.waitStop) // fire waiters for stop
|
2017-03-30 20:52:40 +00:00
|
|
|
s.waitStop = make(chan struct{})
|
2014-08-11 18:07:37 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 19:55:17 +00:00
|
|
|
// SetError sets the container's error state. This is useful when we want to
|
2014-09-30 08:30:58 +00:00
|
|
|
// know the error that occurred when container transits to another state
|
|
|
|
// when inspecting it
|
2015-11-12 19:55:17 +00:00
|
|
|
func (s *State) SetError(err error) {
|
ContainerWait on remove: don't stuck on rm fail
Currently, if a container removal has failed for some reason,
any client waiting for removal (e.g. `docker run --rm`) is
stuck, waiting for removal to succeed while it has failed already.
For more details and the reproducer, please check
https://github.com/moby/moby/issues/34945
This commit addresses that by allowing `ContainerWait()` with
`container.WaitCondition == "removed"` argument to return an
error in case of removal failure. The `ContainerWaitOKBody`
stucture returned to a client is amended with a pointer to `struct Error`,
containing an error message string, and the `Client.ContainerWait()`
is modified to return the error, if any, to the client.
Note that this feature is only available for API version >= 1.34.
In order for the old clients to be unstuck, we just close the connection
without writing anything -- this causes client's error.
Now, docker-cli would need a separate commit to bump the API to 1.34
and to show an error returned, if any.
[v2: recreate the waitRemove channel after closing]
[v3: document; keep legacy behavior for older clients]
[v4: convert Error from string to pointer to a struct]
[v5: don't emulate old behavior, send empty response in error case]
[v6: rename legacy* vars to include version suffix]
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2017-09-27 18:49:22 +00:00
|
|
|
s.ErrorMsg = ""
|
|
|
|
if err != nil {
|
|
|
|
s.ErrorMsg = err.Error()
|
|
|
|
}
|
2014-09-30 08:30:58 +00:00
|
|
|
}
|
|
|
|
|
2015-11-12 19:55:17 +00:00
|
|
|
// IsPaused returns whether the container is paused or not.
|
|
|
|
func (s *State) IsPaused() bool {
|
2014-08-28 11:39:27 +00:00
|
|
|
s.Lock()
|
2014-06-06 11:28:12 +00:00
|
|
|
res := s.Paused
|
2014-08-28 11:39:27 +00:00
|
|
|
s.Unlock()
|
2014-06-06 11:28:12 +00:00
|
|
|
return res
|
2014-05-21 21:06:18 +00:00
|
|
|
}
|
2015-03-12 19:26:17 +00:00
|
|
|
|
2016-01-25 15:38:03 +00:00
|
|
|
// IsRestarting returns whether the container is restarting or not.
|
|
|
|
func (s *State) IsRestarting() bool {
|
|
|
|
s.Lock()
|
|
|
|
res := s.Restarting
|
|
|
|
s.Unlock()
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2015-11-12 19:55:17 +00:00
|
|
|
// SetRemovalInProgress sets the container state as being removed.
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 15:53:35 +00:00
|
|
|
// It returns true if the container was already in that state.
|
|
|
|
func (s *State) SetRemovalInProgress() bool {
|
2015-03-12 19:26:17 +00:00
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
2015-11-12 19:55:17 +00:00
|
|
|
if s.RemovalInProgress {
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 15:53:35 +00:00
|
|
|
return true
|
2015-03-12 19:26:17 +00:00
|
|
|
}
|
2015-11-12 19:55:17 +00:00
|
|
|
s.RemovalInProgress = true
|
Remove static errors from errors package.
Moving all strings to the errors package wasn't a good idea after all.
Our custom implementation of Go errors predates everything that's nice
and good about working with errors in Go. Take as an example what we
have to do to get an error message:
```go
func GetErrorMessage(err error) string {
switch err.(type) {
case errcode.Error:
e, _ := err.(errcode.Error)
return e.Message
case errcode.ErrorCode:
ec, _ := err.(errcode.ErrorCode)
return ec.Message()
default:
return err.Error()
}
}
```
This goes against every good practice for Go development. The language already provides a simple, intuitive and standard way to get error messages, that is calling the `Error()` method from an error. Reinventing the error interface is a mistake.
Our custom implementation also makes very hard to reason about errors, another nice thing about Go. I found several (>10) error declarations that we don't use anywhere. This is a clear sign about how little we know about the errors we return. I also found several error usages where the number of arguments was different than the parameters declared in the error, another clear example of how difficult is to reason about errors.
Moreover, our custom implementation didn't really make easier for people to return custom HTTP status code depending on the errors. Again, it's hard to reason about when to set custom codes and how. Take an example what we have to do to extract the message and status code from an error before returning a response from the API:
```go
switch err.(type) {
case errcode.ErrorCode:
daError, _ := err.(errcode.ErrorCode)
statusCode = daError.Descriptor().HTTPStatusCode
errMsg = daError.Message()
case errcode.Error:
// For reference, if you're looking for a particular error
// then you can do something like :
// import ( derr "github.com/docker/docker/errors" )
// if daError.ErrorCode() == derr.ErrorCodeNoSuchContainer { ... }
daError, _ := err.(errcode.Error)
statusCode = daError.ErrorCode().Descriptor().HTTPStatusCode
errMsg = daError.Message
default:
// This part of will be removed once we've
// converted everything over to use the errcode package
// FIXME: this is brittle and should not be necessary.
// If we need to differentiate between different possible error types,
// we should create appropriate error types with clearly defined meaning
errStr := strings.ToLower(err.Error())
for keyword, status := range map[string]int{
"not found": http.StatusNotFound,
"no such": http.StatusNotFound,
"bad parameter": http.StatusBadRequest,
"conflict": http.StatusConflict,
"impossible": http.StatusNotAcceptable,
"wrong login/password": http.StatusUnauthorized,
"hasn't been activated": http.StatusForbidden,
} {
if strings.Contains(errStr, keyword) {
statusCode = status
break
}
}
}
```
You can notice two things in that code:
1. We have to explain how errors work, because our implementation goes against how easy to use Go errors are.
2. At no moment we arrived to remove that `switch` statement that was the original reason to use our custom implementation.
This change removes all our status errors from the errors package and puts them back in their specific contexts.
IT puts the messages back with their contexts. That way, we know right away when errors used and how to generate their messages.
It uses custom interfaces to reason about errors. Errors that need to response with a custom status code MUST implementent this simple interface:
```go
type errorWithStatus interface {
HTTPErrorStatusCode() int
}
```
This interface is very straightforward to implement. It also preserves Go errors real behavior, getting the message is as simple as using the `Error()` method.
I included helper functions to generate errors that use custom status code in `errors/errors.go`.
By doing this, we remove the hard dependency we have eeverywhere to our custom errors package. Yes, you can use it as a helper to generate error, but it's still very easy to generate errors without it.
Please, read this fantastic blog post about errors in Go: http://dave.cheney.net/2014/12/24/inspecting-errors
Signed-off-by: David Calavera <david.calavera@gmail.com>
2016-02-25 15:53:35 +00:00
|
|
|
return false
|
2015-03-12 19:26:17 +00:00
|
|
|
}
|
|
|
|
|
2016-07-03 12:47:39 +00:00
|
|
|
// ResetRemovalInProgress makes the RemovalInProgress state to false.
|
2015-11-12 19:55:17 +00:00
|
|
|
func (s *State) ResetRemovalInProgress() {
|
2015-03-12 19:26:17 +00:00
|
|
|
s.Lock()
|
2015-11-12 19:55:17 +00:00
|
|
|
s.RemovalInProgress = false
|
2015-03-12 19:26:17 +00:00
|
|
|
s.Unlock()
|
|
|
|
}
|
|
|
|
|
2017-05-20 02:38:45 +00:00
|
|
|
// IsRemovalInProgress returns whether the RemovalInProgress flag is set.
|
|
|
|
// Used by Container to check whether a container is being removed.
|
|
|
|
func (s *State) IsRemovalInProgress() bool {
|
|
|
|
s.Lock()
|
|
|
|
res := s.RemovalInProgress
|
|
|
|
s.Unlock()
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2015-11-12 19:55:17 +00:00
|
|
|
// SetDead sets the container state to "dead"
|
|
|
|
func (s *State) SetDead() {
|
2015-03-12 19:26:17 +00:00
|
|
|
s.Lock()
|
|
|
|
s.Dead = true
|
|
|
|
s.Unlock()
|
|
|
|
}
|
2016-06-14 18:11:43 +00:00
|
|
|
|
2017-05-20 02:38:45 +00:00
|
|
|
// IsDead returns whether the Dead flag is set. Used by Container to check whether a container is dead.
|
|
|
|
func (s *State) IsDead() bool {
|
|
|
|
s.Lock()
|
|
|
|
res := s.Dead
|
|
|
|
s.Unlock()
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2017-03-30 20:52:40 +00:00
|
|
|
// SetRemoved assumes this container is already in the "dead" state and
|
|
|
|
// closes the internal waitRemove channel to unblock callers waiting for a
|
|
|
|
// container to be removed.
|
|
|
|
func (s *State) SetRemoved() {
|
ContainerWait on remove: don't stuck on rm fail
Currently, if a container removal has failed for some reason,
any client waiting for removal (e.g. `docker run --rm`) is
stuck, waiting for removal to succeed while it has failed already.
For more details and the reproducer, please check
https://github.com/moby/moby/issues/34945
This commit addresses that by allowing `ContainerWait()` with
`container.WaitCondition == "removed"` argument to return an
error in case of removal failure. The `ContainerWaitOKBody`
stucture returned to a client is amended with a pointer to `struct Error`,
containing an error message string, and the `Client.ContainerWait()`
is modified to return the error, if any, to the client.
Note that this feature is only available for API version >= 1.34.
In order for the old clients to be unstuck, we just close the connection
without writing anything -- this causes client's error.
Now, docker-cli would need a separate commit to bump the API to 1.34
and to show an error returned, if any.
[v2: recreate the waitRemove channel after closing]
[v3: document; keep legacy behavior for older clients]
[v4: convert Error from string to pointer to a struct]
[v5: don't emulate old behavior, send empty response in error case]
[v6: rename legacy* vars to include version suffix]
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2017-09-27 18:49:22 +00:00
|
|
|
s.SetRemovalError(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetRemovalError is to be called in case a container remove failed.
|
|
|
|
// It sets an error and closes the internal waitRemove channel to unblock
|
|
|
|
// callers waiting for the container to be removed.
|
|
|
|
func (s *State) SetRemovalError(err error) {
|
|
|
|
s.SetError(err)
|
2017-03-30 20:52:40 +00:00
|
|
|
s.Lock()
|
|
|
|
close(s.waitRemove) // Unblock those waiting on remove.
|
ContainerWait on remove: don't stuck on rm fail
Currently, if a container removal has failed for some reason,
any client waiting for removal (e.g. `docker run --rm`) is
stuck, waiting for removal to succeed while it has failed already.
For more details and the reproducer, please check
https://github.com/moby/moby/issues/34945
This commit addresses that by allowing `ContainerWait()` with
`container.WaitCondition == "removed"` argument to return an
error in case of removal failure. The `ContainerWaitOKBody`
stucture returned to a client is amended with a pointer to `struct Error`,
containing an error message string, and the `Client.ContainerWait()`
is modified to return the error, if any, to the client.
Note that this feature is only available for API version >= 1.34.
In order for the old clients to be unstuck, we just close the connection
without writing anything -- this causes client's error.
Now, docker-cli would need a separate commit to bump the API to 1.34
and to show an error returned, if any.
[v2: recreate the waitRemove channel after closing]
[v3: document; keep legacy behavior for older clients]
[v4: convert Error from string to pointer to a struct]
[v5: don't emulate old behavior, send empty response in error case]
[v6: rename legacy* vars to include version suffix]
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2017-09-27 18:49:22 +00:00
|
|
|
// Recreate the channel so next ContainerWait will work
|
|
|
|
s.waitRemove = make(chan struct{})
|
2017-03-30 20:52:40 +00:00
|
|
|
s.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Err returns an error if there is one.
|
|
|
|
func (s *State) Err() error {
|
|
|
|
if s.ErrorMsg != "" {
|
|
|
|
return errors.New(s.ErrorMsg)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|