Skip to content

fix: a language server added while several projects are open starts only for the open files of each project, not once more in every other open project - #2225

Merged
lagergren merged 1 commit into
redhat-developer:mainfrom
lagergren:fix/server-added-starts-only-in-its-own-project
Nov 25, 2026

Conversation

@lagergren

Copy link
Copy Markdown
Collaborator

Fixes #2224

What

With several projects open, adding a language server definition started the server in every open project, for the open files of all open projects. When a definition is added, every project's LanguageServiceAccessor hears it. Its checkCurrentlyOpenFiles went through the open files of every open project (ProjectManager.getOpenProjects()), but connected them through its own project. So each file got one instance of the server per open project, and all but one of them had another project's root and workspace folders.

It also made LSPSemanticTokenDocumentationProviderTest.testPlainTextOfTwoLanguageServers fail now and then on Linux CI: 3 of about 520 jobs since 2026-11-23, including #2198 (#2207) and #2222.

  • In the CI JVM, the light tests' shared project stays open, so addSecondServer started the second server twice.
  • Both starts connected to the definition's one MockLanguageServer, whose addRemoteProxy adds to plain ArrayLists. Two concurrent adds threw ArrayIndexOutOfBoundsException, and one start failed with CannotStartServerException.
  • When that was the test project's own start, the hover had only the first server's text.

#2208 fixed a different cause of the same symptom: the hover cache across language-server changes.

Change

  • checkCurrentlyOpenFiles starts the added definitions only for the open files of the accessor's own project. Each open project's accessor does the same for its own files, as handleChanged and handleRemoved already only touch their own project's servers.
  • Disposed and default projects are skipped.

A project whose accessor doesn't exist yet doesn't connect its open files when a definition is added. Before, another project connected them, to a server of that other project. They connect as usual once a feature or an editor of the project uses LSP4IJ.

Tests

New ServerAddedWithSeveralOpenProjectsTest (2 tests). It opens a second project and adds a server while a file is open in one of the two projects. Then it checks two things:

  • The file gets its own project's server, with a bounded wait.
  • Once the async tasks are done, the other project started none.

Red-green: without the fix, both tests fail every run, with the wrapper that the other project started (expected:<[]> but was:<[LanguageServerWrapper@…]>). With the fix, both pass.

Reproduction of the CI failure, with the light project opened first:

Condition Runs Failures A duplicate server started
Before the fix 1,000 1, the exact CI message every run
After the fix 1,000 0 never
A CI-like Linux suite (the 238 classes that precede it on CI), before the fix 800 0 800 of 800
The same, after the fix 800 0 0

The new class, features.documentation.*, features.semanticTokens.* and *Hover* pass headless on IC 2024.2, 2025.3 and IU 2026.3: 60 tests each. The full suite passes on 2024.2: 2,416 tests, 0 failures, with the 7 skipped tests that were already disabled. Rebased on main at 57bae55, the new class and the hover class pass headless on all three platforms.

Not in this PR: MockLanguageServer and MockTextDocumentService.addRemoteProxy still aren't thread-safe. A test that starts two connections to one mock at once could still hit the same exception.

Behaviour change

Before After
A language server added while several projects are open starts in each open project for the open files of all of them, so each file gets one instance per open project, most of them with another project's root It starts in each project for that project's own open files only: one instance per file, of the file's own project

…nly for the open files of each project, not once more in every other open project

## What

When a language server definition is added, as a user-defined server, a plugin's extension or a test's server, the `LanguageServiceAccessor` of each open project hears of it. Each one went through the open files of every open project, and started the definition's language server in its own project for all of them. With two projects open, a file of project B got a second instance of the server, started in project A with A's root and workspace folders, next to the instance of B, and the files of A got one in B.

It also made `LSPSemanticTokenDocumentationProviderTest.testPlainTextOfTwoLanguageServers` fail at times on the Linux jobs, with only the first server's hover (`First line<br>Second line`): runs 45191503838 (2025.3), 45424896318 (2025.3) and 45523015765 (2024.2).

## Cause of the flaky test

