Skip to content

Catching unhandled exceptions when starting language server by LanguageClient #1307

Open

Description

When starting a language server (in my case python), which fails, there are a few exceptions that cannot be caught:

Error: Pending response rejected since connection got disposed
at Object.dispose (...\dist\extension.js:8861:27)
at Object.dispose (...\dist\extension.js:5418:35)
at LanguageClient.handleConnectionClosed (...\dist\extension.js:5009:34)
at LanguageClient.handleConnectionClosed (...\dist\extension.js:3447:22)
at closeHandler (...\dist\extension.js:4996:18)
at CallbackList.invoke (...\dist\extension.js:6843:39)
at Emitter.fire (...\dist\extension.js:6905:36)
at closeHandler (...\dist\extension.js:8008:26)
at CallbackList.invoke (...\dist\extension.js:6843:39)
at Emitter.fire (...\dist\extension.js:6905:36)
at StreamMessageWriter.fireClose (...\dist\extension.js:7455:27)
at Socket.<anonymous> (...\dist\extension.js:7487:42)
at Socket.emit (node:events:513:28)
at Pipe.<anonymous> (node:net:757:14)
Error: Client is not running and can't be stopped. It's current state is: starting
at LanguageClient.shutdown (...\dist\extension.js:4818:19)
at LanguageClient.stop (...\dist\extension.js:4789:21)
at LanguageClient.stop (...\dist\extension.js:3417:22)
at LanguageClient.doInitialize (...\dist\extension.js:4771:27)
at async LanguageClient.start (...\dist\extension.js:4626:13)
at async restartServer (...\dist\extension.js:440:9)
at async ...\dist\extension.js:50:20

The problem is that VSCode for all unhandled errors creates an "unhandledError" telemetry event. For correct analysis of telemetry data, you must manually create an event that indicates a specific problem. In this case, for example, "serverStartFailed".

Yes, I can set initializationFailedHandler in the LanguageClientOptions and create the necessary telemetry event in it. But VSCode will still create a duplicate "unhandledError" event, because the error is not caught.

Another bewilderment is that the server's stderr output can only be received in the outputChannel. Why not pass it as an argument to initializationFailedHandler? Or create a corresponding attribute in the LanguageClient class? To understand what happened to the user, I need to send all the information from the outputChannel to the telemetry server, including unnecessary information.

It also looks strange to automatically try to stop a server that is not running, which leads to another error. And it also automatically sent to telemetry...

Thanks!

Activity

  1. dbaeumer commented on Sep 10, 2023

    @dbaeumer
    Member

    I am open for improvements here. PR welcome.

  2. added
    help wanted
    on Sep 10, 2023
  3. added
    feature-request
    and removed on Nov 17, 2023
  4. lagergren commented on Oct 18, 2026

    @lagergren
    Contributor

    This still happens on main (8ab2603e). When initialization fails, the catch in BaseLanguageClient.initialize calls void this.stop(): when initializationFailedHandler returns false, when the user declines a retry, and in the default case. At that point the client is still in ClientState.Starting, so shutdown throws Client is not running and can't be stopped. It's current state is: starting, and the void leaves that rejection unhandled.

    The only way an extension can avoid it today is to override stop() and catch every rejection, which also hides legitimate stop failures. #1867 fixed the related loss of the startup promise, and its description mentions this rejection as a separate, existing problem.

    I'd like to send a PR that releases the half-started connection on initialization failure without going through stop(), so that nothing rejects unhandled, with an integration test based on the crash-on-initialize server added in #1867. Does that direction sound right to you?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature-requesthelp wanted

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions