Skip to content

fix: keep a stopped language server stopped when a start that the stop replaced runs late - #2128

Merged
lagergren merged 1 commit into
redhat-developer:mainfrom
lagergren:fix/keep-a-stopped-server-stopped-when-a-late-start-runs
Nov 23, 2026
Merged

lagergren merged 1 commit into
redhat-developer:mainfrom
lagergren:fix/keep-a-stopped-server-stopped-when-a-late-start-runs

Conversation

@lagergren

Copy link
Copy Markdown
Collaborator

What

KeepAliveDuringPendingStopTest.testTheReconnectionOfARestartEndsWhenTheRestartIsStopped of #2119 failed on IC 2024.2 (run 45082111576). After a restart and a stop right after it, the language server never showed as stopped. The bug is in main, not in #2119.

Cause

The task of a start runs on a pooled thread. It set the status to starting first, and only later checked whether a stop had replaced the start. When the pooled thread ran the task only after the stop had ended, the status went from stopped to starting and stayed there, since no later step of that start sets it again. The server was stopped, but getServerStatus() and the Language Servers view showed it as starting until it was started or stopped again.

A probe that lets the start task run only once the stop has ended shows it on main 3 times out of 3:

startTask runs, attempt current=false status=stopped
status stopped -> starting

and no status change after that.

Change

The start task now checks first, under the monitor that a stop uses to replace the start. If the start was replaced, the task ends at once, without setting the status or connecting to the language server. It cancels the start as the same check later in the task already did; the stop has cancelled initializeFuture by then, so the failure handler doesn't run and no error is reported.

Behaviour change

A language server that is stopped just after a start no longer shows as "starting" once the stop has ended.

Test

  • New: LanguageServerWrapperRestartTest.testAStartThatAStopReplacesBeforeItsTaskRunsLeavesTheServerStopped holds the start task back, with an application proxy, until a stop has replaced the start, then runs it. It checks that the status stays stopped and that no connection is made. It fails on main (expected:<stopped> but was:<starting>) and passes with the change. The existing inline-tasks helper is now withPooledThreadTasks.
  • 10 related classes (the restart, keep-alive, watched files, move, file operation, selected editor and instance tests): 181 tests pass on each of 2024.2, 2025.3 and IU 2026.3.
  • The full suite passes: 1865 tests, 0 failures, 7 skipped.

…p replaced runs late

The task of a start runs on a pooled thread. It set the status to
starting first, and only later checked whether a stop had replaced the
start. When the pooled thread ran the task only after the stop had
ended, the status went from stopped to starting and stayed there, since
no later step of that start sets it again. The language server was
stopped, but getServerStatus() and the Language Servers view showed it
as starting until it was started or stopped again.

It made KeepAliveDuringPendingStopTest of redhat-developer#2119 fail on IC 2024.2 (run
45082111576): after a restart and a stop right after it, the language
server never showed as stopped. A probe that lets the start task run
only once the stop has ended shows it on main, 3 times out of 3:

    startTask runs, attempt current=false status=stopped
    status stopped -> starting

and no status change after that.

The task now checks first, under the monitor that a stop replaces the
start under. If the start was replaced, it ends at once, without
setting the status or connecting to the language server.

Behaviour change: a language server that is stopped just after a start
no longer shows as starting once the stop has ended.
@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
    • Fixed a startup race that could allow a language server to start after it had been stopped. The server now remains stopped in this situation, preventing an unexpected connection.

LanguageServerWrapper.start() now cancels a queued start when the wrapper is disposed or its initializing context is no longer current. A regression test verifies that a queued task does not connect after stop().

Changes: Start lifecycle guard

Layer / File(s) Summary
Start guard and regression test
src/main/java/com/redhat/devtools/lsp4ij/LanguageServerWrapper.java, src/test/java/com/redhat/devtools/lsp4ij/LanguageServerWrapperRestartTest.java
The start task checks the wrapper state before setting its status to starting. The regression test queues a task, stops the wrapper, then verifies that running the task leaves the wrapper stopped and does not create a connection. The pooled-thread test helper accepts a task handler so the test can capture scheduled tasks.

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

Change: Bug fix

Suggested reviewers: angelozerr, stephanwald

Merge Risk: Minimal · up to 3f7c8

