June 22, 2023
https://issues.dlang.org/show_bug.cgi?id=24009

          Issue ID: 24009
           Summary: The garbage collector tries to allocate memory while
                    the program is out of memory
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P3
         Component: druntime
          Assignee: nobody@puremagic.com
          Reporter: dlang-bugzilla@thecybershadow.net

- When the program runs out of memory, the runtime starts a GC cycle.
- While the GC is running, the program is still out of memory, so requests for
more memory from the OS will fail.
- Therefore, it should not request more memory from the OS.
- The GC requests more memory from the OS while it is running.
- Therefore, the GC may cause the program to crash when it runs out of memory.

Unfortunately I do not have a standalone reproducer, but here is a stack trace which illustrates the problem:

#0  0x00005555559438b8 in onOutOfMemoryErrorNoGC ()
#1  0x000055555593ed4b in
core.internal.gc.impl.conservative.gc.Gcx.ToScanStack!(void*).ToScanStack.grow()
()
#2  0x0000555555939957 in
core.internal.gc.impl.conservative.gc.Gcx.collectRoots(void*, void*) ()
#3  0x00005555559426a0 in core.thread.threadbase.thread_scanAll(scope
void(void*, void*) nothrow
delegate).__lambda2!(core.thread.threadbase.ScanType, void*,
void*).__lambda2(core.thread.threadbase.ScanType, void*, void*) ()
#4  0x00005555559472dc in core.thread.threadbase.scanAllTypeImpl(scope
void(core.thread.threadbase.ScanType, void*, void*) nothrow delegate, void*) ()
#5  0x0000555555947219 in core.thread.threadbase.thread_scanAllType(scope
void(core.thread.threadbase.ScanType, void*, void*) nothrow
delegate).__lambda2!(void*).__lambda2(void*) ()
#6  0x00005555559426e0 in core.thread.osthread.callWithStackShell(scope
void(void*) nothrow delegate) ()
#7  0x00005555559471ee in thread_scanAllType ()
#8  0x0000555555942666 in thread_scanAll ()
#9  0x0000555555939a46 in
core.internal.gc.impl.conservative.gc.Gcx.collectAllRoots(bool) ()
#10 0x000055555593bb92 in
core.internal.gc.impl.conservative.gc.Gcx.markParallel(bool) ()
#11 0x000055555593b1e0 in
core.internal.gc.impl.conservative.gc.Gcx.fullcollect(bool, bool, bool) ()
#12 0x0000555555939269 in
core.internal.gc.impl.conservative.gc.Gcx.smallAlloc(ulong, ref ulong, uint,
const(TypeInfo)) ()
#13 0x000055555593f235 in
core.internal.gc.impl.conservative.gc.ConservativeGC.runLocked!(core.internal.gc.impl.conservative.gc.ConservativeGC.mallocNoSync(ulong,
uint, ref ulong, const(TypeInfo)),
core.internal.gc.impl.conservative.gc.mallocTime,
core.internal.gc.impl.conservative.gc.numMallocs, ulong, uint, ulong,
const(TypeInfo)).runLocked(ref ulong, ref uint, ref ulong, ref const(TypeInfo))
()
#14 0x0000555555936cce in
core.internal.gc.impl.conservative.gc.ConservativeGC.qalloc(ulong, uint, scope
const(TypeInfo)) ()
#15 0x00005555558c4ba3 in gc_qalloc ()
#16 0x000055555590e7fa in rt.lifetime.__arrayAlloc(ulong, ref
core.memory.BlkInfo_, scope const(TypeInfo), const(TypeInfo)) ()
#17 0x00005555558cc92e in _d_arrayappendcTX ()
#18 0x00005555558cc0e9 in _d_arrayappendT ()

--