Repository navigation
test: accept the RequestCancelled answer of a stopped language server as the cancel of its request - #2104
Merged
lagergren merged 1 commit intoNov 23, 2026
Conversation
… as the cancel of its request testARequestToAServerWhichStopsBeforeTheRequestIsKeptForTheStopIsCancelled checked that cancelOnStop leaves the workspace/symbol request of a stopped language server cancelled. LSP4J cancels a request by sending $/cancelRequest first and cancelling its future after. The language server, through LSP4J, answers $/cancelRequest with a RequestCancelled error, and when the client reads that answer in between, the request ends with a ResponseErrorException and isCancelled() is false. It happened once on CI, and in 2 of 400 runs under CPU load. LSP4IJ takes a RequestCancelled error as a cancel: CancellationSupport gives no result for it and shows no error, so the search ends with the symbols of the other language servers either way. The test now accepts either ending, and still fails for a request that is kept for a later stop.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2104 +/- ##
==========================================
+ Coverage 50.38% 50.41% +0.02%
==========================================
Files 816 816
Lines 33816 33816
Branches 6961 6961
==========================================
+ Hits 17038 17047 +9
+ Misses 14127 14125 -2
+ Partials 2651 2644 -7 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This was referenced Nov 22, 2026
Collaborator
Author
|
@coderabbitai review |
Action performed
|
lagergren
added a commit
that referenced
this pull request
Nov 23, 2026
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
ServerStopPendingWorkspaceSymbolTest.testARequestToAServerWhichStopsBeforeTheRequestIsKeptForTheStopIsCancelledfails now and then: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/symbolto a language server that never answers, stops the server, and callscancelOnStop, which cancels the request because the server is no longer the current one. It then checkedrequest.isCancelled().LSP4J's
JsonRpcRequestFuture.cancelsends$/cancelRequestfirst, and cancels the future after:$/cancelRequest, it cancels the server's future and answers the request with a RequestCancelled error (-32800);ResponseErrorException;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):LSP4IJ itself handles both endings.
CancellationSupport.executetakes a RequestCancelled error as it takes aCancellationException: 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, asCancellationSupportdoes. 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
$/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.ServerStopPendingWorkspaceSymbolTest, the other workspace/symbol tests and the stop tests (11 classes, 42 tests) pass on 2024.2 and 2025.3.Found, not changed
The traces show the stop writing
$/cancelRequestaftershutdown. LSP allows onlyexitaftershutdown.cancelOnStop, as here, and it can happen forcancelPendingRequests()in thefinallyofstop(), which runs after the shutdown has been submitted.$/cancelRequestgoes through LSP4J'sRemoteEndpoint, not LSP4IJ's notification dispatcher.$/cancelRequest.It has no effect that users see, and it doesn't cause this failure, so it would be a PR of its own.