Way to increase retry threshold when sourcekit-lsp crashes?

sourcekit-lsp won’t restart if it crashes more than 5 times in the last 3 minutes. this forces me to Ctrl-Shift-P and reload the entire window to get syntax highlighting again. this happens every few minutes or so about every 30 seconds.

is there a way to increase the retry threshold, so i don’t have to keep restarting the entire editor?

2 Likes

There is no setting I know of that increases the number of attempts to start the LSP server.

Surely the more important question is why does your version of sourcekit-lsp keep crashing.

i wish i knew. recently i’ve been trying to record contexts where it was crashing and file them as issues. yesterday i filed #605.

anecdotally, i get the most crashes when:

  • using editable SPM packages
  • using code-completion
  • filling in the brackets of a subscript expression
  • modernizing pre-exclusivity (swift <= 3) code

but these are very general and probably not very helpful for fixing the crashes, which is why i have held off on reporting them until i can come up with more-reproducible test cases.

got another one: #606


and another one: #607

If you had time you could build your own version of sourcekit-lsp and use that in VS Code. There is a setting to set the sourcekit-lsp executable used. Then on macOS I used Xcode to attach to the version of sourcekit-lsp run by VSCode and then catch any crashes. I’ve done this in the past to diagnose issues.

I haven’t looked into attaching to processes using VSCode yet. I am meant to look into that this weekend though.

Actually attaching to a process is simple in VS Code. Add a launch.json config

{
    "type": "lldb",
    "request": "attach",
    "name": "Attach to Sourcekit-LSP",
    "program": "sourcekit-lsp",
    "pid": <process id>
}

To avoid attaching to the instance of sourcekit-lsp running when editing the SourceKit-LSP project you need to set the pid field to the process id for the correct version of sourcekit-lsp.

I have only tested this on macOS so far.

1 Like

i hate to come back to this, but it really is not practical to develop a swift project while having to Ctrl-Shift-P reload the window every 2 or 3 minutes.

i am really sick of seeing The SourceKit Language Server server crashed 5 times in the last 3 minutes. The server will not be restarted. See the output for more information. show up whenever i type the wrong character, especially if sourcekit-lsp would come back online if i just type the next character it expects.

after (i estimate) 5-7K crashes over the past month, i don’t think there has been a single occasion where i didn’t want to restart the server. can we just make this the default?

Hi @taylorswift ,

This is not my experience at all, but then I'm not generally working with the cutting edge of Swift. Most of my development uses Swift 5.6 and I haven't seen a crash in a long time.

I'm not sure what you want VS Code to do here. If it hits a piece of code that causes SourceKit-LSP to crash. SourceKit-LSP will keep crashing no matter how many times you send that code to it. I think I've found a way to increase the number of retries, but that isn't really going to help you. It'll just crash more times and message you slightly later.

The solution here is to ensure SourceKit-LSP does not crash. So we need to bug every issue we find and make sure they are fixed.

that makes sense, the majority of the crashes i have encountered are in proximity to 5.7 features like opaque parameter types.

usually the crash is not permanent, it is transient. for example, typing an opening parenthesis might crash sourcekit-lsp, but typing the closing parenthesis and exiting the code completion context would stop the crash. so i would really like for sourcekit-lsp to come back online automatically after i type the closing parenthesis, instead of having to manually reload the entire IDE window.

this is what i have been doing relentlessly:

but there are a lot of crashes and some of them are not vscode-swift or even sourcekit-lsp issues, they are compiler crashes that also crash sourcekit-lsp and vscode-swift. so vscode-swift needs a way to be more resilient against problems in the toolchain.

I had a look at this in more detail and there are limited things I can do when the server crashes. I can up the number of times it retries running the server and edit the message shown when it finally decides to not restart the server anymore.

Ideally I would like to show a dialog asking the user what they would like to do, also give them a chance to edit their source code before re-running the LSP server. But with the way the VSCode language client error handling is structured at the moment this isn't really possible. If you'd like to change this then please add your comments to the following issue Return Promise from `ErrorHandler.closed()` · Issue #1090 · microsoft/vscode-languageserver-node · GitHub

1 Like

it seems we are kind of stuck then. because vscode-swift is really unusable at the moment, because editing any code inside a function that has an opaque type parameter crashes the LSP server. (lib/IDE/CodeCompletionResultPrinter.cpp💯 Assertion `(TextSize > 0) && "code completion result should have non-empty description!"' failed · Issue #60966 · apple/swift · GitHub) so i have developed a reflex of just smashing Ctrl-Shift-P-Enter after typing a few characters in the editor, which is really quite silly.

is there really nothing that can be done about this?

Have you tried using production versions of Swift rather than nightly builds to see if they're more stable?

for me at least, the problem is swift-syntax, as its API has diverged substantially between the nightly builds and the release toolchains. and SwiftSyntax will refuse to work unless it is built with the exact same toolchain it is tagged for. so it is not as simple as “just switch to 5.7”.

swift-markdown has a similar issue, but for some reason swift-markdown does not enforce its toolchain requirement, so i can get away with using a mismatched toolchain with that library.