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:
- https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#shutdown
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".