Skip to content

Commit cd6c03b

Browse files
committed
Fix snapshot leak if lo_open called on non-existent object.
lo_open registers the currently active snapshot, and checks if the large object exists after that. Normally, snapshots registered by lo_open are unregistered at end of transaction when the lo descriptor is closed, but if we error out before the lo descriptor is added to the list of open descriptors, it is leaked. Fix by moving the snapshot registration to after checking if the large object exists. Reported by Pavel Stehule. Backpatch to 8.4. The snapshot registration system was introduced in 8.4, so prior versions are not affected (and not supported, anyway).
1 parent 03b6a6b commit cd6c03b

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed

src/backend/storage/large_object/inv_api.c

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -243,39 +243,47 @@ LargeObjectDesc *
243243
inv_open(Oid lobjId, int flags, MemoryContext mcxt)
244244
{
245245
LargeObjectDesc *retval;
246-
247-
retval = (LargeObjectDesc *) MemoryContextAlloc(mcxt,
248-
sizeof(LargeObjectDesc));
249-
250-
retval->id = lobjId;
251-
retval->subid = GetCurrentSubTransactionId();
252-
retval->offset = 0;
246+
Snapshot snapshot = NULL;
247+
int descflags = 0;
253248

254249
if (flags & INV_WRITE)
255250
{
256-
retval->snapshot = SnapshotNow;
257-
retval->flags = IFS_WRLOCK | IFS_RDLOCK;
251+
snapshot = SnapshotNow;
252+
descflags = IFS_WRLOCK | IFS_RDLOCK;
258253
}
259254
else if (flags & INV_READ)
260255
{
261-
/*
262-
* We must register the snapshot in TopTransaction's resowner, because
263-
* it must stay alive until the LO is closed rather than until the
264-
* current portal shuts down.
265-
*/
266-
retval->snapshot = RegisterSnapshotOnOwner(GetActiveSnapshot(),
267-
TopTransactionResourceOwner);
268-
retval->flags = IFS_RDLOCK;
256+
snapshot = GetActiveSnapshot();
257+
descflags = IFS_RDLOCK;
269258
}
270259
else
271260
elog(ERROR, "invalid flags: %d", flags);
272261

273262
/* Can't use LargeObjectExists here because it always uses SnapshotNow */
274-
if (!myLargeObjectExists(lobjId, retval->snapshot))
263+
if (!myLargeObjectExists(lobjId, snapshot))
275264
ereport(ERROR,
276265
(errcode(ERRCODE_UNDEFINED_OBJECT),
277266
errmsg("large object %u does not exist", lobjId)));
278267

268+
/*
269+
* We must register the snapshot in TopTransaction's resowner, because
270+
* it must stay alive until the LO is closed rather than until the
271+
* current portal shuts down. Do this after checking that the LO exists,
272+
* to avoid leaking the snapshot if an error is thrown.
273+
*/
274+
if (snapshot != SnapshotNow)
275+
snapshot = RegisterSnapshotOnOwner(snapshot,
276+
TopTransactionResourceOwner);
277+
278+
/* All set, create a descriptor */
279+
retval = (LargeObjectDesc *) MemoryContextAlloc(mcxt,
280+
sizeof(LargeObjectDesc));
281+
retval->id = lobjId;
282+
retval->subid = GetCurrentSubTransactionId();
283+
retval->offset = 0;
284+
retval->snapshot = snapshot;
285+
retval->flags = descflags;
286+
279287
return retval;
280288
}
281289

0 commit comments

Comments
 (0)