Visual Studio sends 'shutdown' and 'exit' requests to Langauge Server on closing a Solution

Sajith 66 Reputation points
2025-12-18T14:51:24.11+00:00

I am working on implementing a Language Server (LSP) as part of a Visual Studio Extension. We have a requirement of loading the Language Server when extension is loaded and keeping the Language Server loaded throughout the Visual Studio session till the package is disposed.

Language Server is loaded in Package Initialize following this - How to load Language Server (LSP) when loading the extension package

Connection to Language Server is using Standard input/output streams.

But Visual Studio sends 'shutdown' followed by 'exit' messages to Language Server when a solution is closed in Visual Studio. Even when no editor/file of Language Server's Content Type is opened, Visual Studio still sends 'shutdown' and 'exit' messages when a solution is closed. It seems Visual Studio also closes the standard input/output streams of the Language Server process after sending ‘shutdown’ and ‘exit’ messages.

Is there a way to prevent Visual Studio exiting Language Server on closing a Solution? Are there any events other than closing a Solution on which Visual Studio exit a Language Server? How to have a Language Server that is loaded in Package Initialize to keep running till the Package is disposed?

Developer technologies | Visual Studio | Extensions
{count} votes

1 answer

Sort by: Most helpful
  1. Adiba Khan 1,600 Reputation points Microsoft External Staff
    2025-12-19T11:06:11.1666667+00:00

    Thanks for raising this scenario. based on the behavior described what you are seeing is expected and by design in Visual Studio when hosting a language server over standard OK i/o.

    Why Visual Studio sends shut down and exit on solution close

    Visual Studio manages language server instances per solution context, not per extension package lifetime.

    When the solution is closed, Visual Studio:

    • sends the LSP shutdown request
    • sends the LSP exit notification
    • Close the STDIN/STDOUT Streams of the language server process

    this occurs even if no documents of that content type are open because the solution boundary defines the lifetime of language services.

    There is no support mechanism to prevent Visual Studio from sending shutdown and exit when a solution is closed.\

    can the language server stay alive until packaged disposal?

    No. Visual Studio does not support keeping an LSP process running beyond the solution lifecycle.

    Key points:

    • Package.InitializeAsync() It's not equals to language server lifetime
    • LSP lifetime is controlled by the language service infrastructure
    • package disposal does not control LSP shutdown.

    Events that trigger language server shut down

    Visual Studio will shut down the language server when:

    • a solution is closed
    • a solution is switched
    • Visual Studio is shutting down
    • the language services are unloaded

    these are not customizable or overridable events

    recommended Microsoft supported design pattern

    make the language server stateless or restartable

    your language server must expect shutdown and restart

    best practices:

    • persist state externally
    • cleanly handle shut down and exit
    • restart the server automatically when a new solution loads

    Start the language server on demand

    instead of starting the LSP in Package.InitializeAsync():

    • start the server when:
      • a document of your content type is open
      • a workspace is initialized
    • let Visual Studio manage shut down

    this aligns with visual studios LSP host expectations

    unsupported approaches(not recommended)

    • keeping a global background LSP process
    • ignoring shutdown/exit
    • Reusing STDIO streams after solution close
    • attempting to intercept solution close to block shutdown

    these can lead to undefined behavior and extension instability.

    References:

    Please let us know if you require any further assistance we’re happy to help. If you found this information useful, kindly mark this as "Accept Answer".

     

     


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.