Skip to content

Commit dfa51c9

Browse files
committed
runtime-v1: kill shim in cleanupAfterDeadShim
1. kill shim in cleanupAfterDeadShim avoid shim leak 2. refactor cleanupAfterDeadShim, get pid from bundle path instead of make pid as a parameter Signed-off-by: Ace-Tang <[email protected]>
1 parent 835e6d0 commit dfa51c9

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,
@@ -338,12 +333,12 @@ func (r *Runtime) loadTasks(ctx context.Context, ns string) ([]*Task, error) {
338333
ctx = namespaces.WithNamespace(ctx, ns)
339334
pid, _ := runc.ReadPidFile(filepath.Join(bundle.path, proc.InitPidFile))
340335
s, err := bundle.NewShimClient(ctx, ns, ShimConnect(r.config, func() {
341-
_, err := r.tasks.Get(ctx, id)
342-
if err != nil {
336+
if _, err := r.tasks.Get(ctx, id); err != nil {
343337
// Task was never started or was already successfully deleted
344338
return
345339
}
346-
if err := r.cleanupAfterDeadShim(ctx, bundle, ns, id, pid); err != nil {
340+
341+
if err := r.cleanupAfterDeadShim(ctx, bundle, ns, id); err != nil {
347342
log.G(ctx).WithError(err).WithField("bundle", bundle.path).
348343
Error("cleaning up after dead shim")
349344
}
@@ -353,7 +348,7 @@ func (r *Runtime) loadTasks(ctx context.Context, ns string) ([]*Task, error) {
353348
"id": id,
354349
"namespace": ns,
355350
}).Error("connecting to shim")
356-
err := r.cleanupAfterDeadShim(ctx, bundle, ns, id, pid)
351+
err := r.cleanupAfterDeadShim(ctx, bundle, ns, id)
357352
if err != nil {
358353
log.G(ctx).WithError(err).WithField("bundle", bundle.path).
359354
Error("cleaning up after dead shim")
@@ -395,7 +390,13 @@ func (r *Runtime) loadTasks(ctx context.Context, ns string) ([]*Task, error) {
395390
return o, nil
396391
}
397392

398-
func (r *Runtime) cleanupAfterDeadShim(ctx context.Context, bundle *bundle, ns, id string, pid int) error {
393+
func (r *Runtime) cleanupAfterDeadShim(ctx context.Context, bundle *bundle, ns, id string) error {
394+
log.G(ctx).WithFields(logrus.Fields{
395+
"id": id,
396+
"namespace": ns,
397+
}).Warn("cleaning up after shim dead")
398+
399+
pid, _ := runc.ReadPidFile(filepath.Join(bundle.path, proc.InitPidFile))
399400
ctx = namespaces.WithNamespace(ctx, ns)
400401
if err := r.terminate(ctx, bundle, ns, id); err != nil {
401402
if r.config.ShimDebug {
@@ -418,6 +419,10 @@ func (r *Runtime) cleanupAfterDeadShim(ctx context.Context, bundle *bundle, ns,
418419
if err := bundle.Delete(); err != nil {
419420
log.G(ctx).WithError(err).Error("delete bundle")
420421
}
422+
// kill shim
423+
if shimPid, err := runc.ReadPidFile(filepath.Join(bundle.path, "shim.pid")); err == nil && shimPid > 0 {
424+
unix.Kill(shimPid, unix.SIGKILL)
425+
}
421426

422427
r.events.Publish(ctx, runtime.TaskDeleteEventTopic, &eventstypes.TaskDelete{
423428
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)