@@ -35,6 +35,7 @@ import (
3535 "github.com/containerd/containerd/plugin"
3636 "github.com/containerd/containerd/runtime"
3737 ocispec "github.com/opencontainers/image-spec/specs-go/v1"
38+ "github.com/pkg/errors"
3839)
3940
4041// Config for the v2 runtime
@@ -117,20 +118,45 @@ func (m *TaskManager) ID() string {
117118}
118119
119120// Create a new task
120- func (m * TaskManager ) Create (ctx context.Context , id string , opts runtime.CreateOpts ) (_ runtime.Task , err error ) {
121- ns , err := namespaces . NamespaceRequired (ctx )
121+ func (m * TaskManager ) Create (ctx context.Context , id string , opts runtime.CreateOpts ) (_ runtime.Task , retErr error ) {
122+ bundle , err := NewBundle (ctx , m . root , m . state , id , opts . Spec . Value )
122123 if err != nil {
123124 return nil , err
124125 }
125- bundle , err := NewBundle (ctx , m .root , m .state , id , opts .Spec .Value )
126+ defer func () {
127+ if retErr != nil {
128+ bundle .Delete ()
129+ }
130+ }()
131+
132+ shim , err := m .startShim (ctx , bundle , id , opts )
126133 if err != nil {
127134 return nil , err
128135 }
129136 defer func () {
130- if err != nil {
131- bundle . Delete ( )
137+ if retErr != nil {
138+ m . deleteShim ( shim )
132139 }
133140 }()
141+
142+ t , err := shim .Create (ctx , opts )
143+ if err != nil {
144+ return nil , errors .Wrap (err , "failed to create shim" )
145+ }
146+
147+ if err := m .tasks .Add (ctx , t ); err != nil {
148+ return nil , errors .Wrap (err , "failed to add task" )
149+ }
150+
151+ return t , nil
152+ }
153+
154+ func (m * TaskManager ) startShim (ctx context.Context , bundle * Bundle , id string , opts runtime.CreateOpts ) (* shim , error ) {
155+ ns , err := namespaces .NamespaceRequired (ctx )
156+ if err != nil {
157+ return nil , err
158+ }
159+
134160 topts := opts .TaskOptions
135161 if topts == nil {
136162 topts = opts .RuntimeOptions
@@ -148,29 +174,26 @@ func (m *TaskManager) Create(ctx context.Context, id string, opts runtime.Create
148174 m .tasks .Delete (ctx , id )
149175 })
150176 if err != nil {
151- return nil , err
177+ return nil , errors . Wrap ( err , "start failed" )
152178 }
153- defer func () {
154- if err != nil {
155- dctx , cancel := timeout .WithContext (context .Background (), cleanupTimeout )
179+
180+ return shim , nil
181+ }
182+
183+ // deleteShim attempts to properly delete and cleanup shim after error
184+ func (m * TaskManager ) deleteShim (shim * shim ) {
185+ dctx , cancel := timeout .WithContext (context .Background (), cleanupTimeout )
186+ defer cancel ()
187+
188+ _ , errShim := shim .Delete (dctx )
189+ if errShim != nil {
190+ if errdefs .IsDeadlineExceeded (errShim ) {
191+ dctx , cancel = timeout .WithContext (context .Background (), cleanupTimeout )
156192 defer cancel ()
157- _ , errShim := shim .Delete (dctx )
158- if errShim != nil {
159- if errdefs .IsDeadlineExceeded (errShim ) {
160- dctx , cancel = timeout .WithContext (context .Background (), cleanupTimeout )
161- defer cancel ()
162- }
163- shim .Shutdown (dctx )
164- shim .Close ()
165- }
166193 }
167- }()
168- t , err := shim .Create (ctx , opts )
169- if err != nil {
170- return nil , err
194+ shim .Shutdown (dctx )
195+ shim .Close ()
171196 }
172- m .tasks .Add (ctx , t )
173- return t , nil
174197}
175198
176199// Get a specific task
0 commit comments