Skip to content

Commit aa2a3dd

Browse files
committed
Workaround for inconsistency container status
Signed-off-by: Shawn Wang <[email protected]>
1 parent b2f74b2 commit aa2a3dd

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

cmd/dockerd/daemon.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,10 +274,21 @@ func (cli *DaemonCli) start(opts daemonOptions) (err error) {
274274
cli.TrustKeyPath = opts.common.TrustKey
275275

276276
registryService := registry.NewService(cli.Config.ServiceOptions)
277-
containerdRemote, err := libcontainerd.New(cli.getLibcontainerdRoot(), cli.getPlatformRemoteOptions()...)
277+
278+
// libcontainerd.New add hook
279+
hook := func() {
280+
if cli.d == nil {
281+
return
282+
}
283+
for _, c := range cli.d.List() {
284+
c.CheckProcessIsRunning()
285+
}
286+
}
287+
containerdRemote, err := libcontainerd.New(cli.getLibcontainerdRoot(), hook, cli.getPlatformRemoteOptions()...)
278288
if err != nil {
279289
return err
280290
}
291+
281292
signal.Trap(func() {
282293
cli.stop()
283294
<-stopc // wait for daemonCli.start() to return

container/state.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"golang.org/x/net/context"
99

1010
"github.com/docker/docker/api/types"
11+
"github.com/docker/docker/utils"
1112
"github.com/docker/go-units"
1213
)
1314

@@ -230,6 +231,17 @@ func (s *State) IsRunning() bool {
230231
return res
231232
}
232233

234+
// CheckProcessIsRunning check the the process of running container is still performed.
235+
func (s *State) CheckProcessIsRunning() {
236+
if !s.IsRunning() {
237+
return
238+
}
239+
if !utils.IsProcessAlive(s.Pid) {
240+
// TODO: force set exitcode:1, better to define new exitcode
241+
s.SetStopped(&ExitStatus{ExitCode: 1})
242+
}
243+
}
244+
233245
// GetPID holds the process id of a container.
234246
func (s *State) GetPID() int {
235247
s.Lock()

libcontainerd/remote_unix.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,11 @@ type remote struct {
6161
oomScore int
6262
maxHealthCheckRetries int
6363
restoreFromTimestamp *timestamp.Timestamp
64+
handleContainerdHook func()
6465
}
6566

6667
// New creates a fresh instance of libcontainerd remote.
67-
func New(stateDir string, options ...RemoteOption) (_ Remote, err error) {
68+
func New(stateDir string, hook func(), options ...RemoteOption) (_ Remote, err error) {
6869
defer func() {
6970
if err != nil {
7071
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)
@@ -75,6 +76,7 @@ func New(stateDir string, options ...RemoteOption) (_ Remote, err error) {
7576
daemonPid: -1,
7677
eventTsPath: filepath.Join(stateDir, eventTimestampFilename),
7778
}
79+
r.handleContainerdHook = hook
7880
for _, option := range options {
7981
if err := option.Apply(r); err != nil {
8082
return nil, err
@@ -117,7 +119,6 @@ func New(stateDir string, options ...RemoteOption) (_ Remote, err error) {
117119
logrus.Errorf("libcontainerd: failed to convert timestamp: %q", err)
118120
}
119121
r.restoreFromTimestamp = tsp
120-
121122
go r.handleConnectionChange()
122123

123124
if err := r.startEventsMonitor(); err != nil {
@@ -179,6 +180,10 @@ func (r *remote) handleConnectionChange() {
179180
if err := r.runContainerdDaemon(); err != nil { //FIXME: Handle error
180181
logrus.Errorf("libcontainerd: error restarting containerd: %v", err)
181182
}
183+
// Workaround to fix inconsistency stopped status
184+
go func() {
185+
r.handleContainerdHook()
186+
}()
182187
continue
183188
}
184189
}

libcontainerd/remote_windows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func (r *remote) UpdateOptions(opts ...RemoteOption) error {
2626

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

0 commit comments

Comments
 (0)