coder/code-server

Orphaned process after closing the code-server browser tab

Opened this issue · 10 comments

Is there an existing issue for this?

  • I have searched the existing issues

OS/Web Information

  • Web Browser: edge
  • Local OS: windows
  • Remote OS: linux
  • Remote Architecture: amd64
  • code-server --version: 4.100.0

Steps to Reproduce

  1. A standard scala project with metals and bloop
  2. kick off a run through vscode launch configs
  3. close the browser tab

Expected

All the processes launched by that browser sessions should be killed and cleaned up after the browser closure

Actual

The process are still running in the background with unclear states, and there is no way to "reconnect" to the orphaned processes

Logs

Screenshot/Video

No response

Does this bug reproduce in native VS Code?

No, this works as expected in native VS Code

Does this bug reproduce in GitHub Codespaces?

I did not test GitHub Codespaces

Are you accessing code-server over a secure context?

  • I am using a secure context.

Notes

I believe for vscode desktop, closing the editor will close all the "tasks" launched by vscode, but that doesn't appear to be the case for code-server. This applies to all the 3rd party extensions, and it is pretty bad because it basically leaks resources.

Also for certain applications that want to make sure there is only one process running at all time, and failure to clean up the resources actually impacts the functionalities.

Alternatively, it will be great if refreshing the browser tab actually doesn't terminate the tasks, but instead, it can reconnect and resume. and if the session is stale for TTL, processes will be cleaned up

Agreed! I think this might be an upstream bug though. Are you able to reproduce in Codespaces or with code serve-web?

It might be related to microsoft/vscode#211462

Edit: actually it might not reproduce in Codespaces because I think there every page reload restarts the entire process rather than reconnects to the existing process.

This issues suggests reconnecting tasks should work, so could be a bug: microsoft/vscode#159215

I tried reproducing by running a simple Node program that loops (code-server 1.103.2). When I refresh the page it does not reconnect, but the process did die so I am not seeing any orphans.

Thanks. It looks like this could be our setup. We run code-server in a container so there is a chance that the container is messing this up. However can you confirm if the right behavior is to reconnect or clean up from the vscode documentation?

One possible difference is that I used the built-in Node extension, I plan to try Python and Scala to see if I can reproduce there.

However can you confirm if the right behavior is to reconnect or clean up from the vscode documentation

I have no idea unfortunately, this is probably better asked upstream to the VS Code folks. Based on experimentation, it looks like clean up is the behavior, yet from some issues I read it seems like it was supposed to reconnect, so I am not sure what they intend.

One theory I have is that the extension host, last I checked a long time ago, gets suddenly terminated when the browser window closes (extensions do not get a chance to clean up). That could cause orphans if the extension is not spawning processes in such a way that they get killed when the parent process gets killed.

As per Slack discussion, we will try to run the program using Python or Scala instead of the default Node.

OK confirmed that processes do get orphaned with a Python program (I did not actually use the Python extension for running, just the generic task launcher). For posterity, here is my test file:

import threading

def hey():
    threading.Timer(5.0, hey).start()
    print("hello")

hey()

The task configuration:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Run File",
            "command": "python3 ${file}",
            "type": "shell",
            "group": {
                "kind": "build",
                "isDefault": true
            },
        },
    ]
}
  1. Bring up the command palette
  2. Type "run task"
  3. Select "run file"
  4. Reload the page
  5. Confirm python process is still running: ps -eF | grep python

I tried in GitHub Codespaces (although I had to use time.sleep instead, seems like there is an issue with threading in Python 3.12) and with vanilla VS Code web (the thing both code-server and Codespaces are based on; it can be ran with native VS Code using code serve-web) and they exhibit the same problem.

So it does appear to be an upstream issue rather than specific to code-server.

I found this which says they only reconnect to background tasks: microsoft/vscode#174792 (comment)

And sure enough adding "isBackground": true to the task seems to do the trick. On reload the task is reconnected and I see the output again. To me though orphaning non-background processes feels like a bug. In the issue above the user points out something similar but no reply was made; we might want to open an issue upstream.