Skip to content

Commit 24dd34a

Browse files
committed
Fix memory leak on DSM slot exhaustion.
If we attempt to create a DSM segment when no slots are available, we should return the memory to the operating system. Previously we did that if the DSM_CREATE_NULL_IF_MAXSEGMENTS flag was passed in, but we didn't do it if an error was raised. Repair. Back-patch to 9.4, where DSM segments arrived. Author: Thomas Munro Reviewed-by: Robert Haas Reported-by: Julian Backes Discussion: https://postgr.es/m/CA%2BhUKGKAAoEw-R4om0d2YM4eqT1eGEi6%3DQot-3ceDR-SLiWVDw%40mail.gmail.com
1 parent 65aa155 commit 24dd34a

File tree

1 file changed

+8
-9
lines changed
  • src/backend/storage/ipc

1 file changed

+8
-9
lines changed

src/backend/storage/ipc/dsm.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -484,17 +484,16 @@ dsm_create(Size size, int flags)
484484
/* Verify that we can support an additional mapping. */
485485
if (nitems >= dsm_control->maxitems)
486486
{
487+
LWLockRelease(DynamicSharedMemoryControlLock);
488+
dsm_impl_op(DSM_OP_DESTROY, seg->handle, 0, &seg->impl_private,
489+
&seg->mapped_address, &seg->mapped_size, WARNING);
490+
if (seg->resowner != NULL)
491+
ResourceOwnerForgetDSM(seg->resowner, seg);
492+
dlist_delete(&seg->node);
493+
pfree(seg);
494+
487495
if ((flags & DSM_CREATE_NULL_IF_MAXSEGMENTS) != 0)
488-
{
489-
LWLockRelease(DynamicSharedMemoryControlLock);
490-
dsm_impl_op(DSM_OP_DESTROY, seg->handle, 0, &seg->impl_private,
491-
&seg->mapped_address, &seg->mapped_size, WARNING);
492-
if (seg->resowner != NULL)
493-
ResourceOwnerForgetDSM(seg->resowner, seg);
494-
dlist_delete(&seg->node);
495-
pfree(seg);
496496
return NULL;
497-
}
498497
ereport(ERROR,
499498
(errcode(ERRCODE_INSUFFICIENT_RESOURCES),
500499
errmsg("too many dynamic shared memory segments")));

0 commit comments

Comments
 (0)