Skip to content

Conversation

@davidjulien
Copy link
Contributor

On some AWS/container Linux environments RLIMIT_NOFILE (and therefore sysconf(_SC_OPEN_MAX)) can be set extremely high (e.g. 1,048,576). Exile’s spawner was closing non-stdio fds by looping from 3.._SC_OPEN_MAX, which turns each spawn into ~1M close() syscalls and can add seconds of overhead even for trivial commands.

Fix this on Linux by:

  • Trying close_range(2) (Linux 5.9+) via syscall() to efficiently close all fds >= 3 in the kernel.
  • Falling back to scanning /proc/self/fd and closing only the fds that are actually open (O(open_fds)).
  • If both approaches fail, fall back to the historical close-loop.

Non-Linux platforms keep the existing behavior unchanged.

On some AWS/container Linux environments `RLIMIT_NOFILE` (and therefore
`sysconf(_SC_OPEN_MAX)`) can be set extremely high (e.g. 1,048,576). Exile’s
spawner was closing non-stdio fds by looping from 3.._SC_OPEN_MAX, which turns
each spawn into ~1M `close()` syscalls and can add seconds of overhead even for
trivial commands.

Fix this on Linux by:
- Trying `close_range(2)` (Linux 5.9+) via `syscall()` to efficiently close all
  fds >= 3 in the kernel.
- Falling back to scanning `/proc/self/fd` and closing only the fds that are
  actually open (O(open_fds)).
- If both approaches fail, fall back to the historical close-loop.

Non-Linux platforms keep the existing behavior unchanged.
@akash-akya
Copy link
Owner

akash-akya commented Dec 22, 2025

@davidjulien Thank you very much for investigating the issue and finding a fix! 👍🏼

I'll review and cut a release soon, within next few days.

@akash-akya
Copy link
Owner

@davidjulien Thanks for the PR and your patience!

On closer inspection, we don't actually need to iterate through all FDs. The BEAM already ensures that no unrelated file descriptors survive the exec by using FD_CLOEXEC on all internally managed descriptors.

See: https://github.com/erlang/otp/blob/master/erts/emulator/sys/unix/erl_child_setup.c#L310-L315

#55 addresses the issue differently. Closing the PR.

@akash-akya akash-akya closed this Jan 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants