Fix memory leak on DSM slot exhaustion.
authorThomas Munro <tmunro@postgresql.org>
Sat, 1 Feb 2020 01:29:13 +0000 (14:29 +1300)
committerThomas Munro <tmunro@postgresql.org>
Sat, 1 Feb 2020 01:29:13 +0000 (14:29 +1300)
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

src/backend/storage/ipc/dsm.c

index 1ed6ed2b1d1f0b9ed6b881d836ea8394c0eb2495..ef64d083570a4449c171b44ddebf2b9edc9c1e97 100644 (file)
@@ -479,17 +479,16 @@ dsm_create(Size size, int flags)
        /* Verify that we can support an additional mapping. */
        if (nitems >= dsm_control->maxitems)
        {
+               LWLockRelease(DynamicSharedMemoryControlLock);
+               dsm_impl_op(DSM_OP_DESTROY, seg->handle, 0, &seg->impl_private,
+                                       &seg->mapped_address, &seg->mapped_size, WARNING);
+               if (seg->resowner != NULL)
+                       ResourceOwnerForgetDSM(seg->resowner, seg);
+               dlist_delete(&seg->node);
+               pfree(seg);
+
                if ((flags & DSM_CREATE_NULL_IF_MAXSEGMENTS) != 0)
-               {
-                       LWLockRelease(DynamicSharedMemoryControlLock);
-                       dsm_impl_op(DSM_OP_DESTROY, seg->handle, 0, &seg->impl_private,
-                                               &seg->mapped_address, &seg->mapped_size, WARNING);
-                       if (seg->resowner != NULL)
-                               ResourceOwnerForgetDSM(seg->resowner, seg);
-                       dlist_delete(&seg->node);
-                       pfree(seg);
                        return NULL;
-               }
                ereport(ERROR,
                                (errcode(ERRCODE_INSUFFICIENT_RESOURCES),
                                 errmsg("too many dynamic shared memory segments")));