- In the CI JVM, the shared project of the light tests stays open, and a light test that ran before, such as `SemanticTokensInspectorTest`, gave it an accessor.
- So `addSecondServer` started the second server twice: in the test's project, and in the light project for the test's file. A suite of the 238 classes that run before it on CI, followed by the test 400 times, did so in each of the 400 runs.
- Both starts connect to the one `MockLanguageServer` of the definition, whose `addRemoteProxy` adds to plain `ArrayList`s. Two starts at once can make one of them fail with `ArrayIndexOutOfBoundsException`. When the failed start is the test project's own, the hover leaves that server out.
- A probe that opens the light project first, then runs the test 1000 times, failed once with that exact message, after a start that failed so. Without the light project, 3700 runs passed.
- Neither redhat-developer#2207, since the test asks for a hover once, nor `testTheHoverOfALanguageServerWhenTheOtherOneCannotStart`: the 2025.3 failure of run 45191503838 came before that test existed.

## Change

`checkCurrentlyOpenFiles` starts the language servers of the added definitions for the open files of the accessor's own project only. The accessor of each other open project does so for the files of its own, as `handleChanged` and `handleRemoved` already deal only with the accessor's own language servers.

## Test

`ServerAddedWithSeveralOpenProjectsTest` opens a second project, and adds a language server while a file is open in one of the two projects:
- `testTheAddedServerDoesNotStartInAnotherProjectForAFileOfThisOne`: the file of this project gets the language server of this project, and the other project starts none.
- `testTheOpenFileOfAnotherProjectGetsTheAddedServerOfThatProject`: the file open in the other project gets the language server of that project, and this project starts none.

Both fail without the change, each with the language server that the other project started.

## Behaviour change

| Before | After |
|---|---|
| A language server added while several projects are open starts in each open project for the open files of all of them, so each file gets one instance per open project | It starts in each project for that project's open files only: one instance per file, of the file's own project |

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

coderabbitai Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • Bug Fixes
    • Language servers added while multiple projects are open now start only for the project that has an open file matching the server’s language. Other projects are not affected.
    • Server startup is skipped for disposed projects and the default project, preventing servers from being started in those contexts.

When a language server definition is added, LanguageServiceAccessor checks matching open files only in its own project. The accessor skips checks for disposed and default projects. New tests cover server startup across two open projects.

Changes: Project-local startup

Layer / File(s) Summary
Project-local file checks and validation
src/main/java/com/redhat/devtools/lsp4ij/LanguageServiceAccessor.java, src/test/java/com/redhat/devtools/lsp4ij/ServerAddedWithSeveralOpenProjectsTest.java
checkCurrentlyOpenFiles checks only the accessor’s project and skips disposed or default projects. The tests verify that an added server starts for the project with a matching open file, but not for the other project.

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

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: angelozerr, mituuz

Merge Risk: Minimal · up to 2e5b0

Adding a language server definition now starts the server only for matching open files in their own project. This avoids duplicate server instances with wrong roots and workspace folders. The change is small, comes with tests for the two-project case, and no merge-blocking risk remains.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title states the main change: a language server added while several projects are open now starts only for the open files of each project, not in every other open project. This matches the raw summ…
Description check Passed The description explains the problem (duplicate server instances per open project with wrong roots), the change (per-project checkCurrentlyOpenFiles, skipping disposed and default projects), the new S…
Linked Issues check Passed Directly linked issue #2224 requires that adding a language server definition starts it only for open files of the file's own project, not in every open project for all open files. At the reviewed hea…
Out of Scope Changes check Passed The changes are limited to src/main/java/com/redhat/devtools/lsp4ij/LanguageServiceAccessor.java and the new test class src/test/java/com/redhat/devtools/lsp4ij/ServerAddedWithSeveralOpenProjectsTest.…
Full details: Title check

Explanation

