Repository navigation
fix: stop waiting for the workspace/symbol answer of a language server after 10 seconds - #2085
Conversation
…r after 10 seconds Part of redhat-developer#722. A language server which runs but never answers workspace/symbol kept Search Everywhere, Go to Symbol and Go to Class waiting with no time limit: LSPWorkspaceSymbolSupport merges the answers of the language servers of the project with mergeInOneFuture, which waits for all of them. The search ended only when the user typed again or closed the popup, and without the symbols of the other language servers. The workspace/symbol request of a language server is now cancelled when it isn't answered within 10 seconds, the time a workspace/textDocumentContent request is given, which sends $/cancelRequest. A cancelled request gives no result, so the search ends with the answers of the other language servers.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2085 +/- ##
==========================================
+ Coverage 50.24% 50.31% +0.07%
==========================================
Files 816 816
Lines 33699 33714 +15
Branches 5261 5261
==========================================
+ Hits 16931 16963 +32
+ Misses 14127 14110 -17
Partials 2641 2641 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
@coderabbitai review |
Action not completed
|
|
@coderabbitai review |
Action not completed
|
|
@coderabbitai review |
Action not completed
|
|
@coderabbitai review |
Action performed
|
… as the cancel of its request (#2104) ## What `ServerStopPendingWorkspaceSymbolTest.testARequestToAServerWhichStopsBeforeTheRequestIsKeptForTheStopIsCancelled` fails now and then: junit.framework.AssertionFailedError: The request must be cancelled, as no later stop of its server cancels it It failed once on CI, on the `Test (ubuntu-latest, 2025.3)` job of a fork run of #2091, which changes neither workspace/symbol nor the stop. Under CPU load it fails in about 4 of 1000 runs. **Verdict: the test is wrong; LSP4IJ isn't.** The test expected one of two correct endings of a cancelled request. ## Cause The test sends `workspace/symbol` to a language server that never answers, stops the server, and calls `cancelOnStop`, which cancels the request because the server is no longer the current one. It then checked `request.isCancelled()`. LSP4J's `JsonRpcRequestFuture.cancel` sends `$/cancelRequest` first, and cancels the future after: - when LSP4J on the server side reads `$/cancelRequest`, it cancels the server's future and answers the request with a RequestCancelled error (-32800); - if the client reads that answer between the two steps, the answer ends the request with a `ResponseErrorException`; - the cancel then does nothing, and `isCancelled()` is false. Usually the test thread is faster, or the stop closes the connection before the answer comes back. A failing run under load, with temporary traces (ms from the call of `cancelOnStop`): +0.00 test thread calls cancelOnStop +0.69 writer writes $/cancelRequest for request 4 +2.83 listener reads the answer to 4: error -32800 "The request (id: 4, method: 'workspace/symbol') has been cancelled" +4.33 stop closes the connection +4.88 test thread returns from sending $/cancelRequest and cancels the future, too late +4.94 test thread cancelOnStop returns: cancelled=false, done=true LSP4IJ itself handles both endings. `CancellationSupport.execute` takes a RequestCancelled error as it takes a `CancellationException`: the request gives no result and shows no error. So the search ends with the symbols of the other language servers either way, which the other test of the class checks. ## Change The test accepts both endings: the request is cancelled, or it ended with the RequestCancelled answer of the server, checked with `CancellationUtil.isRequestCancelledException`, as `CancellationSupport` does. A request that isn't done still fails the test. That's the case the test is for: a request kept for a stop that has already happened. ## Test - **With a probe:** a temporary 300 ms delay in the test thread after it queues `$/cancelRequest`, with the mock server's shutdown held so the connection stays open. The server's answer then always comes first: 20 of 20 runs fail before the change, and 20 of 20 pass after. - **Under load, without the probe:** 24 busy processes on 10 cores, the test repeated 1000 times in one JVM, before and after at the same time. 4 failures before, 0 after. - **With #2085, #2095 and #2099 merged in:** the test file merges without conflicts, and the test passes 1000 of 1000 times under the same load. - **Related classes:** `ServerStopPendingWorkspaceSymbolTest`, the other workspace/symbol tests and the stop tests (11 classes, 42 tests) pass on 2024.2 and 2025.3. - **The full suite passes:** 1847 tests, 0 failures, 7 skipped (Windows-only and disabled tests). ## Found, not changed The traces show the stop writing `$/cancelRequest` after `shutdown`. LSP allows only `exit` after `shutdown`. - It happens for a late `cancelOnStop`, as here, and it can happen for `cancelPendingRequests()` in the `finally` of `stop()`, which runs after the shutdown has been submitted. - #2099 doesn't cover it: `$/cancelRequest` goes through LSP4J's `RemoteEndpoint`, not LSP4IJ's notification dispatcher. - A fix would end the requests of a stopped language server locally, without sending `$/cancelRequest`. It has no effect that users see, and it doesn't cause this failure, so it would be a PR of its own.
…tdown request (#2105) ## What A stopping language server was often sent `$/cancelRequest` after its `shutdown` request, which LSP forbids: only the `exit` notification may follow `shutdown`. On `main`, the new test sees the language server read `shutdown`, `$/cancelRequest`, `exit` on each of the four paths it covers. The investigation of the flaky test of #2104 found it. ## Cause LSP4J sends `$/cancelRequest` when a request is cancelled. A stopping language server has its requests cancelled after `shutdown` in four ways: - **The stop's own cancel:** the stop cancels the requests kept for it, as `workspace/symbol`, in the `finally` of `stop()`. That runs after it has started the shutdown on another thread, which usually sends `shutdown` first. - **A late `cancelOnStop`:** it cancels at once a request that comes after the stop. - **The file supports:** the stop disposes the supports of the files, which cancel their pending requests, as a hover, in a read action that runs later. - **A feature:** a feature cancels its request while the language server shuts down, as when the user types on. ## Change The messages of a connection are queued to the message writer under one lock, which notes when the `shutdown` request is queued. A `$/cancelRequest` queued after it is dropped, and not traced. - The request still ends cancelled: LSP4J cancels its future once it has handed over the notification. - A request cancelled before the `shutdown` request, as while the language server runs, still sends `$/cancelRequest`. ## Behaviour changes - The LSP console no longer shows those dropped `$/cancelRequest` messages, as they're no longer sent. - A request cancelled after `shutdown` now always ends with a `CancellationException`. Before, the language server's RequestCancelled answer could come first. LSP4IJ takes both as no result, so nothing else changes. ## Test `CancelRequestAfterShutdownTest` records the messages that the mock language server reads, in order, and lets the server read everything sent before the connection closes. The shutdown answer is held, and a listener holds the stop's `$/cancelRequest` until the server has read `shutdown`, so the order is fixed without sleeps. - **The four paths:** a request cancelled by a feature after `shutdown`, a late `cancelOnStop`, a request kept for the stop, and a hover ended by the stop. The language server reads nothing but `exit` after `shutdown`. On `main`, each fails 20 of 20 runs, with `expected:<[shutdown, exit]> but was:<[shutdown, $/cancelRequest, exit]>`. With the change, each passes 20 of 20. - **The control:** a request cancelled while the language server runs still sends `$/cancelRequest`, which the server reads, and it cancels its request. - **Under CPU load:** the stop, workspace/symbol, workspace diagnostics and tracing tests, repeated 10 times, pass (870 tests). - **With #2085, #2099 and #2104 merged in:** the branches merge without conflicts, and the 17 related classes pass (117 tests). - **The full suite passes:** 1853 tests, 0 failures, 7 skipped (Windows-only and disabled tests). ## Related - **#2099** orders the notifications of the dispatcher, as a `didClose`, before `shutdown`. This change keeps cancels from going after it. They cover different messages, and cancels queued during #2099's wait still go out before `shutdown`. - **#2104** is still needed: a cancel queued before `shutdown` can still be answered with RequestCancelled before its future is cancelled.
Part of #722.
What
A language server that runs but never answers
workspace/symbolkeeps Search Everywhere, Go to Symbol and Go to Class waiting with no time limit:workspace/symbolto each language server of the project, and merges their answers withCompletableFutures.mergeInOneFuture, which waits for all of them.#2049 ended the request of a language server that stops before it answers. This ends the request of one that keeps running. Turning off workspace symbols for one language server stays open in #722.
Change
workspace/symbolrequest of a language server is cancelled when the language server doesn't answer it within 10 seconds. That is the time aworkspace/textDocumentContentrequest gets, long enough for a server that searches a large workspace.$/cancelRequestto the language server. A cancelled request gives no result, so the search ends with the answers of the other language servers.$/cancelRequest, and would fail the whole search.Test
ServerStopPendingWorkspaceSymbolTest.testAServerWhichDoesNotAnswerInTimeEndsTheSearchWithTheSymbolsOfTheOtherssearches with two language servers, one of which never answersworkspace/symbol, with a time limit of 2 seconds. The search ends with the symbol of the other language server. The silent language server's request is cancelled, which shows that it got$/cancelRequest.