Open jmcphers opened 1 week ago
The right fix here may require work in Ark to better manage LSP state (e.g. to ensure only one session is active at a time, and gracefully shut down an existing LSP session with its event loops when a new one is requested)
Unfortunately the only way to do that is to send a Shutdown request followed by an Exit notification from the client (also depends on https://github.com/ebkalderon/tower-lsp/issues/399 being fixed, we can use tower-lsp = { git = "https://github.com/lionel-/tower-lsp", branch = "bugfix/patches" }
until then).
The correct fix from Ark's viewpoint would be to refuse to start an LSP until the existing one has been disposed of, but we can't do that while the frontend creates multiple LSP sessions (only because of those zombie extension hosts IIUC?).
So let's just add the guardrails around global state that your proposed in the linked PR.
So let's just add the guardrails around global state that your proposed in the linked PR.
But then it's possible for older LSPs to send log messages and notifications (publish diagnostics) via the currently active LSP, which could be quite confusing.
So I guess we should make that offending global state local. It was mostly made global for the convenience of using free functions/macros for log messages.
Other solutions:
If we move the LSP out of the kernel and into its own process this will all be much simpler. The lifecycle of the LSPs will be fully managed by VSCode and we can guarantee there's only one session per process.
The other way would be to replace tower-lsp by https://github.com/rust-lang/rust-analyzer/tree/master/lib/lsp-server because then we'd be in full control of the LSP event loop and we would be able to forcibly shut it down from the server, making sure there's only one session alive at a time.
No repro yet, but multiple folks have observed R crashes in the LSP main loop, especially on Workbench. Here's what the UI looks like when this happens:
And here's a stack trace:
A Slack thread in which @Trevor-Reid mentioned this crash: https://positpbc.slack.com/archives/C07V12KUZLG/p1731000667301089
There was a proposed fix for this in https://github.com/posit-dev/ark/pull/617 (though it was ultimately not committed), and some discussion there about the problem. The right fix here may require work in Ark to better manage LSP state (e.g. to ensure only one session is active at a time, and gracefully shut down an existing LSP session with its event loops when a new one is requested)
Related to https://github.com/posit-dev/ark/issues/622.