@@ -22,6 +22,7 @@ import (
22
22
"errors"
23
23
"fmt"
24
24
"runtime"
25
+ "time"
25
26
26
27
"github.com/docker/go-units"
27
28
runtimespec "github.com/opencontainers/runtime-spec/specs-go"
@@ -241,7 +242,7 @@ func getUpdateOption(cmd *cobra.Command, globalOptions types.GlobalCommandOption
241
242
return options , nil
242
243
}
243
244
244
- func updateContainer (ctx context.Context , client * containerd.Client , id string , opts updateResourceOptions , cmd * cobra.Command ) error {
245
+ func updateContainer (ctx context.Context , client * containerd.Client , id string , opts updateResourceOptions , cmd * cobra.Command ) ( retErr error ) {
245
246
container , err := client .LoadContainer (ctx , id )
246
247
if err != nil {
247
248
return err
@@ -339,12 +340,18 @@ func updateContainer(ctx context.Context, client *containerd.Client, id string,
339
340
}
340
341
341
342
if err := updateContainerSpec (ctx , container , spec ); err != nil {
342
- log .G (ctx ).WithError (err ).Errorf ("Failed to update spec %+v for container %q" , spec , id )
343
- // reset spec on error.
344
- if err := updateContainerSpec (ctx , container , oldSpec ); err != nil {
345
- log .G (ctx ).WithError (err ).Errorf ("Failed to update spec %+v for container %q" , oldSpec , id )
343
+ return fmt .Errorf ("failed to update spec %+v for container %q" , spec , id )
344
+ }
345
+ defer func () {
346
+ if retErr != nil {
347
+ deferCtx , deferCancel := context .WithTimeout (ctx , 1 * time .Minute )
348
+ defer deferCancel ()
349
+ // Reset spec on error.
350
+ if err := updateContainerSpec (deferCtx , container , oldSpec ); err != nil {
351
+ log .G (ctx ).WithError (err ).Errorf ("Failed to update spec %+v for container %q" , oldSpec , id )
352
+ }
346
353
}
347
- }
354
+ }()
348
355
349
356
restart , err := cmd .Flags ().GetString ("restart" )
350
357
if err != nil {
0 commit comments