@@ -94,8 +94,9 @@ func restoreContainer(context *cli.Context, spec *specs.LinuxSpec, config *confi
9494 if err != nil {
9595 logrus .Error (err )
9696 }
97- if status == libcontainer .Running {
98- fatal (fmt .Errorf ("Container with id %s already running" , context .GlobalString ("id" )))
97+ _ , cerr := handleContainerstatus (status , container , context )
98+ if cerr != nil {
99+ return - 1 , cerr
99100 }
100101 // ensure that the container is always removed if we were the process
101102 // that created it.
@@ -143,6 +144,22 @@ func restoreContainer(context *cli.Context, spec *specs.LinuxSpec, config *confi
143144 return handler .forward (process )
144145}
145146
147+ func handleContainerstatus (status libcontainer.Status , container libcontainer.Container , context * cli.Context ) (int , error ) {
148+ switch status {
149+ case libcontainer .Running :
150+ return - 1 , fmt .Errorf ("Container with id %s already running" , context .GlobalString ("id" ))
151+ case libcontainer .Destroyed :
152+ defer func () {
153+ // Ensure to clean/destroy the container as the container id got created during restore startup
154+ if err := container .Destroy (); err != nil {
155+ logrus .Error (err )
156+ }
157+ }()
158+ return - 1 , fmt .Errorf ("Container with id %s does not exists" , context .GlobalString ("id" ))
159+ }
160+ return 1 , nil
161+ }
162+
146163func criuOptions (context * cli.Context ) * libcontainer.CriuOpts {
147164 imagePath := getCheckpointImagePath (context )
148165 if err := os .MkdirAll (imagePath , 0655 ); err != nil {
0 commit comments