The title states the main change: a language server added while several projects are open now starts only for the open files of each project, not in every other open project. This matches the raw summary (checkCurrentlyOpenFiles restricted to the accessor's own project) and the linked issue #2224 objective.

Full details: Description check

Explanation

The description explains the problem (duplicate server instances per open project with wrong roots), the change (per-project checkCurrentlyOpenFiles, skipping disposed and default projects), the new ServerAddedWithSeveralOpenProjectsTest tests, validation runs, and a behaviour change table. It is closely related to the changeset.

Full details: Linked Issues check

Explanation

Directly linked issue #2224 requires that adding a language server definition starts it only for open files of the file's own project, not in every open project for all open files. At the reviewed head, LanguageServiceAccessor.handleAdded calls checkCurrentlyOpenFiles for its own project only, and the comment states that the accessor of each other open project handles its own project's files. The PR summary and description state that checkCurrentlyOpenFiles now uses only the accessor's project, and that disposed and default projects are skipped. The new ServerAddedWithSeveralOpenProjectsTest checks both directions: a file in the fixture project gets only that project's server, and a file in the other project gets only that project's server. Limits: the body of checkCurrentlyOpenFiles was truncated in the repository read, so the exact skip logic rests on the PR description and raw summary. The known side effect, that a project without an accessor does not connect its open files on definition add until a feature or editor uses LSP4IJ, is a documented trade-off and is not a requirement of #2224. The thread-safety limit of MockLanguageServer is outside the issue's coding requirements. The flaky-test symptom is addressed by the same change, and the reported reproduction results are author-reported, not independently verified here.

Full details: Out of Scope Changes check

Explanation

The changes are limited to src/main/java/com/redhat/devtools/lsp4ij/LanguageServiceAccessor.java and the new test class src/test/java/com/redhat/devtools/lsp4ij/ServerAddedWithSeveralOpenProjectsTest.java. The accessor change restricts checkCurrentlyOpenFiles to the accessor's project, which is the linked issue's requirement. The new test directly verifies that requirement. The accessor's documentation update describes the same behavior. No unrelated changes were identified in the reviewed evidence. The body of checkCurrentlyOpenFiles could not be fully read due to truncation, so this verdict relies on the summary, the visible handleAdded change, and the test file.

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: 3d87560e-637d-42cb-b1b4-372776d77cd9
Commits

Reviewing files that changed from the base of the PR and between 57bae55 and 2e5b0fb.

Files selected for processing: 2

  • src/main/java/com/redhat/devtools/lsp4ij/LanguageServiceAccessor.java
  • src/test/java/com/redhat/devtools/lsp4ij/ServerAddedWithSeveralOpenProjectsTest.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 25, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 25.00000% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 55.50%. Comparing base (57bae55) to head (2e5b0fb).

Files with missing lines Patch % Lines
...edhat/devtools/lsp4ij/LanguageServiceAccessor.java 25.00% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2225      +/-   ##
==========================================
- Coverage   55.52%   55.50%   -0.02%     
==========================================
  Files         843      843              
  Lines       35939    35937       -2     
  Branches     5638     7431    +1793     
==========================================
- Hits        19955    19948       -7     
  Misses      13097    13097              
- Partials     2887     2892       +5     

☔ 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 25, 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 25, 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 2d0cefb into redhat-developer:main Nov 25, 2026
13 of 14 checks passed
lagergren added a commit that referenced this pull request Nov 25, 2026
… at once without losing one (#2226)

## What

`MockLanguageServer` and `MockTextDocumentService` keep the clients of their connections in plain `ArrayList`s. Two starts of a mock server at the same time add to them concurrently. That threw `ArrayIndexOutOfBoundsException` in `MockLanguageServer.addRemoteProxy` and failed one of the starts with `CannotStartServerException`.

This is how `LSPSemanticTokenDocumentationProviderTest.testPlainTextOfTwoLanguageServers` lost its second server's hover on CI: #2224 started the server twice, and #2225 fixes that. With #2225 that test no longer starts a server twice, but any test that starts two connections to one mock at once could still hit the same exception.

## Change

Test only.
- Both lists are now `CopyOnWriteArrayList`s, so concurrent adds, and iteration through `MockLanguageServer.getRemoteProxies()`, are safe.
- `MockTextDocumentService.reset()` clears its list instead of replacing it, under the lock that `publishDiagnostics` holds while it takes the first client and rotates the list.

## Tests

No new test: the race needs two starts of one mock at once, which #2225 removes from the suite. The evidence is the stack trace that the investigation of #2224 found on CI.

The whole suite passes headless on IC 2024.2 and 2025.3: 2,422 tests each, 0 failures, with the 7 skipped tests that were already disabled.
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 language server added while several projects are open starts once more in every other open project, for that project's open files

1 participant