Skip to content

Commit c9c555c

Browse files
authored
Merge pull request #3226 from Ace-Tang/kill_shim_in_clean
runtime-v1: kill shim in exit handler
2 parents ec0b722 + dfa51c9 commit c9c555c

2 files changed

Lines changed: 25 additions & 16 deletions

File tree

runtime/v1/linux/runtime.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -191,18 +191,13 @@ func (r *Runtime) Create(ctx context.Context, id string, opts runtime.CreateOpts
191191
}
192192
exitHandler := func() {
193193
log.G(ctx).WithField("id", id).Info("shim reaped")
194-
t, err := r.tasks.Get(ctx, id)
195-
if err != nil {
194+
195+
if _, err := r.tasks.Get(ctx, id); err != nil {
196196
// Task was never started or was already successfully deleted
197197
return
198198
}
199-
lc := t.(*Task)
200199

201-
log.G(ctx).WithFields(logrus.Fields{
202-
"id": id,
203-
"namespace": namespace,
204-
}).Warn("cleaning up after killed shim")
205-
if err = r.cleanupAfterDeadShim(context.Background(), bundle, namespace, id, lc.pid); err != nil {
200+
if err = r.cleanupAfterDeadShim(context.Background(), bundle, namespace, id); err != nil {
206201
log.G(ctx).WithError(err).WithFields(logrus.Fields{
207202
"id": id,
208203
"namespace": namespace,
@@ -342,12 +337,12 @@ func (r *Runtime) loadTasks(ctx context.Context, ns string) ([]*Task, error) {
342337
ctx = namespaces.WithNamespace(ctx, ns)
343338
pid, _ := runc.ReadPidFile(filepath.Join(bundle.path, proc.InitPidFile))
344339
s, err := bundle.NewShimClient(ctx, ns, ShimConnect(r.config, func() {
345-
_, err := r.tasks.Get(ctx, id)
346-
if err != nil {
340+
if _, err := r.tasks.Get(ctx, id); err != nil {
347341
// Task was never started or was already successfully deleted
348342
return
349343
}
350-
if err := r.cleanupAfterDeadShim(ctx, bundle, ns, id, pid); err != nil {
344+
345+
if err := r.cleanupAfterDeadShim(ctx, bundle, ns, id); err != nil {
351346
log.G(ctx).WithError(err).WithField("bundle", bundle.path).
352347
Error("cleaning up after dead shim")
353348
}
@@ -357,7 +352,7 @@ func (r *Runtime) loadTasks(ctx context.Context, ns string) ([]*Task, error) {
357352
"id": id,
358353
"namespace": ns,
359354
}).Error("connecting to shim")
360-
err := r.cleanupAfterDeadShim(ctx, bundle, ns, id, pid)
355+
err := r.cleanupAfterDeadShim(ctx, bundle, ns, id)
361356
if err != nil {
362357
log.G(ctx).WithError(err).WithField("bundle", bundle.path).
363358
Error("cleaning up after dead shim")
@@ -407,7 +402,13 @@ func (r *Runtime) loadTasks(ctx context.Context, ns string) ([]*Task, error) {
407402
return o, nil
408403
}
409404

410-
func (r *Runtime) cleanupAfterDeadShim(ctx context.Context, bundle *bundle, ns, id string, pid int) error {
405+
func (r *Runtime) cleanupAfterDeadShim(ctx context.Context, bundle *bundle, ns, id string) error {
406+
log.G(ctx).WithFields(logrus.Fields{
407+
"id": id,
408+
"namespace": ns,
409+
}).Warn("cleaning up after shim dead")
410+
411+
pid, _ := runc.ReadPidFile(filepath.Join(bundle.path, proc.InitPidFile))
411412
ctx = namespaces.WithNamespace(ctx, ns)
412413
if err := r.terminate(ctx, bundle, ns, id); err != nil {
413414
if r.config.ShimDebug {
@@ -430,6 +431,10 @@ func (r *Runtime) cleanupAfterDeadShim(ctx context.Context, bundle *bundle, ns,
430431
if err := bundle.Delete(); err != nil {
431432
log.G(ctx).WithError(err).Error("delete bundle")
432433
}
434+
// kill shim
435+
if shimPid, err := runc.ReadPidFile(filepath.Join(bundle.path, "shim.pid")); err == nil && shimPid > 0 {
436+
unix.Kill(shimPid, unix.SIGKILL)
437+
}
433438

434439
r.events.Publish(ctx, runtime.TaskDeleteEventTopic, &eventstypes.TaskDelete{
435440
ContainerID: id,

runtime/v1/shim/client/client.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"os"
2727
"os/exec"
2828
"path/filepath"
29+
"strconv"
2930
"strings"
3031
"sync"
3132
"syscall"
@@ -110,7 +111,10 @@ func WithStart(binary, address, daemonAddress, cgroup string, debug bool, exitHa
110111
"debug": debug,
111112
}).Infof("shim %s started", binary)
112113

113-
if err := writeAddress(filepath.Join(config.Path, "address"), address); err != nil {
114+
if err := writeFile(filepath.Join(config.Path, "address"), address); err != nil {
115+
return nil, nil, err
116+
}
117+
if err := writeFile(filepath.Join(config.Path, "shim.pid"), strconv.Itoa(cmd.Process.Pid)); err != nil {
114118
return nil, nil, err
115119
}
116120
// set shim in cgroup if it is provided
@@ -172,8 +176,8 @@ func newCommand(binary, daemonAddress string, debug bool, config shim.Config, so
172176
return cmd, nil
173177
}
174178

175-
// writeAddress writes a address file atomically
176-
func writeAddress(path, address string) error {
179+
// writeFile writes a address file atomically
180+
func writeFile(path, address string) error {
177181
path, err := filepath.Abs(path)
178182
if err != nil {
179183
return err

0 commit comments

Comments
 (0)