@@ -34,7 +34,6 @@ import (
3434 "github.com/containerd/containerd/plugin"
3535 "github.com/containerd/containerd/runtime"
3636 ocispec "github.com/opencontainers/image-spec/specs-go/v1"
37- "github.com/pkg/errors"
3837)
3938
4039func init () {
@@ -150,7 +149,12 @@ func (m *TaskManager) loadExistingTasks(ctx context.Context) error {
150149 ns := nsd .Name ()
151150 log .G (ctx ).WithField ("namespace" , ns ).Debug ("loading tasks in namespace" )
152151 if err := m .loadTasks (namespaces .WithNamespace (ctx , ns )); err != nil {
153- return err
152+ log .G (ctx ).WithField ("namespace" , ns ).WithError (err ).Error ("loading tasks in namespace" )
153+ continue
154+ }
155+ if err := m .cleanupWorkDirs (namespaces .WithNamespace (ctx , ns )); err != nil {
156+ log .G (ctx ).WithField ("namespace" , ns ).WithError (err ).Error ("cleanup working directory in namespace" )
157+ continue
154158 }
155159 }
156160 return nil
@@ -172,12 +176,16 @@ func (m *TaskManager) loadTasks(ctx context.Context) error {
172176 id := sd .Name ()
173177 bundle , err := LoadBundle (ctx , m .state , id )
174178 if err != nil {
179+ // fine to return error here, it is a programmer error if the context
180+ // does not have a namespace
175181 return err
176182 }
177183 // fast path
178184 bf , err := ioutil .ReadDir (bundle .Path )
179185 if err != nil {
180- return err
186+ bundle .Delete ()
187+ log .G (ctx ).WithError (err ).Errorf ("fast path read bundle path for %s" , bundle .Path )
188+ continue
181189 }
182190 if len (bf ) == 0 {
183191 bundle .Delete ()
@@ -197,7 +205,8 @@ func (m *TaskManager) loadTasks(ctx context.Context) error {
197205 }
198206 binaryCall := shimBinary (ctx , bundle , container .Runtime .Name , m .containerdAddress , m .events , m .tasks )
199207 if _ , err := binaryCall .Delete (ctx ); err != nil {
200- return errors .Wrapf (err , "remove disk state %s" , id )
208+ log .G (ctx ).WithError (err ).Errorf ("binary call to delete for %s" , id )
209+ continue
201210 }
202211 continue
203212 }
@@ -218,3 +227,26 @@ func (m *TaskManager) container(ctx context.Context, id string) (*containers.Con
218227 }
219228 return & container , nil
220229}
230+
231+ func (m * TaskManager ) cleanupWorkDirs (ctx context.Context ) error {
232+ ns , err := namespaces .NamespaceRequired (ctx )
233+ if err != nil {
234+ return err
235+ }
236+ dirs , err := ioutil .ReadDir (filepath .Join (m .root , ns ))
237+ if err != nil {
238+ return err
239+ }
240+ for _ , d := range dirs {
241+ // if the task was not loaded, cleanup and empty working directory
242+ // this can happen on a reboot where /run for the bundle state is cleaned up
243+ // but that persistent working dir is left
244+ if _ , err := m .tasks .Get (ctx , d .Name ()); err != nil {
245+ path := filepath .Join (m .root , ns , d .Name ())
246+ if err := os .RemoveAll (path ); err != nil {
247+ log .G (ctx ).WithError (err ).Errorf ("cleanup working dir %s" , path )
248+ }
249+ }
250+ }
251+ return nil
252+ }
0 commit comments