February 10, 2023
https://issues.dlang.org/show_bug.cgi?id=23684

          Issue ID: 23684
           Summary: std.process.spawnProcess fails if ulimit is too large
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: major
          Priority: P1
         Component: phobos
          Assignee: nobody@puremagic.com
          Reporter: d.bugs@webfreak.org

This code fails:

```d
import std.process;
void main()
{
    auto pid = spawnProcess(["ls"]);
    while (true) {}
}
```

with

`std.process.ProcessException@std/process.d(1196): Failed to allocate memory
(Cannot allocate memory)`

when the ulimit max open files, divided by 8, is equal or larger than the available memory.

This happened when trying to spawn a process inside a docker container:

install rocky linux (rhel) 768M mem + 768M swap
install docker
docker run --rm -it debian:bullseye
apt-get update && apt-get install -y gcc ldc
create d file
ldc2 -run test.d

Problem: it tries to allocate `RLIMIT_NOFILE * pollfd.sizeof` bytes to pass to poll to try which FDs are open to close them to not accidentally inherit them.

However RLIMIT_NOFILE is on this platform = 1073741816

Workaround: Config.inheritFDs will make std.process not try to close them, thus avoiding the malloc, thus avoiding the crash.

There should probably be a check if RLIMIT_NOFILE is very big and in that case probably try to chunk the polling or do a brute force approach or just see how other standard libraries implemented this issue.

--