@@ -22,6 +22,7 @@ import (
2222 "errors"
2323 "fmt"
2424 "runtime"
25+ "time"
2526
2627 "github.com/docker/go-units"
2728 runtimespec "github.com/opencontainers/runtime-spec/specs-go"
@@ -241,7 +242,7 @@ func getUpdateOption(cmd *cobra.Command, globalOptions types.GlobalCommandOption
241242 return options , nil
242243}
243244
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 ) {
245246 container , err := client .LoadContainer (ctx , id )
246247 if err != nil {
247248 return err
@@ -339,12 +340,18 @@ func updateContainer(ctx context.Context, client *containerd.Client, id string,
339340 }
340341
341342 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+ }
346353 }
347- }
354+ }()
348355
349356 restart , err := cmd .Flags ().GetString ("restart" )
350357 if err != nil {
0 commit comments