Repository navigation
fix: walk /proc/self/fd when close_range is not available - #1012
Conversation
|
I tested this change in VS Code Insiders 1.149.0 on Linux. With the candidate native module, the terminal child had no extra file descriptors and CRIU checkpoint/restore passed locally and twice over Remote-SSH. On the same published Insiders build without the change, SSH checkpointing passed but restore failed. Full comparison, environment, and recording on microsoft/vscode#258187. I tested the native change at |
There was a problem hiding this comment.
🟡 Changes recommended
The regression test closes all filler descriptors before the third spawn, so it does not guarantee a second getdents64 batch and could pass even if the fallback processes only the first batch.
1 open issue
What changed in this PR
Fixes #1011 by preventing inherited file descriptors above numbering gaps from leaking into Linux child processes when close_range is unavailable.
Changes:
- Enumerates
/proc/self/fdwithgetdents64to mark descriptors close-on-exec without allocating in the forked child. - Preserves the existing fallback loop when
/proccannot be used. - Updates the leak regression test to create a gap below existing pty masters.
| File | Description |
|---|---|
src/unixTerminal.test.ts |
Creates a descriptor gap before spawning the third pty. |
src/unix/pty.cc |
Adds allocation-free /proc/self/fd enumeration before the existing fallback. |
Review effort: Max
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Thanks for the fix!
The bug: without close_range, pty_close_inherited_fds() counts up from fd 3 and stops at the first unused fd above 15, so any fd numbered above that gap never gets FD_CLOEXEC and leaks into the new shell. Official Linux builds never compile in the close_range call, because the glibc 2.28 sysroot has no SYS_close_range, so they always take this path. Walking /proc/self/fd marks the fds that are actually open, so gaps no longer matter.
(Copilot verification) - Verified on Linux arm64 (Ubuntu 22.04, gcc-10, Node 24.22.1, built with the same sysroot as CI):
- With the node-pty from the stable VS Code server 1.148.0, a new terminal's shell holds another terminal's
/dev/ptmx, and closing that other terminal doesn't hang up its tty, so a process there that ignores SIGHUP keeps running. With this PR, neither happens. - The updated test fails against main's binary, and against a build that only reads the first
getdents64batch. - With
close_rangeblocked (ENOSYS), a build that has it falls through to the/procwalk. Auid/gidspawn works too, and without/procit falls back to the old loop like main.
Follow-up, not for this PR: the walk costs about 1 µs per open fd in the child before exec (the parent's spawn() time is unchanged). The sysroot's kernel headers do define __NR_close_range and CLOSE_RANGE_CLOEXEC, so official builds could take the single-syscall path on newer kernels and keep this walk for older ones.
|
Koushik M L N (@koushikmln) Thank you! |
Brings in microsoft/node-pty#1012. On Linux builds without close_range, node-pty now marks every fd listed in /proc/self/fd close-on-exec before starting the shell, so a terminal no longer inherits fds that sit above a gap in fd numbers, such as the pty masters of other terminals. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
Konstantin Adzer (@AdzerKI) Dmitriy Vasyura (@dmitrivMS) Anthony Kim (@anthonykim1) Thank you for fixing this! |

Fixes #1011
Without
close_range,pty_close_inherited_fds()now sets FD_CLOEXEC on every fd listed in/proc/self/fd, read withgetdents64sincereaddir()may allocate in the forked child. The existing loop stays for when/procis not available.The leak test now opens and closes a block of fds before spawning the third pty, so the masters of the first two sit above a gap. Built against the glibc 2.28 sysroot as CI does, it fails without the change (3 ptmx fds in the shell) and passes with it;
npm testand lint are green.