Skip to content

Commit aab30cd

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 de3d2df commit aab30cd

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
@@ -512,17 +512,16 @@ dsm_create(Size size, int flags)
512512
/* Verify that we can support an additional mapping. */
513513
if (nitems >= dsm_control->maxitems)
514514
{
515+
LWLockRelease(DynamicSharedMemoryControlLock);
516+
dsm_impl_op(DSM_OP_DESTROY, seg->handle, 0, &seg->impl_private,
517+
&seg->mapped_address, &seg->mapped_size, WARNING);
518+
if (seg->resowner != NULL)
519+
ResourceOwnerForgetDSM(seg->resowner, seg);
520+
dlist_delete(&seg->node);
521+
pfree(seg);
522+
515523
if ((flags & DSM_CREATE_NULL_IF_MAXSEGMENTS) != 0)
516-
{
517-
LWLockRelease(DynamicSharedMemoryControlLock);
518-
dsm_impl_op(DSM_OP_DESTROY, seg->handle, 0, &seg->impl_private,
519-
&seg->mapped_address, &seg->mapped_size, WARNING);
520-
if (seg->resowner != NULL)
521-
ResourceOwnerForgetDSM(seg->resowner, seg);
522-
dlist_delete(&seg->node);
523-
pfree(seg);
524524
return NULL;
525-
}
526525
ereport(ERROR,
527526
(errcode(ERRCODE_INSUFFICIENT_RESOURCES),
528527
errmsg("too many dynamic shared memory segments")));

0 commit comments

Comments
 (0)