Skip to content

fix: send a stopping language server nothing but exit after its shutdown request - #2151

Merged
lagergren merged 1 commit into
redhat-developer:mainfrom
lagergren:fix/send-nothing-but-exit-after-shutdown
Nov 23, 2026
Merged

lagergren merged 1 commit into
redhat-developer:mainfrom
lagergren:fix/send-nothing-but-exit-after-shutdown

Conversation

@lagergren

Copy link
Copy Markdown
Collaborator

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 fix: send a language server the notifications queued before its stop ahead of its shutdown #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:

  • fix: send a stopping language server no $/cancelRequest after its shutdown request #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.

…own request

LSP allows only the exit notification after the shutdown request, but a request that a
feature sent to a stopping language server after its shutdown request was still written.
The editor's code vision does so: it gets the language servers of the file, and sends
textDocument/codeLens on another thread, which can come after the stop. The request then
stayed pending until the language server answered it, or the stop cancelled it.

Once the shutdown request is queued to a connection, only the exit notification and the
responses to the requests of the language server are queued after it:
- a request ends at once with a CancellationException, as the requests of a stopped
  language server do, and is neither traced nor sent;
- a notification is dropped, as a $/cancelRequest already was.

Fixes redhat-developer#2145
@lagergren lagergren added this to the 0.30.2 milestone Nov 23, 2026
@lagergren lagergren added the bug Something isn't working label Nov 23, 2026
@coderabbitai

coderabbitai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • Bug Fixes
    • Improved language server shutdown handling: requests sent after shutdown are cancelled, and notifications sent after shutdown are ignored.
    • Responses to server requests can still be sent during shutdown, and the server can still receive its exit notification.
    • Messages accepted before shutdown continue to be processed in order.

The outgoing message queue now rejects requests and notifications after shutdown is queued, while allowing server responses and exit. Tests cover these cases, including a request refused during a race between the initial check and queueing.

Changes: Shutdown message policy

Layer / File(s) Summary
Enforce the post-shutdown message policy
src/main/java/com/redhat/devtools/lsp4ij/LanguageServerWrapper.java
The wrapper checks outgoing messages before tracing and again when queueing. It refuses requests with CancellationException and drops notifications, while allowing responses and exit.
Verify post-shutdown behavior
src/test/java/com/redhat/devtools/lsp4ij/CancelRequestAfterShutdownTest.java
Tests verify that a workspace-symbol request is cancelled, a configuration-change notification is dropped, and a server configuration request receives a response while shutdown is pending.

Estimated code review effort: 2 (Simple) | ~12 minutes

Change: Bug fix · Severity of issue fixed: Low

Sequence Diagram(s)

sequenceDiagram
  participant LanguageServerWrapper
  participant OutgoingMessages
  participant LanguageServer
  LanguageServerWrapper->>OutgoingMessages: Queue shutdown
  OutgoingMessages-->>LanguageServerWrapper: Accept shutdown
  LanguageServerWrapper->>LanguageServer: Send shutdown
  LanguageServerWrapper->>OutgoingMessages: Queue a later request
  OutgoingMessages-->>LanguageServerWrapper: Refuse request
  LanguageServer->>LanguageServerWrapper: Request configuration
  LanguageServerWrapper->>OutgoingMessages: Queue response
  OutgoingMessages-->>LanguageServerWrapper: Accept response
  LanguageServerWrapper->>LanguageServer: Send response
  LanguageServerWrapper->>OutgoingMessages: Queue exit
  OutgoingMessages-->>LanguageServerWrapper: Accept exit
  LanguageServerWrapper->>LanguageServer: Send exit
Loading

Suggested reviewers: angelozerr, d-biehl

Merge Risk: Minimal · up to 8c9e6

