Skip to content

Commit

Permalink
Issue a stop when start container failed with EOF error.
Browse files Browse the repository at this point in the history
When start container failed with EOF there's a chance that the container is started anyway, so issue a stop for this case.
  • Loading branch information
fenxiong committed Oct 23, 2019
1 parent 6125b11 commit f8a68b7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
4 changes: 3 additions & 1 deletion agent/dockerclient/dockerapi/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const (
DockerTimeoutErrorName = "DockerTimeoutError"
// CannotInspectContainerErrorName is the name of container inspect error.
CannotInspectContainerErrorName = "CannotInspectContainerError"
// CannotStartContainerErrorName is the name of container start error.
CannotStartContainerErrorName = "CannotStartContainerError"
// CannotDescribeContainerErrorName is the name of describe container error.
CannotDescribeContainerErrorName = "CannotDescribeContainerError"
)
Expand Down Expand Up @@ -201,7 +203,7 @@ func (err CannotStartContainerError) Error() string {

// ErrorName returns name of the CannotStartContainerError
func (err CannotStartContainerError) ErrorName() string {
return "CannotStartContainerError"
return CannotStartContainerErrorName
}

// CannotInspectContainerError indicates any error when trying to inspect a container
Expand Down
14 changes: 13 additions & 1 deletion agent/engine/task_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ package engine

import (
"context"
"io"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -666,10 +668,20 @@ func (mtask *managedTask) handleEventError(containerChange dockerContainerChange
container.SetKnownStatus(currentKnownStatus)
container.SetDesiredStatus(apicontainerstatus.ContainerStopped)
errorName := event.Error.ErrorName()
errorStr := event.Error.Error()
shouldForceStop := false
if errorName == dockerapi.DockerTimeoutErrorName || errorName == dockerapi.CannotInspectContainerErrorName {
// If there's an error with inspecting the container or in case of timeout error,
// we'll also assume that the container has transitioned to RUNNING and issue
// we'll assume that the container has transitioned to RUNNING and issue
// a stop. See #1043 for details
shouldForceStop = true
} else if errorName == dockerapi.CannotStartContainerErrorName && strings.HasSuffix(errorStr, io.EOF.Error()) {
// If we get an EOF error from Docker when starting the container, we don't really know whether the
// container is started anyway. So issuing a stop here as well. See #1708.
shouldForceStop = true
}

if shouldForceStop {
seelog.Warnf("Managed task [%s]: forcing container [%s] to stop",
mtask.Arn, container.Name)
go mtask.engine.transitionContainer(mtask.Task, container, apicontainerstatus.ContainerStopped)
Expand Down
12 changes: 12 additions & 0 deletions agent/engine/task_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,18 @@ func TestHandleEventError(t *testing.T) {
ExpectedContainerDesiredStatusStopped: true,
ExpectedOK: false,
},
{
Name: "Start EOF",
EventStatus: apicontainerstatus.ContainerRunning,
CurrentContainerKnownStatus: apicontainerstatus.ContainerCreated,
Error: &dockerapi.CannotStartContainerError{
FromError: errors.New("error during connect: Post http://%2Fvar%2Frun%2Fdocker.sock/v1.19/containers/containerid/start: EOF"),
},
ExpectedContainerKnownStatusSet: true,
ExpectedContainerKnownStatus: apicontainerstatus.ContainerCreated,
ExpectedContainerDesiredStatusStopped: true,
ExpectedOK: false,
},
{
Name: "Inspect failed during create",
EventStatus: apicontainerstatus.ContainerCreated,
Expand Down

0 comments on commit f8a68b7

Please sign in to comment.