Skip to content

Commit 3f694f1

Browse files
darfuxthaJeztah
authored andcommitted
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 <[email protected]> (cherry picked from commit 84464b8) Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 7a24105 commit 3f694f1

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

runtime/v2/binary.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,13 @@ func (b *binary) Start(ctx context.Context, opts *types.Any, onClose func()) (_
7777
}
7878
// Windows needs a namespace when openShimLog
7979
ns, _ := namespaces.Namespace(ctx)
80-
f, err := openShimLog(namespaces.WithNamespace(context.Background(), ns), b.bundle, client.AnonDialer)
80+
shimCtx, cancelShimLog := context.WithCancel(namespaces.WithNamespace(context.Background(), ns))
81+
defer func() {
82+
if err != nil {
83+
cancelShimLog()
84+
}
85+
}()
86+
f, err := openShimLog(shimCtx, b.bundle, client.AnonDialer)
8187
if err != nil {
8288
return nil, errors.Wrap(err, "open shim log pipe")
8389
}
@@ -106,7 +112,11 @@ func (b *binary) Start(ctx context.Context, opts *types.Any, onClose func()) (_
106112
if err != nil {
107113
return nil, err
108114
}
109-
client := ttrpc.NewClient(conn, ttrpc.WithOnClose(onClose))
115+
onCloseWithShimLog := func() {
116+
onClose()
117+
cancelShimLog()
118+
}
119+
client := ttrpc.NewClient(conn, ttrpc.WithOnClose(onCloseWithShimLog))
110120
return &shim{
111121
bundle: b.bundle,
112122
client: client,

0 commit comments

Comments
 (0)