Skip to content

Commit 1e683ff

Browse files
authored
Merge pull request #4755 from thaJeztah/1.3_backport_cancel_shim_log_ctx_by_onclose
[release/1.3 backport] v2: Cancel shim log ctx when ttrpc is closed
2 parents ea765ab + 3f694f1 commit 1e683ff

3 files changed

Lines changed: 25 additions & 6 deletions

File tree

runtime/v2/binary.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626

2727
"github.com/containerd/containerd/events/exchange"
2828
"github.com/containerd/containerd/log"
29+
"github.com/containerd/containerd/namespaces"
2930
"github.com/containerd/containerd/runtime"
3031
client "github.com/containerd/containerd/runtime/v2/shim"
3132
"github.com/containerd/containerd/runtime/v2/task"
@@ -74,7 +75,15 @@ func (b *binary) Start(ctx context.Context, opts *types.Any, onClose func()) (_
7475
if err != nil {
7576
return nil, err
7677
}
77-
f, err := openShimLog(ctx, b.bundle, client.AnonDialer)
78+
// Windows needs a namespace when openShimLog
79+
ns, _ := namespaces.Namespace(ctx)
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)
7887
if err != nil {
7988
return nil, errors.Wrap(err, "open shim log pipe")
8089
}
@@ -103,7 +112,11 @@ func (b *binary) Start(ctx context.Context, opts *types.Any, onClose func()) (_
103112
if err != nil {
104113
return nil, err
105114
}
106-
client := ttrpc.NewClient(conn, ttrpc.WithOnClose(onClose))
115+
onCloseWithShimLog := func() {
116+
onClose()
117+
cancelShimLog()
118+
}
119+
client := ttrpc.NewClient(conn, ttrpc.WithOnClose(onCloseWithShimLog))
107120
return &shim{
108121
bundle: b.bundle,
109122
client: client,

runtime/v2/manager.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/containerd/containerd/metadata"
3030
"github.com/containerd/containerd/mount"
3131
"github.com/containerd/containerd/namespaces"
32+
"github.com/containerd/containerd/pkg/timeout"
3233
"github.com/containerd/containerd/platforms"
3334
"github.com/containerd/containerd/plugin"
3435
"github.com/containerd/containerd/runtime"
@@ -154,8 +155,13 @@ func (m *TaskManager) Create(ctx context.Context, id string, opts runtime.Create
154155
}
155156
defer func() {
156157
if err != nil {
157-
shim.Shutdown(ctx)
158-
shim.Close()
158+
dctx, cancel := timeout.WithContext(context.Background(), cleanupTimeout)
159+
defer cancel()
160+
_, errShim := shim.Delete(dctx)
161+
if errShim != nil {
162+
shim.Shutdown(ctx)
163+
shim.Close()
164+
}
159165
}
160166
}()
161167
t, err := shim.Create(ctx, opts)

runtime/v2/shim.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,11 @@ func (s *shim) Delete(ctx context.Context) (*runtime.Exit, error) {
235235
// this seems dirty but it cleans up the API across runtimes, tasks, and the service
236236
s.rtTasks.Delete(ctx, s.ID())
237237
if err := s.waitShutdown(ctx); err != nil {
238-
log.G(ctx).WithError(err).Error("failed to shutdown shim")
238+
log.G(ctx).WithField("id", s.ID()).WithError(err).Error("failed to shutdown shim")
239239
}
240240
s.Close()
241241
if err := s.bundle.Delete(); err != nil {
242-
log.G(ctx).WithError(err).Error("failed to delete bundle")
242+
log.G(ctx).WithField("id", s.ID()).WithError(err).Error("failed to delete bundle")
243243
}
244244
if shimErr != nil {
245245
return nil, shimErr

0 commit comments

Comments
 (0)