Skip to content

Commit 0581e02

Browse files
author
yangshukui
committed
Kill shim on restore if the bundle dir is missing
Some anomalies due to the reboot of containerd(killed by docker daemon), can lead to docker-containerd-shim residue and shim process will not exit except you kill it manually. Signed-off-by: yangshukui <yangshukui@huawei.com>
1 parent f1a935c commit 0581e02

File tree

7 files changed

+56
-17
lines changed

7 files changed

+56
-17
lines changed

containerd-shim/process.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"syscall"
1616
"time"
1717

18+
"github.com/containerd/containerd/osutils"
1819
"github.com/containerd/containerd/specs"
1920
)
2021

@@ -177,8 +178,8 @@ func (p *process) create() error {
177178
cmd.Stdin = p.stdio.stdin
178179
cmd.Stdout = p.stdio.stdout
179180
cmd.Stderr = p.stdio.stderr
180-
// Call out to setPDeathSig to set SysProcAttr as elements are platform specific
181-
cmd.SysProcAttr = setPDeathSig()
181+
// Call out to SetPDeathSig to set SysProcAttr as elements are platform specific
182+
cmd.SysProcAttr = osutils.SetPDeathSig()
182183

183184
if err := cmd.Start(); err != nil {
184185
if exErr, ok := err.(*exec.Error); ok {
@@ -220,7 +221,7 @@ func (p *process) pid() int {
220221
func (p *process) delete() error {
221222
if !p.state.Exec {
222223
cmd := exec.Command(p.runtime, append(p.state.RuntimeArgs, "delete", p.id)...)
223-
cmd.SysProcAttr = setPDeathSig()
224+
cmd.SysProcAttr = osutils.SetPDeathSig()
224225
out, err := cmd.CombinedOutput()
225226
if err != nil {
226227
return fmt.Errorf("%s: %v", out, err)

containerd-shim/process_linux.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,11 @@ import (
99
"syscall"
1010
"time"
1111

12+
"github.com/containerd/containerd/osutils"
1213
"github.com/tonistiigi/fifo"
1314
"golang.org/x/net/context"
1415
)
1516

16-
// setPDeathSig sets the parent death signal to SIGKILL so that if the
17-
// shim dies the container process also dies.
18-
func setPDeathSig() *syscall.SysProcAttr {
19-
return &syscall.SysProcAttr{
20-
Pdeathsig: syscall.SIGKILL,
21-
}
22-
}
23-
2417
// openIO opens the pre-created fifo's for use with the container
2518
// in RDWR so that they remain open if the other side stops listening
2619
func (p *process) openIO() error {
@@ -141,7 +134,7 @@ func (p *process) Wait() {
141134
func (p *process) killAll() error {
142135
if !p.state.Exec {
143136
cmd := exec.Command(p.runtime, append(p.state.RuntimeArgs, "kill", "--all", p.id, "SIGKILL")...)
144-
cmd.SysProcAttr = setPDeathSig()
137+
cmd.SysProcAttr = osutils.SetPDeathSig()
145138
out, err := cmd.CombinedOutput()
146139
if err != nil {
147140
return fmt.Errorf("%s: %v", out, err)

containerd-shim/process_solaris.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@ import (
88
"syscall"
99
)
1010

11-
// setPDeathSig is a no-op on Solaris as Pdeathsig is not defined.
12-
func setPDeathSig() *syscall.SysProcAttr {
13-
return nil
14-
}
15-
1611
// TODO: Update to using fifo's package in openIO. Need to
1712
// 1. Merge and vendor changes in the package to use sys/unix.
1813
// 2. Figure out why context.Background is timing out.

osutils/pdeathsig_linux.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// +build !solaris
2+
3+
package osutils
4+
5+
import (
6+
"syscall"
7+
)
8+
9+
// SetPDeathSig sets the parent death signal to SIGKILL so that if the
10+
// shim dies the container process also dies.
11+
func SetPDeathSig() *syscall.SysProcAttr {
12+
return &syscall.SysProcAttr{
13+
Pdeathsig: syscall.SIGKILL,
14+
}
15+
}

osutils/pdeathsig_solaris.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// +build solaris
2+
3+
package osutils
4+
5+
// SetPDeathSig is a no-op on Solaris as Pdeathsig is not defined.
6+
func SetPDeathSig() *syscall.SysProcAttr {
7+
return nil
8+
}

runtime/container.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,17 @@ func Load(root, id, shimName string, timeout time.Duration) (Container, error) {
193193
}
194194
c.processes[pid] = p
195195
}
196+
197+
_, err = os.Stat(c.bundle)
198+
if err != nil && !os.IsExist(err) {
199+
for key, p := range c.processes {
200+
if key == InitProcessID {
201+
p.Delete()
202+
break
203+
}
204+
}
205+
return nil, fmt.Errorf("bundle dir %s don't exist", c.bundle)
206+
}
196207
return c, nil
197208
}
198209

runtime/process.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"time"
1616

1717
"github.com/Sirupsen/logrus"
18+
"github.com/containerd/containerd/osutils"
1819
"github.com/containerd/containerd/specs"
1920
"golang.org/x/sys/unix"
2021
)
@@ -458,3 +459,18 @@ func (p *process) Start() error {
458459
}
459460
return nil
460461
}
462+
463+
// Delete delete any resources held by the container
464+
func (p *process) Delete() error {
465+
var (
466+
args = append(p.container.runtimeArgs, "delete", "-f", p.container.id)
467+
cmd = exec.Command(p.container.runtime, args...)
468+
)
469+
470+
cmd.SysProcAttr = osutils.SetPDeathSig()
471+
out, err := cmd.CombinedOutput()
472+
if err != nil {
473+
return fmt.Errorf("%s: %v", out, err)
474+
}
475+
return nil
476+
}

0 commit comments

Comments
 (0)