Repository: vscode
Issue: vscode leaking tty fd's
Labels: bug,upstream,macos,confirmation-pending,terminal-process

<!-- ⚠️⚠️ Do Not Delete This! bug_report_template ⚠️⚠️ -->
<!-- Please read our Rules of Conduct: https://opensource.microsoft.com/codeofconduct/ -->
<!-- 🕮 Read our guide about submitting issues: https://github.com/microsoft/vscode/wiki/Submitting-Bugs-and-Suggestions -->
<!-- 🔎 Search existing issues to avoid creating duplicates. -->
<!-- 🧪 Test using the latest Insiders build to see if your issue has already been fixed: https://code.visualstudio.com/insiders/ -->
<!-- 💡 Instead of creating your report here, use 'Report Issue' from the 'Help' menu in VS Code to pre-fill useful information. -->
<!-- 🔧 Launch with `code --disable-extensions` to check. -->
Does this issue occur when all extensions are disabled?: TBD

<!-- 🪓 If you answered No above, use 'Help: Start Extension Bisect' from Command Palette to try to identify the cause. -->
<!-- 📣 Issues caused by an extension need to be reported directly to the extension publisher. The 'Help > Report Issue' dialog can assist with this. -->
- VS Code Version: 

```
Version: 1.78.1
Commit: 6a995c4f4cc2ced6e3237749973982e751cb0bf9
Date: 2023-05-04T10:01:22.349Z (1 wk ago)
Electron: 22.5.1
Chromium: 108.0.5359.215
Node.js: 16.17.1
V8: 10.8.168.25-electron.0
OS: Darwin arm64 22.4.0
Sandboxed: No
```

- OS Version: 

macOS 13.3.1 (a) (22E772610a)

Steps to Reproduce:

I've had this happen twice now, once today and last time was on May 5th.
I try to connect to my remote session and I get this:

<img width="638" alt="image" src="https://github.com/microsoft/vscode/assets/799078/416a06d9-81c1-42b9-8912-2a6ca0eb0b29">

I try to jump into my macOS terminal, which I typically always have open anyway and run some commands, ssh works fine of course, perplexed... I wound up trying to open a new tab with a new terminal and it fails...
After much digging I find this:

```
% lsof | grep /dev/ptmx | grep Code | wc -l
     510
```

vscode is using all of my available TTY fd's.
This happened in the course of 2 days (I rebooted the machine 2 days ago).
It is worth noting right now I have 3 vscode windows open, all disconnected from the remote.
Closing all vscode windows (but with the vscode process still running as happens on macOS until you cmd+q it) does not change this number.
Actually quitting vscode gives me all my tty fd's back.

(let's assume from now on when I say fd I'm referring to /dev/ptmx fd's)

On a brand new vscode instance (no remote connection or anything), just a window open there are 0 open fd's from vscode.
Opening one terminal in vscode there are now 2 fd's opened.
After closing that terminal there is 1 fd open.
Opening a new terminal there are 3 fd's open.
Closing that terminal there are 2 fd's open now.
Open a new terminal and there are now 4 fd's (seeing a pattern here).
Opening a 2nd terminal there are 6 fd's open.
Close that 2nd terminal 5 fd's.
Connect to a remote, which automatically opens a new terminal, 8 fd's.
Close the remote terminal, still 8 fd's open.
Open a new remote terminal, still 8 fd's open (some re-use here?)
Open 3 more remote terminals, still 8 fd's open.
Go back to the original local window and open a remote project, 10 fd's open. (seems remote's share the same local fd's between terminals in one window)
Close that window, create a new window and open the same remote project, 13 fd's open.
Closing all open windows (but keeping vscode running) still 13 fd's open
Open a new window connected to the same remote+project, 16 fd's open.

Based on this pattern it looks like:

- Local terminals leak 1 fd per terminal after closing
- Remote terminals share the same 2 local fd's for all terminals in one window
- New windows connected to the same remote get new fd's
- fd's attached to remote terminals are never closed until vscode as a whole is closed.