Skip to content

Commit 693eabf

Browse files
committed
librbd: images aren't closed in group_snap_*_by_record() on error
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 <[email protected]>
1 parent 614e83c commit 693eabf

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/librbd/api/Group.cc

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -180,27 +180,30 @@ int group_snap_remove_by_record(librados::IoCtx& group_ioctx,
180180
const std::string& group_header_oid) {
181181

182182
CephContext *cct = (CephContext *)group_ioctx.cct();
183+
std::vector<librados::IoCtx> ioctxs;
184+
std::vector<librbd::ImageCtx*> ictxs;
183185
std::vector<C_SaferCond*> on_finishes;
184186
int r, ret_code;
185187

186-
std::vector<librbd::ImageCtx*> ictxs;
187-
188188
cls::rbd::GroupSnapshotNamespace ne{group_ioctx.get_id(), group_id,
189189
group_snap.id};
190190

191191
ldout(cct, 20) << "Removing snapshots" << dendl;
192192
int snap_count = group_snap.snaps.size();
193193

194194
for (int i = 0; i < snap_count; ++i) {
195-
librbd::IoCtx image_io_ctx;
195+
librados::IoCtx image_io_ctx;
196196
r = util::create_ioctx(group_ioctx, "image", group_snap.snaps[i].pool, {},
197197
&image_io_ctx);
198198
if (r < 0) {
199199
return r;
200200
}
201+
ioctxs.push_back(std::move(image_io_ctx));
202+
}
201203

204+
for (int i = 0; i < snap_count; ++i) {
202205
librbd::ImageCtx* image_ctx = new ImageCtx("", group_snap.snaps[i].image_id,
203-
nullptr, image_io_ctx, false);
206+
nullptr, ioctxs[i], false);
204207

205208
C_SaferCond* on_finish = new C_SaferCond;
206209

@@ -286,11 +289,11 @@ int group_snap_rollback_by_record(librados::IoCtx& group_ioctx,
286289
const std::string& group_id,
287290
ProgressContext& pctx) {
288291
CephContext *cct = (CephContext *)group_ioctx.cct();
292+
std::vector<librados::IoCtx> ioctxs;
293+
std::vector<librbd::ImageCtx*> ictxs;
289294
std::vector<C_SaferCond*> on_finishes;
290295
int r, ret_code;
291296

292-
std::vector<librbd::ImageCtx*> ictxs;
293-
294297
cls::rbd::GroupSnapshotNamespace ne{group_ioctx.get_id(), group_id,
295298
group_snap.id};
296299

@@ -304,9 +307,12 @@ int group_snap_rollback_by_record(librados::IoCtx& group_ioctx,
304307
if (r < 0) {
305308
return r;
306309
}
310+
ioctxs.push_back(std::move(image_io_ctx));
311+
}
307312

313+
for (int i = 0; i < snap_count; ++i) {
308314
librbd::ImageCtx* image_ctx = new ImageCtx("", group_snap.snaps[i].image_id,
309-
nullptr, image_io_ctx, false);
315+
nullptr, ioctxs[i], false);
310316

311317
C_SaferCond* on_finish = new C_SaferCond;
312318

0 commit comments

Comments
 (0)