Skip to content

Commit ad8f7b7

Browse files
author
Wei Fu
authored
Merge pull request #1407 from HusterWan/master
bugfix: containerd instance exit may cause container exit
2 parents 48eb589 + 33611b3 commit ad8f7b7

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

ctrd/client.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,10 @@ func (c *Client) Cleanup() error {
302302
return err
303303
}
304304

305+
// Note(ziren): notify containerd is dead before containerd
306+
// is really dead
307+
c.watch.setContainerdDead(true)
308+
305309
// Ask the daemon to quit
306310
syscall.Kill(c.daemonPid, syscall.SIGTERM)
307311

ctrd/watch.go

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,26 @@ func (m *Message) ExitTime() time.Time {
3434

3535
type watch struct {
3636
sync.Mutex
37-
//client *containerd.Client
3837
containers map[string]*containerPack
3938
hooks []func(string, *Message) error
39+
40+
// containerdDead to specify whether containerd process is dead
41+
containerdDead bool
42+
}
43+
44+
func (w *watch) setContainerdDead(isDead bool) error {
45+
w.Lock()
46+
defer w.Unlock()
47+
48+
w.containerdDead = isDead
49+
return nil
50+
}
51+
52+
func (w *watch) isContainerdDead() bool {
53+
w.Lock()
54+
defer w.Unlock()
55+
56+
return w.containerdDead
4057
}
4158

4259
func (w *watch) add(pack *containerPack) {
@@ -49,9 +66,16 @@ func (w *watch) add(pack *containerPack) {
4966

5067
w.containers[pack.id] = pack
5168

52-
go func(pack *containerPack) {
69+
go func(w *watch, pack *containerPack) {
5370
status := <-pack.sch
5471

72+
// if containerd is dead, the task.Wait channel return is not because
73+
// container' task quit, but the channel has broken.
74+
// so we just return.
75+
if w.isContainerdDead() {
76+
return
77+
}
78+
5579
logrus.Infof("the task has quit, id: %s, err: %v, exitcode: %d, time: %v",
5680
pack.id, status.Error(), status.ExitCode(), status.ExitTime())
5781

@@ -85,7 +109,7 @@ func (w *watch) add(pack *containerPack) {
85109

86110
pack.ch <- msg
87111

88-
}(pack)
112+
}(w, pack)
89113

90114
logrus.Infof("success to add container, id: %s", pack.id)
91115
}

0 commit comments

Comments
 (0)