From 84464b801faf2c80b71f8fa057d1f8daf9474a1c Mon Sep 17 00:00:00 2001 From: Li Yuxuan Date: Thu, 20 Feb 2020 16:49:47 +0800 Subject: [PATCH] v2: Cancel shim log ctx when ttrpc is closed The background context aovids shim blocking when the ctx is cancelled unexpectedly during shim start. But if the shim exits unexpectedly before opening the pipe, the fd will never be closed. `onCloseWithShimLog` makes sure that the shim log fd is closed properly once the shim disconnects. Signed-off-by: Li Yuxuan --- runtime/v2/binary.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/runtime/v2/binary.go b/runtime/v2/binary.go index 30c72827d..4b3f4ab93 100644 --- a/runtime/v2/binary.go +++ b/runtime/v2/binary.go @@ -77,7 +77,13 @@ func (b *binary) Start(ctx context.Context, opts *types.Any, onClose func()) (_ } // Windows needs a namespace when openShimLog ns, _ := namespaces.Namespace(ctx) - f, err := openShimLog(namespaces.WithNamespace(context.Background(), ns), b.bundle, client.AnonDialer) + shimCtx, cancelShimLog := context.WithCancel(namespaces.WithNamespace(context.Background(), ns)) + defer func() { + if err != nil { + cancelShimLog() + } + }() + f, err := openShimLog(shimCtx, b.bundle, client.AnonDialer) if err != nil { return nil, errors.Wrap(err, "open shim log pipe") } @@ -106,7 +112,11 @@ func (b *binary) Start(ctx context.Context, opts *types.Any, onClose func()) (_ if err != nil { return nil, err } - client := ttrpc.NewClient(conn, ttrpc.WithOnClose(onClose)) + onCloseWithShimLog := func() { + onClose() + cancelShimLog() + } + client := ttrpc.NewClient(conn, ttrpc.WithOnClose(onCloseWithShimLog)) return &shim{ bundle: b.bundle, client: client,