Skip to content

fix: walk /proc/self/fd when close_range is not available - #1012

Merged
Dmitriy Vasyura (dmitrivMS) merged 3 commits into
microsoft:mainfrom
AdzerKI:proc-fd-cloexec
Dec 1, 2026
Merged

Dmitriy Vasyura (dmitrivMS) merged 3 commits into
microsoft:mainfrom
AdzerKI:proc-fd-cloexec

Conversation

@AdzerKI

Copy link
Copy Markdown
Contributor

Fixes #1011

Without close_range, pty_close_inherited_fds() now sets FD_CLOEXEC on every fd listed in /proc/self/fd, read with getdents64 since readdir() may allocate in the forked child. The existing loop stays for when /proc is 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 test and lint are green.

@dmitrivMS

Copy link
Copy Markdown
Contributor

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 0633029; the production source was unchanged at PR head 0d5a783.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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/fd with getdents64 to mark descriptors close-on-exec without allocating in the forked child.
  • Preserves the existing fallback loop when /proc cannot 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

Comment thread src/unixTerminal.test.ts Resolved
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

@anthonykim1 Anthony Kim (anthonykim1) left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 getdents64 batch.
  • With close_range blocked (ENOSYS), a build that has it falls through to the /proc walk. A uid/gid spawn works too, and without /proc it 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.

@dmitrivMS
Dmitriy Vasyura (dmitrivMS) merged commit 5995fac into microsoft:main Dec 1, 2026
8 checks passed
@dmitrivMS

Copy link
Copy Markdown
Contributor

Koushik M L N (@koushikmln) Thank you!

Manoj (manoj-1407) pushed a commit to manoj-1407/vscode that referenced this pull request Dec 1, 2026
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>
@rst0git

Copy link
Copy Markdown

Konstantin Adzer (@AdzerKI) Dmitriy Vasyura (@dmitrivMS) Anthony Kim (@anthonykim1) Thank you for fixing this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Linux: fds above a gap in fd numbers leak into the spawned process when close_range isn't compiled in

5 participants