microsoft / vscode-languageserver-node

Language server protocol implementation for VSCode. This allows implementing language services in JS/TS running on node.js
MIT License
1.45k stars 325 forks source link

VSCode restarts language server during exit #597

Closed meakbiyik closed 4 years ago

meakbiyik commented 4 years ago

I have a language server/client implementation that works quite well with vscode. Problem is, when vscode sends the exit request from client to server, my server shuts down too fast. As a result vscode thinks that server has failed, restarts the server and then exits, thus creating a rogue server process.

I can trace this easily in the logs of my server, as it receives the exit notification, shuts down the event loop, and then a new server starts without any further requests. I can prevent this by adding a 5-second delay to the shutdown process, but I believe this is a bug stemming from the asynchronous behavior of the client and program shutdowns.

I am not sure what kind of logs should I provide to support resolving this issue, so just ask for it.

Does this issue occur when all extensions are disabled?: Yes

dbaeumer commented 4 years ago

@meakbiyik can you provide your extension and server so that I could reproduce this?

meakbiyik commented 4 years ago

Well, I am not allowed to give you the source code, but I went and prepared a minimal example for you.

minimal_example.zip

npm install and tsc -p ./ will be necessary, and the server is implemented in python (I only imported one non-default package and put it next to the code so there isn't any need for an environment).

Logs will be created in the src directory, but you can also see the hanging python process anyways. Log file will be overwritten just before the exit as server is restarted (an initialized request arrives and the features are registered). Behavior can be corrected by adding a 2 seconds delay in the start_tcp() method of .pygls.server module, just before shutdown() is called.

dbaeumer commented 4 years ago

@meakbiyik thanks for the test case. Will definitely help.

dbaeumer commented 4 years ago

@meakbiyik thanks for the example. I was not able to reproduce this (might highly depend on OS, machine, ...). I looked through the code and I couldn't easily spot a place where the shutdown could go wrong. When this happens for you do you see something like this in the server's output channel

Connection to server got closed. Server will restart.

Could you set a breakpoint here to see if you hit the connection close method? https://github.com/microsoft/vscode-languageserver-node/blob/master/client/src/client.ts#L3221

meakbiyik commented 4 years ago

Hi @dbaeumer, to be honest I also checked out the code a lot back then, and there is simply no issue with the minimal example or anything it touches. I think the reason why you couldn't reproduce it was because you were running the server on debug mode: debug mode (I believe) handles the exit timings much more synchronously, thus resolving the issue. You can try it again by putting the extension into the .vscode/extensions directory, opening an empty .txt file, closing vscode and opening the same file again. Following these steps, I see this:

The JSON Language Server server crashed 5 times in the last 3 minutes. The server will not be restarted.

And actually client continues working since there is already a server in the correct port, just not the one that client has initiated. If this again does not work, then maybe it is just a specific error with how these stuff are handled in Windows...

P.S. I was not able to put that breakpoint you mention since I do not import vscode-languageserver-node (and to be honest I am not completely sure why the issue is moved here, I do not have that much understanding on the vscode backend...). Maybe you can debug this better by putting the minimal example as said before in .vscode/extensions, open vscode WITHOUT any files ( i.e. just a bare window so that the extension is not immediately triggered) and run only the client in Debug mode? Just an idea of course.

dbaeumer commented 4 years ago

@meakbiyik the breakpoint I ask you to set is the ine vscode LSP client code. I agree it is a little confusing that it is part of the vscode-languageserver-node repository. When I was debugging it I steps through the code on the client side to see if the restart is ignored when the client is in shutdown mode which is the case for me. This is by the way timing independent since when a client stops a server ther first thing that is set is that the client is in Stopping mode which further on avoid any restart.

Regarding:

The JSON Language Server server crashed 5 times in the last 3 minutes. The server will not be restarted.

Why do you think this is related to your server? The JSON language server is handled by a different extension and therefore by a different LSP client.

meakbiyik commented 4 years ago

@dbaeumer oh interesting, then the problem can be sourced from something else that I have no idea of to be honest.

For the JSON Language Server thing, I just named that minimal example client like that, sorry for the confusion. You can change the name in the extension.ts file, under src/client/src.

dbaeumer commented 4 years ago

Thanks for the clarification around the JSON Language Server

vscodebot[bot] commented 4 years ago

This issue has been closed automatically because it needs more information and has not had recent activity. See also our issue reporting guidelines.

Happy Coding!

dbaeumer commented 4 years ago

@meakbiyik where you able to debug this?