Skip to content

fix: stop a language server whose start fails before its initialize future is set - #1827

Merged
angelozerr merged 1 commit into
redhat-developer:mainfrom
lagergren:fix/start-failure-status
Nov 3, 2026
Merged

angelozerr merged 1 commit into
redhat-developer:mainfrom
lagergren:fix/start-failure-status

Conversation

@lagergren

Copy link
Copy Markdown
Collaborator

When a language server's start fails early, LanguageServerWrapper.start() can leave the server in the "starting" status, with its connection provider not stopped, until the next start().

start() hands the creation of the connection to a pooled thread, then builds the future chain on startFuture, with the failure handler (.exceptionally(...)) at its end, and assigns the chain to initializeFuture. Until its synchronized (this) block, the pooled thread does not wait for the monitor that start() holds (once the client features exist, which LanguageServiceAccessor creates before it starts a server). So a start which fails there, such as a StreamConnectionProvider whose start() throws, can run in this order:

  1. start() submits the pooled task;
  2. the task fails and completes startFuture exceptionally;
  3. start() attaches the chain to the completed startFuture, so each stage runs at once on the calling thread, the failure handler too (or the handler runs on the pooled thread between the attach of .exceptionally(...) and the assignment);
  4. the handler sets serverError, shows the error notification, and calls initializeFuture.completeExceptionally(serverError) while initializeFuture is still null.

The NullPointerException becomes the result of the .exceptionally(...) future, which start() then assigns to initializeFuture, and nothing logs it. The handler's onError() and stop(context) are skipped, so the status stays "starting", the connection provider is not stopped, and a caller of getInitializedServer() gets the NullPointerException instead of the start error. The next start() recovers, because serverError is set. The window is short, so this is rare: the pooled thread has to fail before the calling thread has attached the chain.

Fixes #1825.

Related to #9.
The race reported there, on an exception in StreamConnectionProvider.start(), no longer applies, as I commented on the issue; this narrower ordering is what is left of it.

Changes

  • LanguageServerWrapper.start(): the pooled task is now a Runnable, which start() submits after it has assigned initializeFuture, instead of before it builds the chain. Every stage then attaches to a startFuture that is not complete yet, so none can run before the assignment, on any thread, and submitting the task after the assignment also orders the task and the handler after it. The task itself is unchanged.

A null check in the handler would not be enough: its stop(context) clears initializeFuture, which start() would then set again, to a done future that no later stop() clears, so no later start() would start the server.

Tests

LanguageServerWrapperRestartTest.testAStartWhichFailsBeforeStartReturnsStopsTheServer runs the pooled task on the calling thread, before executeOnPooledThread returns, through a proxy of the application which delegates every other call. So its first start, whose StreamConnectionProvider.start() throws, fails before start() returns. The test checks that:

  • the server stops, with the start error;
  • the caller of getInitializedServer() gets no server, not a NullPointerException;
  • the next start starts the server.

Without the change, the test fails: the server stays "starting", so the test times out, and getInitializedServer() fails with NullPointerException: Cannot invoke "java.util.concurrent.CompletableFuture.completeExceptionally(java.lang.Throwable)" because "this.initializeFuture" is null. With only a null check in the handler, the server stops, but the next start never starts it.

LanguageServerWrapper*Test, LanguageServerManagerTest, ConnectDuringStopTest, DocumentLifecycleTest, LanguageServerFileEnablementTest, DocumentContentSynchronizerDisposalTest, NotificationOrderTest and the installation tests pass (74 tests).

Locally, the full suite passes (1,011 tests, 3 skipped) except the 7 known macOS failures ("File accessed outside allowed roots"), which fail on main too. Not checked in a running IDE.

…uture is set

LanguageServerWrapper.start() hands the creation of the connection to a
pooled thread, then builds the future chain on startFuture, with the
failure handler at its end, and assigns the chain to initializeFuture.
Until its synchronized block, the pooled thread does not wait for the
monitor that start() holds (once the client features exist, which
LanguageServiceAccessor creates before it starts a server), so a start
which fails there, such as a StreamConnectionProvider whose start()
throws, can complete startFuture before start() has attached the chain
or assigned initializeFuture. A stage attached to a completed future
runs at once, so the failure handler then ran inside start(), or on the
pooled thread just before the assignment, with initializeFuture still
null. Its initializeFuture.completeExceptionally() threw a
NullPointerException, which the chain kept as the result of
initializeFuture, and nothing logged it. The handler's onError() and
stop(context) were skipped, so the server stayed "starting", the
connection provider was not stopped, and a caller of
getInitializedServer() got the NullPointerException instead of the
start error. The next start() recovered, since serverError was set.

start() now submits the pooled task only once initializeFuture is
assigned, so no stage of the chain can run before. A null check in the
handler would not do: its stop(context) clears initializeFuture, which
start() would then set again, to a done future that no later stop()
clears, so no later start() would start the server.

Tests: LanguageServerWrapperRestartTest gets
testAStartWhichFailsBeforeStartReturnsStopsTheServer, which runs the
pooled task on the calling thread, through a proxy of the application,
so that the start fails before start() returns. It checks that the
server stops with the start error, that the caller gets no server
rather than a NullPointerException, and that the next start starts the
server. Without the change, the server stays "starting" and the test
times out; with only a null check in the handler, the next start never
starts the server. LanguageServerWrapper*Test, LanguageServerManagerTest,
ConnectDuringStopTest, DocumentLifecycleTest,
LanguageServerFileEnablementTest, DocumentContentSynchronizerDisposalTest,
NotificationOrderTest and the installation tests pass (74 tests).
@coderabbitai

coderabbitai Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

