]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
librbd: images aren't closed in group_snap_*_by_record() on error
authorMiki Patel <miki.patel132@gmail.com>
Tue, 15 Jul 2025 11:07:16 +0000 (16:37 +0530)
committerIlya Dryomov <idryomov@gmail.com>
Tue, 22 Jul 2025 15:03:46 +0000 (17:03 +0200)
Fixes memory leak and handles resource leak scenario when at leat one IoCtx is not
created successfully. This is done by returning error before opening any image.
Changes are made in group_snap_remove_by_record and group_snap_rollback_by_record

Fixes: https://tracker.ceph.com/issues/71961
Signed-off-by: Miki Patel <miki.patel132@gmail.com>
(cherry picked from commit 693eabf58cc19eba4c21e2ea7c0011643659b3c3)

src/librbd/api/Group.cc

index 9ce7e033ef81ea222748f3a213dfac1308b14a6a..f38d38fd5f918dc2841dc6dd6520288094852fd6 100644 (file)
@@ -205,11 +205,11 @@ int group_snap_remove_by_record(librados::IoCtx& group_ioctx,
                                const std::string& group_header_oid) {
 
   CephContext *cct = (CephContext *)group_ioctx.cct();
+  std::vector<librados::IoCtx> ioctxs;
+  std::vector<librbd::ImageCtx*> ictxs;
   std::vector<C_SaferCond*> on_finishes;
   int r, ret_code;
 
-  std::vector<librbd::ImageCtx*> ictxs;
-
   cls::rbd::GroupSnapshotNamespace ne{group_ioctx.get_id(), group_id,
                                      group_snap.id};
 
@@ -217,15 +217,18 @@ int group_snap_remove_by_record(librados::IoCtx& group_ioctx,
   int snap_count = group_snap.snaps.size();
 
   for (int i = 0; i < snap_count; ++i) {
-    librbd::IoCtx image_io_ctx;
+    librados::IoCtx image_io_ctx;
     r = util::create_ioctx(group_ioctx, "image", group_snap.snaps[i].pool, {},
                            &image_io_ctx);
     if (r < 0) {
       return r;
     }
+    ioctxs.push_back(std::move(image_io_ctx));
+  }
 
+  for (int i = 0; i < snap_count; ++i) {
     librbd::ImageCtx* image_ctx = new ImageCtx("", group_snap.snaps[i].image_id,
-                                              nullptr, image_io_ctx, false);
+                                              nullptr, ioctxs[i], false);
 
     C_SaferCond* on_finish = new C_SaferCond;
 
@@ -311,11 +314,11 @@ int group_snap_rollback_by_record(librados::IoCtx& group_ioctx,
                                   const std::string& group_id,
                                   ProgressContext& pctx) {
   CephContext *cct = (CephContext *)group_ioctx.cct();
+  std::vector<librados::IoCtx> ioctxs;
+  std::vector<librbd::ImageCtx*> ictxs;
   std::vector<C_SaferCond*> on_finishes;
   int r, ret_code;
 
-  std::vector<librbd::ImageCtx*> ictxs;
-
   cls::rbd::GroupSnapshotNamespace ne{group_ioctx.get_id(), group_id,
                                       group_snap.id};
 
@@ -329,9 +332,12 @@ int group_snap_rollback_by_record(librados::IoCtx& group_ioctx,
     if (r < 0) {
       return r;
     }
+    ioctxs.push_back(std::move(image_io_ctx));
+  }
 
+  for (int i = 0; i < snap_count; ++i) {
     librbd::ImageCtx* image_ctx = new ImageCtx("", group_snap.snaps[i].image_id,
-                                               nullptr, image_io_ctx, false);
+                                               nullptr, ioctxs[i], false);
 
     C_SaferCond* on_finish = new C_SaferCond;