Skip to content

std.process.spawnProcess fails if ulimit is too large #10515

@dlangBugzillaToGithub

Description

@dlangBugzillaToGithub

d.bugs reported this on 2023-02-10T23:58:52Z

Transfered from https://issues.dlang.org/show_bug.cgi?id=23684

Description

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions