Skip to content

VS Code is killing our language client process before we can shutdown cleanly #35196

Closed

Description

  • VSCode Version:1.16.1
  • OS Version: Windows 10 (I think others too)

Steps to Reproduce:

  1. Install the C++ extension.
  2. Open some folder with some C++ files (it might have be non-trivial).
  3. Debug our Microsoft.VSCode.Extension.exe and put a breakpoint in the Microsoft.VSCode.CPP.Extension.exe!message_handler::exit(void) or shutdown method.
  4. Close VS Code.

Bug: VS Code is somehow killing the Microsoft.VSCode.Extension.exe process before we can finish our required exiting steps, which causes our database to not be safety shutdown and we end up having to rebuild it unnecessarily. The process is created via the LanguageClient interface. We've been having this problem for as long as we can remember. The bug doesn't repro with our test framework that launches the Microsoft.VSCode.Extension.exe process without going through VS Code. I assume the issue is reproduceable using any language server protocol process. If I put the breakpoint earlier in the shutdown process, it will hit the breakpoint, but as soon as I step, the process exits prematurely (i.e. we never get an exit message).

By adding sleeps in our shutdown method, I see that with as little as 150 ms of sleeping is allowed before the process is killed before calling exit, although we get up to 1 second in shutdown itself -- so as a workaround for this issue, I moved our exit code to shutdown and that appears to have fixed our issue (although I need to do more testing). In other words, we can't rely on exit to be called. But it's hard to debug our shutdown code when VS Code keeps killing the process, so if you could disable that with some setting it would be helpful (I ended up just relying on logging).

Reproduces without extensions: Yes

Activity

  1. added
    debug
    on Sep 27, 2017
  2. removed
    debug
    on Sep 27, 2017
  3. dbaeumer commented on Sep 28, 2017

    @dbaeumer
    Member

    Sean McManus (@sean-mcmanus) the LSP is spec here as follows:

    • shutdown is a request / response pair were the client waits to receive a response
    • exit is a notification which asks the server to exit. Since notifications have no responses the client can't wait for this. From an OS prespective exit can't be a request / response since it asks the server process to exit hence the client might never receive the response if not all buffers are flushed correctly on exit.

    Given this all work that a client needs to wait for must happen in shutdown not in exit. This is the view of the protocol.

    Now in VS Code we give extensions a limited timeslot to shutdown. I think the value is in the hundreds of ms. So if our server takes longer than this in the shutdown our recommendation is as follows:

    • start the server in orphaned mode. This means that the OS (here only Windows) will not kill the server process if its parent process exits.
    • However in this mode the server needs to check for the parent process being alive periodically. VS Code could die without sending shutdown / exit to the server.

    Now that I talk about this there is one little thing I need to change in the LSP client library. Letting the client know that the server is running in orphaned mode. There is some code that kills the server if it doesn't disappear after 5 seconds after the request is sent.

  4. added
    *question
    on Sep 28, 2017
  5. dbaeumer commented on Sep 28, 2017

    @dbaeumer
    Member

    Opened microsoft/vscode-languageserver-node#256.

    However this I think is not hitting the C++ extension since the extension host currently doesn't survive that long.

  6. dbaeumer commented on Sep 28, 2017

    @dbaeumer
    Member

    Created #35306 for the 5 seconds in shutdown and to widen that in debug mode.

  7. bobbrow commented on Sep 28, 2017

    @bobbrow

    Thank you for the explanation. We try to shutdown as quickly as possible, but are not always able to abort our parsing within a few hundred milliseconds, and having VS Code terminate our process prematurely results in a corrupted database that we have to reconstruct the next time the user opens VS Code (which is a very expensive operation).

    Windows gives UWP apps 5 seconds to shut down which I think is quite reasonable. Can we ask for a slightly longer grace period? In most cases we should be able to satisfy the current short requirement, but if our extension just started parsing a large file at the time the shutdown is received, we don't have a lot of good options for inserting a bunch of abort code since we license some of the code we use in the extension from a 3rd party.

  8. sean-mcmanus commented on Sep 29, 2017

    @sean-mcmanus
    ContributorAuthor

    Dirk Bäumer (@dbaeumer) The C++ extension does survive longer if parsing a large enough file. With our pending update, we made it less likely to occur, but for large enough files, our parser isn't able to get to an abortable spot fast enough (at least not without additional modifications). We don't need VS Code to stick around, since we don't need to communicate with it, but we just need it not to kill our process.

  9. dbaeumer commented on Sep 29, 2017

    @dbaeumer
    Member

    Bob Brown (@bobbrow) actually we do wait 5 seconds as UWP does. However since we can't control CPU and in the extension host everything is single threaded we advice extensions to shutdown in hundred of ms since we can't guarantee a total of 5 seconds for each extensions.

    As said if you want to make sure VS Code is not killing your server in any way then start the server in detached mode (e.g see: https://nodejs.org/dist/latest-v8.x/docs/api/child_process.html#child_process_child_process_spawn_command_args_options and https://github.com/Microsoft/vscode-languageserver-node/blob/master\client\src\main.ts#L34)

  10. sean-mcmanus commented on Oct 3, 2017

    @sean-mcmanus
    ContributorAuthor

    Dirk Bäumer (@dbaeumer) We have fixed the issue on our side via aborting our code during the long parsing operations. Our process gets killed about 1 second after VS Code is closed, so I don't understand how it could be waiting 5 seconds (I don't have any other extensions installed). We'll look into the detached mode if we still see problems.

  11. dbaeumer commented on Oct 4, 2017

    @dbaeumer
    Member

    Just to double check: you do this in shutdown and not in exit. As said we only wait for shutdown (e.g. for the promise to be resolved) and we wait in total 5 seconds. There is not waiting happening on exit.

    How do you trigger the shutdown. To make this work correctly you need to call client.stop() in the extensions deactivate function and return a promise which is either the promise returned from client.stop() or a promise that waits for client.stop for resolve.

  12. bobbrow commented on Oct 4, 2017

    @bobbrow

    We put the vscode.LanguageClient object into the vscode.ExtensionContext.subscriptions array so that dispose will be called on it when the extension is deactivated. Does dispose not shutdown the language client for us?

    We are currently receiving a "shutdown" message from vscode, so something seems to be working. Are you saying we need to call LanguageClient.stop in addition to having dispose called? LanguageClient.stop does not appear to return anything.

  13. dbaeumer commented on Oct 5, 2017

    @dbaeumer
    Member

    The disposable assumes that everything can be executed sync which is not the case in the LSP. We later added async deactivate to VS Code's API which needs to be used for the LSP client. I might want to consider removing the return type form start in a version that allows breakage to make this more clear.

    So for you that means that you shouldn't push the disposable into the array but implement a deactivate method for the extension and return the promise that is returned from the stop method. Then VS Code will wait max 5 seconds for your server to shutdown.

  14. sean-mcmanus commented on Oct 25, 2017

    @sean-mcmanus
    ContributorAuthor

    Dirk Bäumer (@dbaeumer) Thanks for you help. We've figured out how to use the newer APIs to enable a 5 second max shutdown (although in most scenarios we should still be < 1 second).

  15. locked and limited conversation to collaborators on Nov 17, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

*question

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions