diff --git a/src/machine/image-dbus.c b/src/machine/image-dbus.c index 801ecd419a..d8068cdfcd 100644 --- a/src/machine/image-dbus.c +++ b/src/machine/image-dbus.c @@ -127,9 +127,17 @@ int bus_image_method_rename( if (r == 0) return 1; /* Will call us back */ + /* The image is cached with its name, hence it is necessary to remove from the cache before renaming. */ + assert_se(hashmap_remove_value(m->image_cache, image->name, image)); + r = image_rename(image, new_name); - if (r < 0) + if (r < 0) { + image_unref(image); return r; + } + + /* Then save the object again in the cache. */ + assert_se(hashmap_put(m->image_cache, image->name, image) > 0); return sd_bus_reply_method_return(message, NULL); }