Skip to content

Commit

Permalink
Kill shim on restore if the bundle dir is missing
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
yangshukui committed Apr 26, 2017
1 parent f1a935c commit 0581e02
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 17 deletions.
7 changes: 4 additions & 3 deletions containerd-shim/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"syscall"
"time"

"github.com/containerd/containerd/osutils"
"github.com/containerd/containerd/specs"
)

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

if err := cmd.Start(); err != nil {
if exErr, ok := err.(*exec.Error); ok {
Expand Down Expand Up @@ -220,7 +221,7 @@ func (p *process) pid() int {
func (p *process) delete() error {
if !p.state.Exec {
cmd := exec.Command(p.runtime, append(p.state.RuntimeArgs, "delete", p.id)...)
cmd.SysProcAttr = setPDeathSig()
cmd.SysProcAttr = osutils.SetPDeathSig()
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("%s: %v", out, err)
Expand Down
11 changes: 2 additions & 9 deletions containerd-shim/process_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,11 @@ import (
"syscall"
"time"

"github.com/containerd/containerd/osutils"
"github.com/tonistiigi/fifo"
"golang.org/x/net/context"
)

// setPDeathSig sets the parent death signal to SIGKILL so that if the
// shim dies the container process also dies.
func setPDeathSig() *syscall.SysProcAttr {
return &syscall.SysProcAttr{
Pdeathsig: syscall.SIGKILL,
}
}

// openIO opens the pre-created fifo's for use with the container
// in RDWR so that they remain open if the other side stops listening
func (p *process) openIO() error {
Expand Down Expand Up @@ -141,7 +134,7 @@ func (p *process) Wait() {
func (p *process) killAll() error {
if !p.state.Exec {
cmd := exec.Command(p.runtime, append(p.state.RuntimeArgs, "kill", "--all", p.id, "SIGKILL")...)
cmd.SysProcAttr = setPDeathSig()
cmd.SysProcAttr = osutils.SetPDeathSig()
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("%s: %v", out, err)
Expand Down
5 changes: 0 additions & 5 deletions containerd-shim/process_solaris.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@ import (
"syscall"
)

// setPDeathSig is a no-op on Solaris as Pdeathsig is not defined.
func setPDeathSig() *syscall.SysProcAttr {
return nil
}

// TODO: Update to using fifo's package in openIO. Need to
// 1. Merge and vendor changes in the package to use sys/unix.
// 2. Figure out why context.Background is timing out.
Expand Down
15 changes: 15 additions & 0 deletions osutils/pdeathsig_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// +build !solaris

package osutils

import (
"syscall"
)

// SetPDeathSig sets the parent death signal to SIGKILL so that if the
// shim dies the container process also dies.
func SetPDeathSig() *syscall.SysProcAttr {
return &syscall.SysProcAttr{
Pdeathsig: syscall.SIGKILL,
}
}
8 changes: 8 additions & 0 deletions osutils/pdeathsig_solaris.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// +build solaris

package osutils

// SetPDeathSig is a no-op on Solaris as Pdeathsig is not defined.
func SetPDeathSig() *syscall.SysProcAttr {
return nil
}
11 changes: 11 additions & 0 deletions runtime/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,17 @@ func Load(root, id, shimName string, timeout time.Duration) (Container, error) {
}
c.processes[pid] = p
}

_, err = os.Stat(c.bundle)
if err != nil && !os.IsExist(err) {
for key, p := range c.processes {
if key == InitProcessID {
p.Delete()
break
}
}
return nil, fmt.Errorf("bundle dir %s don't exist", c.bundle)
}
return c, nil
}

Expand Down
16 changes: 16 additions & 0 deletions runtime/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"time"

"github.com/Sirupsen/logrus"
"github.com/containerd/containerd/osutils"
"github.com/containerd/containerd/specs"
"golang.org/x/sys/unix"
)
Expand Down Expand Up @@ -458,3 +459,18 @@ func (p *process) Start() error {
}
return nil
}

// Delete delete any resources held by the container
func (p *process) Delete() error {
var (
args = append(p.container.runtimeArgs, "delete", "-f", p.container.id)
cmd = exec.Command(p.container.runtime, args...)
)

cmd.SysProcAttr = osutils.SetPDeathSig()
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("%s: %v", out, err)
}
return nil
}

0 comments on commit 0581e02

Please sign in to comment.