Skip to content

fix: send a stopping language server no $/cancelRequest after its shutdown request - #2105

Merged
lagergren merged 1 commit into
redhat-developer:mainfrom
lagergren:fix/end-the-requests-of-a-stopping-server-without-cancel-request
Nov 23, 2026
Merged

lagergren merged 1 commit into
redhat-developer:mainfrom
lagergren:fix/end-the-requests-of-a-stopping-server-without-cancel-request

Conversation

@lagergren

Copy link
Copy Markdown
Collaborator

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.

Related

…tdown request

LSP allows only the exit notification after the shutdown request, but a
stopping language server was often sent $/cancelRequest after it:

- the stop cancels the requests kept for it, as workspace/symbol, after it
  has started the shutdown on another thread;
- cancelOnStop cancels at once a request that comes after the stop;
- the stop disposes the supports of the files, which cancel their requests,
  in a read action that runs later;
- a feature can cancel its request while the server shuts down.

LSP4J sends $/cancelRequest for each, through the message writer of the
connection.

The messages of a connection are now queued to the message writer under one
lock, which notes when the shutdown request is queued, and 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 server runs, still sends
$/cancelRequest.
@lagergren lagergren added this to the 0.30.2 milestone Nov 22, 2026
@lagergren lagergren added the bug Something isn't working label Nov 22, 2026
@coderabbitai

coderabbitai Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • Bug Fixes
    • Prevented cancellation notifications from being sent after a language server begins shutting down.
    • Improved the ordering of outgoing messages, helping ensure cancellation notifications are handled consistently during normal operation and shutdown.

The wrapper now queues outgoing messages in order and drops cancellation notifications queued after a shutdown request. New tests record message order and check cancellation during normal operation and shutdown.

Changes: Outgoing cancellation handling

Layer / File(s) Summary
Queue outgoing messages and suppress late cancellations
src/main/java/com/redhat/devtools/lsp4ij/LanguageServerWrapper.java
Outgoing messages pass through an ordered queue before asynchronous writing. The queue drops cancellation notifications after a shutdown request is queued.
Verify cancellation timing
src/test/java/com/redhat/devtools/lsp4ij/CancelRequestAfterShutdownTest.java
Tests check cancellation during normal operation and shutdown. A recording connection captures message order, including the messages sent from shutdown onward.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Suggested reviewers: angelozerr, coobaha

Merge Risk: Minimal · up to d6877

This change stops a stopping language server from receiving $/cancelRequest after shutdown, so only exit follows. No actionable merge-blocking risk was found in the supplied changes, and the new tests cover the four described paths plus normal-operation cancellation.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title states the main change: a stopping language server no longer receives $/cancelRequest after its shutdown request. The raw summary confirms LanguageServerWrapper drops such cancellations once…
Description check Passed The description explains the problem (a $/cancelRequest sent after shutdown, which LSP forbids), its four causes, the queueing change in the message writer, behaviour changes, and the new CancelReques…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title states the main change: a stopping language server no longer receives $/cancelRequest after its shutdown request. The raw summary confirms LanguageServerWrapper drops such cancellations once shutdown is queued, and a new test covers this. The title is concise and specific, though it uses the word 'send' loosely.

Full details: Description check

Explanation

The description explains the problem (a $/cancelRequest sent after shutdown, which LSP forbids), its four causes, the queueing change in the message writer, behaviour changes, and the new CancelRequestAfterShutdownTest. It matches the changeset in the raw summary.

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: e42be910-48b0-40f9-96f1-9f325b5c8ae9
Commits

Reviewing files that changed from the base of the PR and between a158c02 and d68779c.

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 22, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.44444% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 50.42%. Comparing base (a158c02) to head (d68779c).

Files with missing lines Patch % Lines
.../redhat/devtools/lsp4ij/LanguageServerWrapper.java 94.44% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2105      +/-   ##
==========================================
- Coverage   50.43%   50.42%   -0.01%     
==========================================
  Files         816      816              
  Lines       33825    33842      +17     
  Branches     6961     5283    -1678     
==========================================
+ Hits        17060    17066       +6     
- Misses      14117    14127      +10     
- Partials     2648     2649       +1     

☔ 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 22, 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 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 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 cac53f9 into redhat-developer:main Nov 23, 2026
9 checks passed
lagergren added a commit that referenced this pull request Nov 23, 2026
…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.
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.

1 participant