Repository: vscode
Issue: Unable to connect to VS Code server: Error in request - ENOENT /run/user/1000/vscode-ipc-*.sock
Labels: bug,server

This issue was originally reported in microsoft/vscode-remote-release#6997, but the cause was narrowed down to a file in this repo, `src/vs/server/node/server.cli.ts`.

Summary:

I'm occasionally but regularly seeing this error when using Remote SSH and running the `code` command in the built-in terminal.

```
Unable to connect to VS Code server: Error in request.
Error: connect ENOENT /run/user/1000/vscode-ipc-5cc265b0-6bec-4e0e-99c7-8fc48bccd7c4.sock
    at PipeConnectWrap.afterConnect [as oncomplete] (node:net:1161:16) {
  errno: -2,
  code: 'ENOENT',
  syscall: 'connect',
  address: '/run/user/1000/vscode-ipc-5cc265b0-6bec-4e0e-99c7-8fc48bccd7c4.sock'
}
```

Here's a temporary fix.

```sh
VSCODE_IPC_HOOK_CLI=$( lsof | grep $UID/vscode-ipc | awk '{print $(NF-1)}' | head -n 1 )
```

It searches for the first available VSCode IPC socket, gets its second to last column for the path to `vscode-ipc-*.sock`, and updates the relevant env variable.

After running the above line, the error no longer occurs.

---

It means the error is caused by that environment variable having a "stale" value, pointing at an old socket process which no longer exists.

https://github.com/microsoft/vscode/blob/f8ae10c8d05cf92f55c19b4937f0a5c5c0498778/src/vs/server/node/server.cli.ts#L372-L376

The env variable is passed to the `http` module for the option `socketPath`. When it's an invalid value, `http.request` throws, "Error in request - ENOENT".

---

The permanent solution seems to be: to check if `VSCODE_IPC_HOOK_CLI` is pointing to a living socket process, and if needed, refresh the value with the path to an available one.

---

- VSCode Version: 1.69.2
- Local OS Version: Darwin x64 18.2.0
- Remote OS Version: Ubuntu 22.04 LTS
- Remote Extension/Connection Type: SSH

Steps to Reproduce:

The error appears occasionally, maybe after host reboots or when Remote SSH reconnects.

1. Open new window with Remote SSH
2. Open terminal with CTRL/CMD+J
3. Run `code some-file.txt`

<!-- ⚠️⚠️ 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?: Yes/No -->
<!-- 🪓 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. -->