Skip to content

Commit abc98dc

Browse files
committed
When LockAcquire fails at the stage of creating a proclock object, be
sure to clean up the already-created lock object, if it has no other references. Avoids possibly-permanent leak of shared memory.
1 parent 493f726 commit abc98dc

File tree

1 file changed

+18
-2
lines changed
  • src/backend/storage/lmgr

1 file changed

+18
-2
lines changed

src/backend/storage/lmgr/lock.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/storage/lmgr/lock.c,v 1.139 2004/08/29 05:06:48 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/storage/lmgr/lock.c,v 1.140 2004/09/12 18:30:50 tgl Exp $
1212
*
1313
* NOTES
1414
* Outside modules can create a lock table and acquire/release
@@ -602,7 +602,23 @@ LockAcquire(LOCKMETHODID lockmethodid, LOCKTAG *locktag,
602602
HASH_ENTER, &found);
603603
if (!proclock)
604604
{
605+
/* Ooops, not enough shmem for the proclock */
606+
if (lock->nRequested == 0)
607+
{
608+
/*
609+
* There are no other requestors of this lock, so garbage-collect
610+
* the lock object. We *must* do this to avoid a permanent leak
611+
* of shared memory, because there won't be anything to cause
612+
* anyone to release the lock object later.
613+
*/
614+
Assert(SHMQueueEmpty(&(lock->procLocks)));
615+
lock = (LOCK *) hash_search(LockMethodLockHash[lockmethodid],
616+
(void *) &(lock->tag),
617+
HASH_REMOVE, NULL);
618+
}
605619
LWLockRelease(masterLock);
620+
if (!lock) /* hash remove failed? */
621+
elog(WARNING, "lock table corrupted");
606622
ereport(ERROR,
607623
(errcode(ERRCODE_OUT_OF_MEMORY),
608624
errmsg("out of shared memory"),
@@ -1528,7 +1544,7 @@ LockReleaseAll(LOCKMETHODID lockmethodid, bool allxids)
15281544
if (!lock)
15291545
{
15301546
LWLockRelease(masterLock);
1531-
elog(WARNING, "cannot remove lock from HTAB");
1547+
elog(WARNING, "lock table corrupted");
15321548
return FALSE;
15331549
}
15341550
}

0 commit comments

Comments
 (0)