From 87d15a5ffcea9833ef24b8ab97e35d893d94cfef Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Tue, 6 Feb 2018 10:45:50 -0500 Subject: [PATCH] Set OnClose shim function When restoring a task make sure that dead shims are handled. Fixes #2078 Signed-off-by: Michael Crosby --- linux/bundle.go | 4 ++-- linux/runtime.go | 12 +++++++++++- linux/shim/client/client.go | 8 +++++--- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/linux/bundle.go b/linux/bundle.go index fbed32224..f1d957cf8 100644 --- a/linux/bundle.go +++ b/linux/bundle.go @@ -84,9 +84,9 @@ func ShimLocal(exchange *exchange.Exchange) ShimOpt { } // ShimConnect is a ShimOpt for connecting to an existing remote shim -func ShimConnect() ShimOpt { +func ShimConnect(onClose func()) ShimOpt { return func(b *bundle, ns string, ropts *runctypes.RuncOptions) (shim.Config, client.Opt) { - return b.shimConfig(ns, ropts), client.WithConnect(b.shimAddress(ns)) + return b.shimConfig(ns, ropts), client.WithConnect(b.shimAddress(ns), onClose) } } diff --git a/linux/runtime.go b/linux/runtime.go index 5eb08c187..1d747b0d7 100644 --- a/linux/runtime.go +++ b/linux/runtime.go @@ -383,7 +383,17 @@ func (r *Runtime) loadTasks(ctx context.Context, ns string) ([]*Task, error) { ) ctx = namespaces.WithNamespace(ctx, ns) pid, _ := runc.ReadPidFile(filepath.Join(bundle.path, proc.InitPidFile)) - s, err := bundle.NewShimClient(ctx, ns, ShimConnect(), nil) + s, err := bundle.NewShimClient(ctx, ns, ShimConnect(func() { + log.G(ctx).WithError(err).WithFields(logrus.Fields{ + "id": id, + "namespace": ns, + }).Error("connecting to shim") + err := r.cleanupAfterDeadShim(ctx, bundle, ns, id, pid) + if err != nil { + log.G(ctx).WithError(err).WithField("bundle", bundle.path). + Error("cleaning up after dead shim") + } + }), nil) if err != nil { log.G(ctx).WithError(err).WithFields(logrus.Fields{ "id": id, diff --git a/linux/shim/client/client.go b/linux/shim/client/client.go index 6dd1a9fd7..aea7c8172 100644 --- a/linux/shim/client/client.go +++ b/linux/shim/client/client.go @@ -80,7 +80,7 @@ func WithStart(binary, address, daemonAddress, cgroup string, debug bool, exitHa if err = sys.SetOOMScore(cmd.Process.Pid, sys.OOMScoreMaxKillable); err != nil { return nil, nil, errors.Wrap(err, "failed to set OOM Score on shim") } - c, clo, err := WithConnect(address)(ctx, config) + c, clo, err := WithConnect(address, func() {})(ctx, config) if err != nil { return nil, nil, errors.Wrap(err, "failed to connect") } @@ -149,13 +149,15 @@ func annonDialer(address string, timeout time.Duration) (net.Conn, error) { } // WithConnect connects to an existing shim -func WithConnect(address string) Opt { +func WithConnect(address string, onClose func()) Opt { return func(ctx context.Context, config shim.Config) (shimapi.ShimService, io.Closer, error) { conn, err := connect(address, annonDialer) if err != nil { return nil, nil, err } - return shimapi.NewShimClient(ttrpc.NewClient(conn)), conn, nil + client := ttrpc.NewClient(conn) + client.OnClose(onClose) + return shimapi.NewShimClient(client), conn, nil } }