After shutdown is queued, the client now sends only exit and responses to server requests. Later client requests are cancelled immediately and later notifications are dropped. This closes the race that let Code Vision send textDocument/codeLens after shutdown. No concrete merge-blocking risk was found, and the change adds tests for the new behavior.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title states the main change: after a language server's shutdown request, the client sends only exit. The change does exactly this, refusing later requests and notifications while allowing exit an…
Description check Passed The description explains the failing CI test, the root cause (a codeLens request sent from another thread after shutdown), the behaviour change, the three new tests, and the verification runs. It rela…
Linked Issues check Passed [#2145] Requirement 1: once shutdown is queued, nothing but exit is written. The change in LanguageServerWrapper.java refuses later client requests and notifications after the queued shutdown. Only ex…
Out of Scope Changes check Passed The changes stay within #2145. LanguageServerWrapper.java adds the shutdown gate on outgoing messages, the check before tracing and again at queueing, and the CancellationException and drop behaviour.…
Full details: Title check

Explanation

The title states the main change: after a language server's shutdown request, the client sends only exit. The change does exactly this, refusing later requests and notifications while allowing exit and server-request responses. The title uses the 'fix:' prefix and is one clear sentence, though it is somewhat long at 13 words.

Full details: Description check

Explanation

The description explains the failing CI test, the root cause (a codeLens request sent from another thread after shutdown), the behaviour change, the three new tests, and the verification runs. It relates directly to the changeset.

Full details: Linked Issues check

Explanation

[#2145] Requirement 1: once shutdown is queued, nothing but exit is written. The change in LanguageServerWrapper.java refuses later client requests and notifications after the queued shutdown. Only exit and responses to server-initiated requests pass. Requirement 2: a request made after shutdown ends at once with a CancellationException, as requests to a stopped server do. The change completes it without tracing or writing it. Requirement 3: the fix applies whatever starts the request. It does not switch off Code Vision. The PR's CancelRequestAfterShutdownTest adds a post-shutdown request test, a post-shutdown notification test, and a workspace/configuration response test. Verification relied on the whole-PR summary and PR description. Reading the full wrapper diff failed because the changed blobs were unavailable, so the exact hunk was not read directly. Note: the issue text says nothing but exit is written. The PR deliberately keeps responses to server-initiated requests after shutdown. This is a narrow, explicit exception, and the PR tests it. It does not affect the motivating client-request case.

Full details: Out of Scope Changes check

Explanation

The changes stay within #2145. LanguageServerWrapper.java adds the shutdown gate on outgoing messages, the check before tracing and again at queueing, and the CancellationException and drop behaviour. CancelRequestAfterShutdownTest.java adds the three tests for the post-shutdown request, notification, and server-request response cases. The PR description states that #2105's tests are unchanged and that related test classes pass. No unrelated changes were identified in the summary. The full wrapper diff could not be read directly, so this verdict rests on the summary and description.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 8a184529-50f1-44a6-9736-4db9c211b39e
Commits

Reviewing files that changed from the base of the PR and between 11be81a and 8c9e66e.

Files selected for processing: 2

  • src/main/java/com/redhat/devtools/lsp4ij/LanguageServerWrapper.java
  • src/test/java/com/redhat/devtools/lsp4ij/CancelRequestAfterShutdownTest.java

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 0 remain after this review.

@codecov

codecov Bot commented Nov 23, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 53.33333% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 53.21%. Comparing base (11be81a) to head (8c9e66e).

Files with missing lines Patch % Lines
.../redhat/devtools/lsp4ij/LanguageServerWrapper.java 53.33% 4 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2151      +/-   ##
==========================================
- Coverage   53.22%   53.21%   -0.02%     
==========================================
  Files         826      826              
  Lines       34717    34728      +11     
  Branches     7143     7147       +4     
==========================================
+ Hits        18477    18479       +2     
- Misses      13468    13477       +9     
  Partials     2772     2772              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@lagergren

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Action not completed

Review rate limited.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@lagergren

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@lagergren
lagergren merged commit 53fd2f9 into redhat-developer:main Nov 23, 2026
9 checks passed
lagergren added a commit that referenced this pull request Nov 23, 2026
…ion that it shuts down (#2162)

## What

`CancelRequestAfterShutdownTest.testTheAnswerToARequestOfTheLanguageServerIsSentOnceTheShutdownIsSent`, which came with #2151, failed once on CI, on macOS 2024.2 (run [45231743923](https://github.com/redhat-developer/lsp4ij/actions/runs/45231743923), in #2160):

    The language server must read the answer to its request, and exit, after shutdown expected:<[shutdown, ResponseMessage, exit]> but was:<[shutdown, exit]>

It's a bug in the test, not in LSP4IJ or #2151: LSP4IJ did answer the request, but over another connection.

## Cause

- **A restart:** the test stops the language server while its editor is open. A stop that doesn't disable the language server lets a feature of the editor start it again, over a second connection (`start()` with the status `stopping`). The code vision does that when its first lookup of the language servers comes after the stop.
- **The wrong client:** the mock language server then has two clients. The test sent `workspace/configuration` through the last of `getRemoteProxies()`, which was the new connection's, and that connection answered it. The connection that the test stops, and whose reads it checks, read only `shutdown` and `exit`.
- **Reproduced:** under CPU load on macOS, with probes, main failed 2 of 2,100 runs. Each time, the second connection read the answer:

      [[initialize, initialized, didOpen, diagnostic, shutdown, exit], [initialize, ResponseMessage, initialized, didOpen, diagnostic, codeLens, $/cancelRequest]]

  With a 20 ms delay before the code vision's lookup of the language servers, 7 of 100 runs fail. Asking for the file's language servers right after the shutdown is read fails 20 of 20.

## Change

- The test's connection keeps the client that it gives the language server.
- The test sends the request through the client of the connection that it stops.
- The test checks that the request ends with an answer: `isDone()` is also true for a request that couldn't be sent.

## Test

- **With the fix:** 0 of 20 runs fail with the forced restart, and 0 of 100 with the delayed code vision (55 of them with two clients when the request was sent).
- **`CancelRequestAfterShutdownTest`:** 8 tests, 0 failures, on IC 2024.2, IC 2025.3 and IU 2026.3.
- **Full suite on IC 2024.2:** 2,134 tests, 0 failures, 0 errors, 7 skipped, as before.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A stopping language server is sent requests, such as textDocument/codeLens, after its shutdown request

1 participant