This fix keeps a language server from being shown as starting after it was stopped. The change is small and has a regression test. No merge-blocking risk was identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: a stopped language server stays stopped when a start that a stop replaced runs late. This matches the raw summary (start() checks under the monitor and exits if replac…
Description check Passed The description explains the cause (a pooled start task sets status to starting after a stop has ended), the change (an early check under the stop monitor), the behaviour change, and the tests added. …
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 names the main change: a stopped language server stays stopped when a start that a stop replaced runs late. This matches the raw summary (start() checks under the monitor and exits if replaced) and the objectives (status race fix). The title is a single sentence, though it is long (89 characters).

Full details: Description check

Explanation

The description explains the cause (a pooled start task sets status to starting after a stop has ended), the change (an early check under the stop monitor), the behaviour change, and the tests added. It matches the changeset in LanguageServerWrapper and LanguageServerWrapperRestartTest.

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: 8db0914e-9d8f-4731-950d-c4b94c94e46f
Commits

Reviewing files that changed from the base of the PR and between 40c958d and 3f7c89e.

Files selected for processing: 2

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

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

@codecov

codecov Bot commented Nov 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 50.66%. Comparing base (40c958d) to head (3f7c89e).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2128      +/-   ##
==========================================
- Coverage   50.66%   50.66%   -0.01%     
==========================================
  Files         816      816              
  Lines       33910    33915       +5     
  Branches     5290     6980    +1690     
==========================================
+ Hits        17180    17182       +2     
- Misses      14066    14070       +4     
+ Partials     2664     2663       -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 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 778eed4 into redhat-developer:main Nov 23, 2026
9 checks passed
lagergren added a commit that referenced this pull request Nov 23, 2026
… its open documents again (#2119)

Fixes #2133

#2128, whose fix `testTheReconnectionOfARestartEndsWhenTheRestartIsStopped` needs to be stable, is merged, and so is `main` into this branch (3336fea): the 10 related classes pass on it (189 tests).

## What

`WatchedFilesTest.testTheWatchersOfAStoppedServerAreDropped` failed on `main` on Windows ([run 45057940746](https://github.com/redhat-developer/lsp4ij/actions/runs/45057940746)). Right after `restart()`, `getLanguageServers()` returned no language server for the open file.

**Verdict: a real bug in LSP4IJ, not in the test.** The end of a keep-alive lease stopped the restarted server while it was still starting.

## Cause

Since #2076, a file operation keeps its language server alive with a lease until the server is told of it:
1. The test creates a file just before the restart, so that lease ended after the restart had begun.
2. The restart's stop disconnects the open documents, and the restarted server connects them again only once it has initialized, so it had none yet.
3. The end of the lease found nothing keeping it alive, and stopped it. That cancelled its start, and the request waiting for it got nothing.

A trace of a failure under load:

```
133.585ms [EDT]      restart begin
134.789ms [EDT]      start NEW ATTEMPT                    (restart)
142.027ms [FJ]       getLanguageServers matched … status=starting   (the test's connect)
142.291ms [pooled 4] stop … status=starting
      at maybeShutdown ← decrementKeepAlive ← LanguageServerItem$1.dispose ← AbstractLSPFileListener.sendThenRelease
142.347ms            initialized server=null error=CancellationException
```

In tests the stop is immediate. In the IDE it comes after `lastDocumentDisconnectedTimeout` (5 s by default), so it stops a restarted server that takes longer than that to initialize.

## Change

- A restart holds off the stops for idleness from before its stop until the open documents are connected again, or until its start fails or is stopped. A start that connects again the documents of a connection that ended does so too.
- A stop asked for meanwhile, as by the end of a lease, is then decided: the server stops only if nothing keeps it alive by then. A restarted server that no stop was asked for keeps running, as before, so a server that `LanguageServerManager.start()` or the console's Restart starts without an open document isn't stopped.
- Leases are counted as before, once for the language server, whatever connection they were taken on. That's the contract of the public `LanguageServerItem.keepAlive()`: a lease is a demand on the server as an open document is, and an open document keeps a restarted server running. `LanguageServerItem` doesn't change.
- The hold of a start that connects the documents of an ended connection again also ends when a stop replaces that start (d4efe26). The stop cancels `initializeFuture`, which then runs neither the reconnection nor the failure handler, so later stops for idleness would have waited forever. That hold exists only in this PR, so this changes nothing against `main`.
- `LanguageServiceAccessor.sendDidOpenAndRefreshEditorFeatureForOpenedFiles` (package-private) returns a future that completes once each open file is connected, or its connection failed or was cancelled.

The first commit of this PR tied the end of a lease to the connection it was taken on. The second one replaces that, after CodeRabbit's review: it broke the lease contract, and left a restarted server running after its last lease ended when a lease from before the restart outlived one taken after it.

## Behaviour change

A lease that ends, or another reason to stop for idleness that comes, while a language server restarts no longer stops it before it has connected its open documents again. Nothing else changes for users or plugins.

## Test

`KeepAliveDuringPendingStopTest`:

| Test | Checks |
|---|---|
| `testALeaseThatEndsAsTheServerRestartsLetsItStartBeforeItStops` | The flaky test's order: the request made during the restart isn't ended by the end of the lease, and the server starts before the deferred stop for idleness stops it, as it has no document. Fails on `main`. Since 769eb34 it no longer asserts which server the request gets: in unit-test mode the deferred stop comes at once and can come before the request reads the server, which failed it on CI twice. |
| `testALeaseHeldAcrossARestartKeepsTheRestartedServerRunningUntilItEnds` | CodeRabbit's order: the end of a lease taken after the restart doesn't stop the server while one from before is held; the end of that one does. |
| `testALeaseTakenAfterARestartStopsTheServerWithoutDocumentWhenItEnds` | A lease on the restarted server still stops it when it ends. |
| `testALeaseTakenBeforeTheServerStartsKeepsItRunningAndStopsItWhenItEnds` | A lease from before the first start. |
| `testARestartedServerWithoutDocumentOrLeaseKeepsRunning` | As on `main`. |
| `testTheReconnectionOfARestartEndsWhenItsStartFails`, `…WhenTheRestartIsStopped` | The hold ends when the restart's start fails or is stopped. |
| `testTheReconnectionOfAStartForAnEndedConnectionEndsWhenTheStartIsStopped` | The hold of a start that connects an ended connection's documents again ends when a stop replaces it. Fails before d4efe26. |

- **With a probe** that delays the lease's release 100 ms and the restarted start 300 ms, `WatchedFilesTest` fails 5 of 5 runs on `main` and passes 5 of 5 with the change.
- **10 related classes** (187 tests after d4efe26: the keep-alive, watched files, move, file operation, restart and instance tests) pass on 2024.2, 2025.3 and IU 2026.3, and also merged with today's `main`.
- **The full suite passes:** 1866 tests, 0 failures, 7 skipped.
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