vscode-coder icon indicating copy to clipboard operation
vscode-coder copied to clipboard

VS Code keeps disconnecting if the client device sleeps

Open matifali opened this issue 11 months ago • 16 comments

Hi Team! When using Cursor/VSCode desktop, whenever we close and reopen laptop even for a few seconds, we see a popup Cannot reconnect. Please reload the window. I was wondering if this is expected behavior and if you might be aware of some settings that would make VSCode reconnect more smoothly? I tried changing connect timeout and max reconnection attempts so far but didn't see a difference.

VSCode connection often disconnects even for a slight network issue or if the device sleeps. I am unsure if this is caused by how VSCode handles remote connections as coder ssh <workspace> survives device sleep.

matifali avatar Dec 30 '24 06:12 matifali

It is possible that https://github.com/coder/vscode-coder/pull/420 may have solved this issue. @bcpeinhardt said he's going to try to see if he can repro and confirm if this change fixes things.

f0ssel avatar Feb 10 '25 16:02 f0ssel

@bpmct curious why this is marked as blocked?

bcpeinhardt avatar Feb 13 '25 19:02 bcpeinhardt

Leaving as todo in Bugs Board because it doesn't seem to have a Blocked status available.

bcpeinhardt avatar Feb 13 '25 19:02 bcpeinhardt

@matifali told me we are are unable to reproduce and waiting on a user. If we are able to repro, this feel pretty important so don't mean to arbitrarily "block" anything

bpmct avatar Feb 14 '25 13:02 bpmct

After attempting and failing to reproduce, I'm moving it back to blocked (where it was in the first place) and reaching back out to the customer.

bcpeinhardt avatar Mar 03 '25 15:03 bcpeinhardt

We are also seeing this problem constantly. Usually it happens when the device goes to sleep and then wakes up and you try to continue working. This does not happen with web based VSCode at all, that can immediately reconnect.

Image

csibe17 avatar Mar 03 '25 17:03 csibe17

I confirm I have the same issue for the past weeks, still going on on 1.5.0. Have to reload the window whenever I leave my computer 5 minutes.

Mikescops avatar Mar 26 '25 07:03 Mikescops

Hi @Mikescops @csibe17 can you still reproduce the issue on the latest extension version 1.8.0?

matifali avatar May 12 '25 09:05 matifali

@matifali absolutely yes

Mikescops avatar May 12 '25 10:05 Mikescops

I can reliably reproduce the issue, and we will investigate this further.

I am seeing a line like [<unknown>][a81f0b5a][ManagementConnection] Unknown reconnection token (never seen). whenever I see the Reload Window dialogue in Remote - SSH output logs.

I am not sure yet, but this looks related to an upstream issue: https://github.com/microsoft/vscode-remote-release/issues/7497

matifali avatar May 12 '25 12:05 matifali

I can still reproduce the issue.

Image

If I hit cancel I can find these logs in Remote - SSH output:

[17:53:31.250] SSH Resolver called for "ssh-remote+coder-vscode.yourcoderdomain.com--gabor--gabor2", attempt 6, (Reconnection)
[17:53:31.252] SSH Resolver called for host: coder-vscode.yourcoderdomain.com--gabor--gabor2
[17:53:31.252] Setting up SSH remote "coder-vscode.yourcoderdomain.com--gabor--gabor2"
[17:53:31.253] Acquiring local install lock: /var/folders/tl/505vltyj0tn8bpn6dyzywjxc0000gn/T/vscode-remote-ssh-aa6aec7a-install.lock
[17:53:31.255] Looking for existing server data file at /Users/gaborcseh/Library/Application Support/Cursor/User/globalStorage/ms-vscode-remote.remote-ssh/vscode-ssh-host-aa6aec7a-7801a556824585b7f2721900066bc87c4a09b740-0.113.1-tr/data.json
[17:53:31.255] Using commit id "7801a556824585b7f2721900066bc87c4a09b740" and quality "stable" for server
[17:53:31.258] Install and start server if needed
[17:53:31.261] askpass server listening on /var/folders/tl/505vltyj0tn8bpn6dyzywjxc0000gn/T/vscode-ssh-askpass-ec152e3e5721b0960dc108bf329c95ae2d54615d.sock
[17:53:31.261] Spawning local server with {"serverId":6,"ipcHandlePath":"/var/folders/tl/505vltyj0tn8bpn6dyzywjxc0000gn/T/vscode-ssh-askpass-f6483eedef8aff168507b29f9d1f65b9851ed83a.sock","sshCommand":"ssh","sshArgs":["-v","-T","-D","61210","-o","ConnectTimeout=1800","coder-vscode.yourcoderdomain.com--gabor--gabor2","bash"],"serverDataFolderName":".cursor-server","dataFilePath":"/Users/gaborcseh/Library/Application Support/Cursor/User/globalStorage/ms-vscode-remote.remote-ssh/vscode-ssh-host-aa6aec7a-7801a556824585b7f2721900066bc87c4a09b740-0.113.1-tr/data.json"}
[17:53:31.262] Local server env: {"SSH_AUTH_SOCK":"/private/tmp/com.apple.launchd.PG3V74XFGC/Listeners","SHELL":"/bin/zsh","DISPLAY":"1","ELECTRON_RUN_AS_NODE":"1","SSH_ASKPASS":"/Users/gaborcseh/.cursor/extensions/ms-vscode-remote.remote-ssh-0.113.1/out/local-server/askpass.sh","VSCODE_SSH_ASKPASS_NODE":"/Applications/Cursor.app/Contents/Frameworks/Cursor Helper (Plugin).app/Contents/MacOS/Cursor Helper (Plugin)","VSCODE_SSH_ASKPASS_EXTRA_ARGS":"","VSCODE_SSH_ASKPASS_MAIN":"/Users/gaborcseh/.cursor/extensions/ms-vscode-remote.remote-ssh-0.113.1/out/askpass-main.js","VSCODE_SSH_ASKPASS_HANDLE":"/var/folders/tl/505vltyj0tn8bpn6dyzywjxc0000gn/T/vscode-ssh-askpass-ec152e3e5721b0960dc108bf329c95ae2d54615d.sock"}
[17:53:31.262] Spawned 80446
[17:53:31.340] > local-server-6> Running ssh connection command: ssh -v -T -D 61210 -o ConnectTimeout=1800 coder-vscode.yourcoderdomain.com--gabor--gabor2 bash
[17:53:31.341] > local-server-6> Spawned ssh, pid=80449
[17:53:31.344] stderr> OpenSSH_9.6p1, LibreSSL 3.3.6
[17:53:31.359] stderr> Encountered an error running "coder ssh", see "coder ssh --help" for more information
[17:53:31.359] stderr> error: Get "https://yourcoderdomain.com/api/v2/users/gabor/workspace/gabor2?include_deleted=false": dial tcp: lookup yourcoderdomain.com: no such host
[17:53:31.359] stderr> kex_exchange_identification: Connection closed by remote host
[17:53:31.359] stderr> Connection closed by UNKNOWN port 65535
[17:53:31.360] > local-server-6> ssh child died, shutting down
[17:53:31.363] Local server exit: 0
[17:53:31.363] Received install output: local-server-6> Running ssh connection command: ssh -v -T -D 61210 -o ConnectTimeout=1800 coder-vscode.yourcoderdomain.com--gabor--gabor2 bash
local-server-6> Spawned ssh, pid=80449
OpenSSH_9.6p1, LibreSSL 3.3.6
Encountered an error running "coder ssh", see "coder ssh --help" for more information
error: Get "https://yourcoderdomain.com/api/v2/users/gabor/workspace/gabor2?include_deleted=false": dial tcp: lookup yourcoderdomain.com: no such host
kex_exchange_identification: Connection closed by remote host
Connection closed by UNKNOWN port 65535
local-server-6> ssh child died, shutting down

[17:53:31.363] Failed to parse remote port from server output
[17:53:31.369] Resolver error: Error: 
	at m.Create (/Users/gaborcseh/.cursor/extensions/ms-vscode-remote.remote-ssh-0.113.1/out/extension.js:2:501230)
	at t.handleInstallOutput (/Users/gaborcseh/.cursor/extensions/ms-vscode-remote.remote-ssh-0.113.1/out/extension.js:2:498552)
	at e (/Users/gaborcseh/.cursor/extensions/ms-vscode-remote.remote-ssh-0.113.1/out/extension.js:2:560424)
	at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
	at async /Users/gaborcseh/.cursor/extensions/ms-vscode-remote.remote-ssh-0.113.1/out/extension.js:2:582481
	at async t.withShowDetailsEvent (/Users/gaborcseh/.cursor/extensions/ms-vscode-remote.remote-ssh-0.113.1/out/extension.js:2:586100)
	at async /Users/gaborcseh/.cursor/extensions/ms-vscode-remote.remote-ssh-0.113.1/out/extension.js:2:557129
	at async T (/Users/gaborcseh/.cursor/extensions/ms-vscode-remote.remote-ssh-0.113.1/out/extension.js:2:555180)
	at async t.resolveWithLocalServer (/Users/gaborcseh/.cursor/extensions/ms-vscode-remote.remote-ssh-0.113.1/out/extension.js:2:556669)
	at async k (/Users/gaborcseh/.cursor/extensions/ms-vscode-remote.remote-ssh-0.113.1/out/extension.js:2:579620)
	at async t.resolve (/Users/gaborcseh/.cursor/extensions/ms-vscode-remote.remote-ssh-0.113.1/out/extension.js:2:583511)
	at async /Users/gaborcseh/.cursor/extensions/ms-vscode-remote.remote-ssh-0.113.1/out/extension.js:2:851172
[17:53:31.370] ------

I hope it helps.

csibe17 avatar May 12 '25 16:05 csibe17

This has been a known issue with VSCode for a few years: https://github.com/microsoft/vscode-remote-release/issues/5755

Using something like https://eternalterminal.dev/ to preserve the TCP connection between connection changes appears to resolve the issue.

ethanndickson avatar Jun 03 '25 07:06 ethanndickson

Adding some context from some testing:

If I unplug my ethernet cable from my MacBook, it takes consistently around 18s to popup the "Cannot reconnect. Please reload the window."

If I close the lid of my MacBook whilst not plugged in to power, and wait roughly 15s, and then open it again, it will popup with "Cannot reconnect. Please reload the window."

I end up getting either one of two errors occur:

  • stderr> error: Get "https://<coder deployment>": dial tcp: lookup <coder deployment>: no such host

  • Unknown reconnection token (never seen). after successfully connecting to the host.

DanielleMaywood avatar Jun 04 '25 15:06 DanielleMaywood

@ethanndickson could you say a little more on how to use ET to prevent this situation?

sachit-menon avatar Jun 17 '25 15:06 sachit-menon

@ethanndickson could you say a little more on how to use ET to prevent this situation?

We're also tracking how we at Coder can improve this experience in https://github.com/coder/coder/issues/18101, including a PoC for Eternal Terminal

ethanndickson avatar Jun 18 '25 07:06 ethanndickson

VS Code Server (server-main.js) has 2 kinds of connections:

  1. ManagementConnection
  2. ExtensionHostConnection

They are both websocket connections.

2025-07-03 11:18:49.872 [info] Extension host agent started.
2025-07-03 11:18:50.301 [info] [<unknown>][c21ec7a5][ExtensionHostConnection] New connection established.
2025-07-03 11:18:50.303 [info] [<unknown>][2987964b][ManagementConnection] New connection established.
2025-07-03 11:18:50.361 [info] [<unknown>][c21ec7a5][ExtensionHostConnection] <3602> Launched Extension Host Process.
2025-07-03 11:18:50.690 [info] ComputeTargetPlatform: linux-x64
2025-07-03 11:18:53.383 [info] ComputeTargetPlatform: linux-x64
2025-07-03 11:21:03.419 [info] [<unknown>][2987964b][ManagementConnection] The client has disconnected, will wait for reconnection 3h before disposing...
2025-07-03 11:23:49.873 [info] New EH opened, aborting shutdown
2025-07-03 11:35:33.283 [info] [<unknown>][2987964b][ManagementConnection] Another client has connected, will shorten the wait for reconnection 5m before disposing...
2025-07-03 11:35:33.286 [info] [<unknown>][c21ec7a5][ExtensionHostConnection] The client has reconnected.
2025-07-03 11:35:33.287 [info] [<unknown>][2987964b][ManagementConnection] The client has reconnected.
2025-07-03 11:35:33.289 [error] Error: Unexpected SIGPIPE
    at process.<anonymous> (file:///home/coder/.vscode-server/cli/servers/Stable-dfaf44141ea9deb3b4096f7cd6d24e00c147a4b1/server/out/server-main.js:191:1060)
    at process.emit (node:events:530:35)
2025-07-03 12:11:11.741 [info] [<unknown>][2987964b][ManagementConnection] The client has disconnected, will wait for reconnection 3h before disposing...
2025-07-03 12:28:38.699 [info] [<unknown>][2987964b][ManagementConnection] Another client has connected, will shorten the wait for reconnection 5m before disposing...
2025-07-03 12:28:38.700 [info] [<unknown>][2987964b][ManagementConnection] The client has reconnected.
2025-07-03 12:29:29.459 [info] [<unknown>][2987964b][ManagementConnection] The client has disconnected, will wait for reconnection 3h before disposing...
2025-07-03 12:33:40.804 [info] [<unknown>][c21ec7a5][ExtensionHostConnection] <3602> Extension Host Process exited with code: 0, signal: null.
2025-07-03 12:33:40.805 [info] Last EH closed, waiting before shutting down
2025-07-03 12:38:40.807 [info] Last EH closed, shutting down

When we get either one, the VS Code Server sets a 5 minute reconnection timeout on all connections of both types. If you reconnect, it cancels the timeout.

It appears that macOS briefly “sleepwalks” and allows VS Code Desktop to reconnect to the workspace (12:28:38 in the above log). Sometimes it doesn’t manage to reconnect to the ExtensionHostConnection, and the 5 minute timer starts. If you don’t wake your laptop within 5 minutes, the ExtensionHostConnection is closed (12:33:40 above), and you will have to “Reload Window” when you wake. When all ExtensionHostConnections are closed, then the VS Code Server shuts down.

I believe this mechanism is the source of the Unknown reconnection token (never seen) errors that we see after a relatively short sleep.

Even if you don’t trigger this timeout, there is a 3h “long” disconnection timeout.

spikecurtis avatar Jul 07 '25 11:07 spikecurtis

I am running into the same issue consistently.

sparyani-boop avatar Nov 25 '25 22:11 sparyani-boop

@ibetitsmike, can we share our latest findings here?

matifali avatar Nov 26 '25 05:11 matifali