No actionable comments were generated in the recent review.

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: 68143af8-d22b-4396-8138-89d7b7561594
Commits

Reviewing files that changed from the base of the PR and between e96dee9 and 6171de6.

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; 0 remain after this review.

Summary

Summary by CodeRabbit

  • Bug Fixes
    • Improved reliability when a language server fails during startup, so the failed attempt leaves it stopped and a later restart can succeed.

LanguageServerWrapper.start() now assigns initializeFuture before submitting startup work to the pooled executor. A regression test checks that an immediate startup failure stops the wrapper and that a later start succeeds.

Changes: Startup failure handling

Layer / File(s) Summary
Assign the future before scheduling startup
src/main/java/com/redhat/devtools/lsp4ij/LanguageServerWrapper.java
Startup work is stored in a Runnable. The wrapper submits it after assigning initializeFuture.
Test immediate failure and restart
src/test/java/com/redhat/devtools/lsp4ij/LanguageServerWrapperRestartTest.java
The test runs pooled-thread tasks inline. It checks that the first start returns null and stops the wrapper, then checks that a later start succeeds.

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

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: angelozerr, stephanwald

Merge Risk: Minimal · up to 6171d

The change assigns the initialization future before scheduling startup, addressing immediate failure handling and allowing restart. No actionable merge-blocking risk remains.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documenta… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title names the fix and its trigger: a language server whose start fails before its initialize future is set is stopped. The changeset reorders the pooled task submission in LanguageServerWrapper.…
Description check Passed The description explains the race in LanguageServerWrapper.start(), the reordering of the pooled task submission after initializeFuture assignment, the rejected null-check alternative, and the new tes…
Linked Issues check Passed Issue #1825 (direct) requires that an early start failure stops the server with the error, that the status does not stay "starting", and that the next start works. At the reviewed head, `LanguageServe…
Out of Scope Changes check Passed The whole-PR diff from merge base e96dee9 to head 6171de6 changes two files. In LanguageServerWrapper.java, the pooled task body is unchanged. Only its wrapping as a Runnable startTask and the pos…
Full details: Title check

Explanation

The title names the fix and its trigger: a language server whose start fails before its initialize future is set is stopped. The changeset reorders the pooled task submission in LanguageServerWrapper.start() after initializeFuture assignment and adds a regression test for this exact scenario, so the title matches the main change.

Full details: Description check

Explanation

The description explains the race in LanguageServerWrapper.start(), the reordering of the pooled task submission after initializeFuture assignment, the rejected null-check alternative, and the new test with its expected outcomes. It directly describes the changeset.

Full details: Linked Issues check

Explanation

Issue #1825 (direct) requires that an early start failure stops the server with the error, that the status does not stay "starting", and that the next start works. At the reviewed head, LanguageServerWrapper.start() assigns initializeFuture before it submits the pooled startTask (the diff moves executeOnPooledThread(startTask) to after the .exceptionally(...) chain assignment). The failure handler therefore cannot run while initializeFuture is null, on the pooled thread or on the calling thread. The handler's onError(...) and stop(context) are no longer skipped by the NullPointerException path. The added test LanguageServerWrapperRestartTest.testAStartWhichFailsBeforeStartReturnsStopsTheServer (present at head) runs the pooled task inline on the calling thread. It asserts that the server stops with serverError set, that getInitializedServer() returns null rather than throwing, and that the next start() starts the server. Under the old ordering this inline run reaches the handler before the assignment, so the test exercises the reported defect. I did not run the tests. The test result claims in the PR description are not independently verified. The source change and test together cover the coding requirements of #1825. #9 is only related context, and the PR does not claim to fix it.

Full details: Out of Scope Changes check

Explanation

The whole-PR diff from merge base e96dee9 to head 6171de6 changes two files. In LanguageServerWrapper.java, the pooled task body is unchanged. Only its wrapping as a Runnable startTask and the position of its submission changed, plus an explanatory comment. In LanguageServerWrapperRestartTest.java, a new test and its helpers (runPooledThreadTasksHere, publicInterfaces, waitUntil, and the failing-once server definition) support that test. All changes connect directly to the #1825 ordering defect. No unrelated behavior, API, or declaration change is visible in the reviewed evidence.

Full details: Docstring Coverage

Explanation

Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 2 functions across 2 files.

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

@codecov

codecov Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 66.66667% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 37.81%. Comparing base (e96dee9) to head (6171de6).
⚠️ Report is 13 commits behind head on main.

Files with missing lines Patch % Lines
.../redhat/devtools/lsp4ij/LanguageServerWrapper.java 66.66% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1827      +/-   ##
==========================================
+ Coverage   37.65%   37.81%   +0.15%     
==========================================
  Files         786      786              
  Lines       30019    30020       +1     
  Branches     6001     6001              
==========================================
+ Hits        11305    11353      +48     
+ Misses      16756    16707      -49     
- Partials     1958     1960       +2     

☔ 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 2, 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.

@angelozerr angelozerr added the bug Something isn't working label Nov 3, 2026
@angelozerr angelozerr added this to the 0.30.0 milestone Nov 3, 2026
@angelozerr angelozerr moved this to In Progress in Java Tooling Nov 3, 2026
@angelozerr
angelozerr merged commit 6bc4bc0 into redhat-developer:main Nov 3, 2026
9 checks passed
@github-project-automation github-project-automation Bot moved this from In Progress to Done in Java Tooling Nov 3, 2026
@angelozerr

Copy link
Copy Markdown
Contributor

Thanks @lagergren !

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

Status: Done

Development

Successfully merging this pull request may close these issues.

A language server whose start fails at once can stay "starting", with its error unreported

2 participants