Skip to content

test: accept the RequestCancelled answer of a stopped language server as the cancel of its request - #2104

Merged
lagergren merged 1 commit into
redhat-developer:mainfrom
lagergren:fix/workspace-symbol-stop-pending-flake
Nov 23, 2026
Merged

lagergren merged 1 commit into
redhat-developer:mainfrom
lagergren:fix/workspace-symbol-stop-pending-flake

Conversation

@lagergren

Copy link
Copy Markdown
Collaborator

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

Found, not changed

The traces show the stop writing $/cancelRequest after shutdown. LSP allows only exit after shutdown.

It has no effect that users see, and it doesn't cause this failure, so it would be a PR of its own.

… 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.
@lagergren lagergren added this to the 0.30.2 milestone 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

  • Tests
    • Updated the cancellation test to recognize both directly cancelled requests and requests that complete with a cancellation error.

The workspace symbol cancellation test now accepts either direct future cancellation or exceptional completion with an LSP request-cancelled exception.

Changes: Workspace symbol cancellation

Layer / File(s) Summary
Recognize cancellation outcomes
src/test/java/com/redhat/devtools/lsp4ij/features/workspaceSymbol/ServerStopPendingWorkspaceSymbolTest.java
The test checks direct future cancellation and exceptional completion identified by CancellationUtil as a request-cancelled error.

Estimated code review effort: 1 (Trivial) | ~4 minutes

Change: Other

Merge Risk: Minimal · up to 6472c

This change only makes a flaky cancellation test more tolerant of a valid race outcome. It does not affect production behavior, and merge risk is minimal.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title states the primary change: the test now accepts the RequestCancelled answer of a stopped language server as the cancellation of its request. This matches the changeset, which updates ServerS…
Description check Passed The description explains the flaky test failure, its cause (LSP4J sends $/cancelRequest before cancelling the future, so a RequestCancelled response can end the request first), the test change using C…
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 primary change: the test now accepts the RequestCancelled answer of a stopped language server as the cancellation of its request. This matches the changeset, which updates ServerStopPendingWorkspaceSymbolTest to accept either a cancelled future or a RequestCancelled ResponseErrorException.

Full details: Description check

Explanation

The description explains the flaky test failure, its cause (LSP4J sends $/cancelRequest before cancelling the future, so a RequestCancelled response can end the request first), the test change using CancellationUtil.isRequestCancelledException, and the verification performed. It is directly related to the changeset.

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: 2c345ba1-fcf3-4788-9ee3-74902295910a
Commits

Reviewing files that changed from the base of the PR and between a82a94e and 6472c17.

Files selected for processing: 1

  • src/test/java/com/redhat/devtools/lsp4ij/features/workspaceSymbol/ServerStopPendingWorkspaceSymbolTest.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

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 50.41%. Comparing base (a82a94e) to head (6472c17).

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.
📢 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 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 40c958d into redhat-developer:main Nov 23, 2026
9 checks passed
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant