Skip to content

fix: ask for a document's didOpen outside documentsLock, so that connecting cannot deadlock with stop() - #1751

Merged
angelozerr merged 1 commit into
redhat-developer:mainfrom
lagergren:fix/connect-document-lock-order
Oct 20, 2026
Merged

angelozerr merged 1 commit into
redhat-developer:mainfrom
lagergren:fix/connect-document-lock-order

Conversation

@lagergren

Copy link
Copy Markdown
Collaborator

Fixes a deadlock between LanguageServerWrapper.stop() and a document that connects at the same time, which I found while rebasing #1749 (see my comment there). When stop() runs on the EDT, which is common, IntelliJ freezes.

The deadlock

stop() holds the wrapper's monitor and then takes documentsLock. connectDocument took them the other way round. Under documentsLock it asked for the document's didOpen, and the first such request reaches the synchronized start():

DocumentContentSynchronizer.getDidOpenFuture → getDidOpenFutureSync → LanguageServerWrapper.getInitializedServer → start()

A thread dump from the hung check run:

"AWT-EventQueue-0": LanguageServerWrapper.stop(LanguageServerWrapper.java:1909)
    waiting to lock documentsLock, holding the LanguageServerWrapper
"ForkJoinPool.commonPool-worker-3": LanguageServerWrapper.start
    <- getInitializedServer <- DocumentContentSynchronizer.getDidOpenFutureSync
    <- getLanguageServerWhenDidOpen <- lambda$connectDocument (LanguageServerWrapper.java:906)
    waiting to lock the LanguageServerWrapper, holding documentsLock
Found 1 deadlock.

The lock order is old: getDidOpenFutureSync last changed in May 2025.

The fix

connectDocument now only looks up, or creates and registers, the document's synchronizer under documentsLock, and asks for its didOpen after releasing it. It did so in two places, for a document already connected and for one it has just connected, and both are changed. The code already avoided this once: it calls getInitializedServer() before taking documentsLock, for the workspace folder notification, with a comment explaining why.

If stop() closes the document in between, the didOpen request fails as for a document closed before it was opened, and the caller already handles that.

Test

New ConnectDuringStopTest:

  • It holds the wrapper's monitor, as stop() does, and connects a second document.
  • It waits until the pooled thread connecting that document blocks on the monitor in getDidOpenFutureSync.
  • That thread must not hold documentsLock, which the test then takes, as stop() does.

On the previous code the thread holds documentsLock. The test then fails before taking it, so it reports the deadlock instead of hanging the build.

./gradlew check: 654 tests, 0 failures, the 3 existing skips.

…ecting cannot deadlock with stop()

stop() holds the wrapper's monitor and then takes documentsLock.
Connecting a document did it the other way round: under documentsLock it
asked for the document's didOpen, and the first such request reaches the
synchronized start() (DocumentContentSynchronizer.getDidOpenFuture ->
getDidOpenFutureSync -> LanguageServerWrapper.getInitializedServer). A
stop() while a document connects on a pooled thread therefore deadlocked
both threads, and stop() is often called on the EDT, which then froze. A
thread dump from a check run of redhat-developer#1749:

  "AWT-EventQueue-0": LanguageServerWrapper.stop, holding the wrapper,
    waiting for documentsLock
  "ForkJoinPool.commonPool-worker-3": LanguageServerWrapper.start <-
    getInitializedServer <- DocumentContentSynchronizer.getDidOpenFutureSync
    <- getLanguageServerWhenDidOpen <- connectDocument, holding
    documentsLock, waiting for the wrapper

connectDocument now only looks up, or creates and registers, the
document's synchronizer under documentsLock, and asks for its didOpen
after releasing it, at both places it did so: for a document already
connected, and for one it has just connected. A didOpen for a document
that stop() closes in between fails as a document closed before it was
opened, which the caller already handles.

Test: ConnectDuringStopTest holds the wrapper's monitor, as stop() does,
connects a second document, and waits until the pooled thread connecting
it blocks on the monitor in getDidOpenFutureSync. That thread must not
hold documentsLock, which the test then takes, as stop() does. On the
previous code the thread holds it, and the test fails without taking it,
so it reports the deadlock instead of hanging.
@coderabbitai

coderabbitai Bot commented Oct 20, 2026

Copy link
Copy Markdown

Warning

Review limit reached

This review ran on the open-source allowance, not this organization's plan, because the pull request author doesn't have an assigned seat. Waiting won't change this — ask an organization admin to assign them a seat, or add seats in Billing if every seat is already assigned, then retry.

Next included review available in 18 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 2 included reviews currently available.

Learn how review limits work.

Review configuration:

⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 8940a270-231f-49f7-954f-d273b95b59c6
📥 Commits

Reviewing files that changed from the base of the PR and between ca60a68 and f79d853.

📒 Files selected for processing: 2
  • src/main/java/com/redhat/devtools/lsp4ij/LanguageServerWrapper.java
  • src/test/java/com/redhat/devtools/lsp4ij/ConnectDuringStopTest.java

Comment @coderabbitai help to get the list of available commands.

@codecov

codecov Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 86.95652% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 30.73%. Comparing base (ca60a68) to head (f79d853).
⚠️ Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
.../redhat/devtools/lsp4ij/LanguageServerWrapper.java 86.95% 1 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1751      +/-   ##
==========================================
- Coverage   30.73%   30.73%   -0.01%     
==========================================
  Files         749      749              
  Lines       27337    27334       -3     
  Branches     4115     5422    +1307     
==========================================
- Hits         8402     8400       -2     
- Misses      17410    17413       +3     
+ Partials     1525     1521       -4     

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

@angelozerr angelozerr added this to the 0.21.1 milestone Oct 20, 2026
@angelozerr angelozerr added the bug Something isn't working label Oct 20, 2026
@angelozerr angelozerr moved this to In Progress in Java Tooling Oct 20, 2026
@angelozerr
angelozerr merged commit 0c0f4bf into redhat-developer:main Oct 20, 2026
9 checks passed
@github-project-automation github-project-automation Bot moved this from In Progress to Done in Java Tooling Oct 20, 2026
@lagergren
lagergren deleted the fix/connect-document-lock-order branch October 26, 2026 21:43
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.

2 participants