Skip to content

Commit

Permalink
Workaround for inconsistency container status
Browse files Browse the repository at this point in the history
Signed-off-by: Shawn Wang <[email protected]>
  • Loading branch information
shawn111 committed Mar 29, 2019
1 parent b2f74b2 commit aa2a3dd
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
13 changes: 12 additions & 1 deletion cmd/dockerd/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,21 @@ func (cli *DaemonCli) start(opts daemonOptions) (err error) {
cli.TrustKeyPath = opts.common.TrustKey

registryService := registry.NewService(cli.Config.ServiceOptions)
containerdRemote, err := libcontainerd.New(cli.getLibcontainerdRoot(), cli.getPlatformRemoteOptions()...)

// libcontainerd.New add hook
hook := func() {
if cli.d == nil {
return
}
for _, c := range cli.d.List() {
c.CheckProcessIsRunning()
}
}
containerdRemote, err := libcontainerd.New(cli.getLibcontainerdRoot(), hook, cli.getPlatformRemoteOptions()...)
if err != nil {
return err
}

signal.Trap(func() {
cli.stop()
<-stopc // wait for daemonCli.start() to return
Expand Down
12 changes: 12 additions & 0 deletions container/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"golang.org/x/net/context"

"github.com/docker/docker/api/types"
"github.com/docker/docker/utils"
"github.com/docker/go-units"
)

Expand Down Expand Up @@ -230,6 +231,17 @@ func (s *State) IsRunning() bool {
return res
}

// CheckProcessIsRunning check the the process of running container is still performed.
func (s *State) CheckProcessIsRunning() {
if !s.IsRunning() {
return
}
if !utils.IsProcessAlive(s.Pid) {
// TODO: force set exitcode:1, better to define new exitcode
s.SetStopped(&ExitStatus{ExitCode: 1})
}
}

// GetPID holds the process id of a container.
func (s *State) GetPID() int {
s.Lock()
Expand Down
9 changes: 7 additions & 2 deletions libcontainerd/remote_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,11 @@ type remote struct {
oomScore int
maxHealthCheckRetries int
restoreFromTimestamp *timestamp.Timestamp
handleContainerdHook func()
}

// New creates a fresh instance of libcontainerd remote.
func New(stateDir string, options ...RemoteOption) (_ Remote, err error) {
func New(stateDir string, hook func(), options ...RemoteOption) (_ Remote, err error) {
defer func() {
if err != nil {
err = fmt.Errorf("Failed to connect to containerd. Please make sure containerd is installed in your PATH or you have specified the correct address. Got error: %v", err)
Expand All @@ -75,6 +76,7 @@ func New(stateDir string, options ...RemoteOption) (_ Remote, err error) {
daemonPid: -1,
eventTsPath: filepath.Join(stateDir, eventTimestampFilename),
}
r.handleContainerdHook = hook
for _, option := range options {
if err := option.Apply(r); err != nil {
return nil, err
Expand Down Expand Up @@ -117,7 +119,6 @@ func New(stateDir string, options ...RemoteOption) (_ Remote, err error) {
logrus.Errorf("libcontainerd: failed to convert timestamp: %q", err)
}
r.restoreFromTimestamp = tsp

go r.handleConnectionChange()

if err := r.startEventsMonitor(); err != nil {
Expand Down Expand Up @@ -179,6 +180,10 @@ func (r *remote) handleConnectionChange() {
if err := r.runContainerdDaemon(); err != nil { //FIXME: Handle error
logrus.Errorf("libcontainerd: error restarting containerd: %v", err)
}
// Workaround to fix inconsistency stopped status
go func() {
r.handleContainerdHook()
}()
continue
}
}
Expand Down
2 changes: 1 addition & 1 deletion libcontainerd/remote_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (r *remote) UpdateOptions(opts ...RemoteOption) error {

// New creates a fresh instance of libcontainerd remote. On Windows,
// this is not used as there is no remote containerd process.
func New(_ string, _ ...RemoteOption) (Remote, error) {
func New(_ string, _ func(), _ ...RemoteOption) (Remote, error) {
return &remote{}, nil
}

Expand Down

0 comments on commit aa2a3dd

Please sign in to comment.