…own request (#2151)
Fixes #2145
## What
`CancelRequestAfterShutdownTest.testARequestCancelledOnceTheShutdownIsSentSendsNoCancelRequest` failed on main's CI at 1e40358 (run 45186700307, `Test (ubuntu-latest)`, IC 2024.2):
The language server must read nothing but exit after shutdown expected:<[shutdown, exit]> but was:<[shutdown, textDocument/codeLens, exit]>
**Verdict: a bug in LSP4IJ, not only in the test.** LSP allows only the `exit` notification after `shutdown`. #2105 stopped `$/cancelRequest` from going out after `shutdown`, but any other request or notification that a feature sent then was still written. Here the request came from the code vision of the test's editor.
## Cause
The code vision of an editor gets the language servers of its file, then sends `textDocument/codeLens` from another thread (`thenComposeAsync` in `LSPCodeLensSupport`). If the language server stops between the two, the request is written after `shutdown`. The stop's disposal of the file supports doesn't prevent it: `codeLens(params)` is sent before `CancellationSupport.execute` sees that its support is cancelled. Such a request then stayed pending until the language server answered it, or something cancelled it.
The test doesn't switch code vision off, so the editor's background `textDocument/codeLens` can land in that window. That happens rarely: 300 runs under CPU load on macOS passed. With two temporary probes (a 1 s delay before the codeLens request, and a 2 s delay between `shutdown` and `exit`), main fails 10 of 10 with the CI message.
## Change
The messages of a connection were already queued to the message writer under one lock, which notes when `shutdown` is queued (#2105). After that, only `exit` and the responses to the language server's own requests are queued:
- **A request** ends at once with a `CancellationException`, the same way the requests of a stopped language server end. LSP4J completes a request's future with the exception that its send throws. It isn't traced or written.
- **A notification** is dropped and isn't traced, as `$/cancelRequest` already was.
- **Nothing queued before `shutdown` changes:** the writer keeps its order, and the notifications that #2099 sends ahead of `shutdown` are queued before it.
A message is checked before it's traced and again when it's queued. If `shutdown` is queued between the two checks, a message other than `$/cancelRequest` is traced but not written. Requests are still traced before they're queued, so that a request's trace stays ahead of its answer's.
## Behaviour change
A request sent to a language server after its `shutdown` request is now cancelled at once without being sent, and no notification other than `exit` is sent after `shutdown`. Before, the request was written, and it stayed pending until the language server answered it or something cancelled it. A notification was written too.
Callers see the same outcome as for the requests of a stopped language server: `CancellationSupport` takes the `CancellationException` as no result, and `awaitWithCheckCanceled` as a `ProcessCanceledException`. Nothing new is logged. No API changes.
## Test
`CancelRequestAfterShutdownTest` gets three tests. Each holds the shutdown answer so that the connection stays open after `shutdown`:
- **A request sent after `shutdown`** is cancelled at once, and the language server reads `shutdown`, `exit`. On main it fails: the request isn't cancelled, and it's written.
- **A notification sent after `shutdown`** isn't sent. On main it fails with `but was:<[shutdown, workspace/didChangeConfiguration, exit]>`.
- **The answer to a `workspace/configuration` request** that the language server sends after `shutdown` is still sent. This one passes on main and with the change.
Other runs:
- **#2105's tests** are unchanged and pass.
- **With the probes:** main fails 10 of 10 with `[shutdown, textDocument/codeLens, exit]`. With the change, 10 of 10 pass, and each run still sends its delayed codeLens request.
- **Under CPU load** (16 busy processes on 10 cores, without probes): the flaky test passes 300 of 300 runs, and the whole class 40 of 40 (320 tests), all in one JVM. Main passed 300 of 300 there too, so a loop alone doesn't reproduce the race on macOS.
- **Related classes** (`CancelRequestAfterShutdownTest`, `ServerStopPendingRequestsTest`, `ServerStopPendingWorkspaceSymbolTest`, `LanguageServerWrapperDisposeWhileStartingTest`, `LanguageServerWrapperRestartTest`, `SelectedEditorConnectionTest`, `LanguageServerInstancesTest`, `LanguageServerManagerInstancesTest`, `KeepAliveDuringPendingStopTest`, `EditorRefreshBatchTest`, `ServerStartFailureTest`, `SocketLanguageServerTest`, `LanguageServerDisposeFailureTest`, `RejectedLanguageServerTest`): 193 tests, 0 failures, 0 skipped on each of IC 2024.2, 2025.3 and IU 2026.3.
- **Full suite:** 2067 tests, 0 failures, 7 skipped (Windows-only and disabled tests), on IC 2024.2.
What
A stopping language server was often sent
$/cancelRequestafter itsshutdownrequest, which LSP forbids: only theexitnotification may followshutdown. Onmain, the new test sees the language server readshutdown,$/cancelRequest,exiton each of the four paths it covers. The investigation of the flaky test of #2104 found it.Cause
LSP4J sends
$/cancelRequestwhen a request is cancelled. A stopping language server has its requests cancelled aftershutdownin four ways:workspace/symbol, in thefinallyofstop(). That runs after it has started the shutdown on another thread, which usually sendsshutdownfirst.cancelOnStop: it cancels at once a request that comes after the stop.Change
The messages of a connection are queued to the message writer under one lock, which notes when the
shutdownrequest is queued. A$/cancelRequestqueued after it is dropped, and not traced.shutdownrequest, as while the language server runs, still sends$/cancelRequest.Behaviour changes
$/cancelRequestmessages, as they're no longer sent.shutdownnow always ends with aCancellationException. Before, the language server's RequestCancelled answer could come first. LSP4IJ takes both as no result, so nothing else changes.Test
CancelRequestAfterShutdownTestrecords 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$/cancelRequestuntil the server has readshutdown, so the order is fixed without sleeps.shutdown, a latecancelOnStop, a request kept for the stop, and a hover ended by the stop. The language server reads nothing butexitaftershutdown. Onmain, each fails 20 of 20 runs, withexpected:<[shutdown, exit]> but was:<[shutdown, $/cancelRequest, exit]>. With the change, each passes 20 of 20.$/cancelRequest, which the server reads, and it cancels its request.Related
didClose, beforeshutdown. This change keeps cancels from going after it. They cover different messages, and cancels queued during fix: send a language server the notifications queued before its stop ahead of its shutdown #2099's wait still go out beforeshutdown.shutdowncan still be answered with RequestCancelled before its future is cancelled.