Repository navigation
fix: stop a language server whose start fails before its initialize future is set - #1827
Conversation
…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).
|
No actionable comments were generated in the recent review.
|
| 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: 
4 · 
1
-
Failed checks: 1 warning Passed checks: 4 passed-
Check name Status Explanation Title check 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 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 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 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 aRunnable startTaskand 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()assignsinitializeFuturebefore it submits the pooledstartTask(the diff movesexecuteOnPooledThread(startTask)to after the.exceptionally(...)chain assignment). The failure handler therefore cannot run whileinitializeFutureis null, on the pooled thread or on the calling thread. The handler'sonError(...)andstop(context)are no longer skipped by the NullPointerException path. The added testLanguageServerWrapperRestartTest.testAStartWhichFailsBeforeStartReturnsStopsTheServer(present at head) runs the pooled task inline on the calling thread. It asserts that the server stops withserverErrorset, thatgetInitializedServer()returns null rather than throwing, and that the nextstart()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.#9is 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 aRunnable startTaskand the position of its submission changed, plus an explanatory comment. InLanguageServerWrapperRestartTest.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#1825ordering 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 Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
|
@coderabbitai review |
Action performed
|
|
Thanks @lagergren ! |
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 nextstart().start()hands the creation of the connection to a pooled thread, then builds the future chain onstartFuture, with the failure handler (.exceptionally(...)) at its end, and assigns the chain toinitializeFuture. Until itssynchronized (this)block, the pooled thread does not wait for the monitor thatstart()holds (once the client features exist, whichLanguageServiceAccessorcreates before it starts a server). So a start which fails there, such as aStreamConnectionProviderwhosestart()throws, can run in this order:start()submits the pooled task;startFutureexceptionally;start()attaches the chain to the completedstartFuture, 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);serverError, shows the error notification, and callsinitializeFuture.completeExceptionally(serverError)whileinitializeFutureis still null.The
NullPointerExceptionbecomes the result of the.exceptionally(...)future, whichstart()then assigns toinitializeFuture, and nothing logs it. The handler'sonError()andstop(context)are skipped, so the status stays "starting", the connection provider is not stopped, and a caller ofgetInitializedServer()gets theNullPointerExceptioninstead of the start error. The nextstart()recovers, becauseserverErroris 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 aRunnable, whichstart()submits after it has assignedinitializeFuture, instead of before it builds the chain. Every stage then attaches to astartFuturethat 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)clearsinitializeFuture, whichstart()would then set again, to a done future that no laterstop()clears, so no laterstart()would start the server.Tests
LanguageServerWrapperRestartTest.testAStartWhichFailsBeforeStartReturnsStopsTheServerruns the pooled task on the calling thread, beforeexecuteOnPooledThreadreturns, through a proxy of the application which delegates every other call. So its first start, whoseStreamConnectionProvider.start()throws, fails beforestart()returns. The test checks that:getInitializedServer()gets no server, not aNullPointerException;Without the change, the test fails: the server stays "starting", so the test times out, and
getInitializedServer()fails withNullPointerException: 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,NotificationOrderTestand 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
maintoo. Not checked in a running IDE.