Repository navigation
fix: ask for a document's didOpen outside documentsLock, so that connecting cannot deadlock with stop() - #1751
Conversation
…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.
|
Warning Review limit reachedThis 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. View limit details
Comment |
Codecov Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
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). Whenstop()runs on the EDT, which is common, IntelliJ freezes.The deadlock
stop()holds the wrapper's monitor and then takesdocumentsLock.connectDocumenttook them the other way round. UnderdocumentsLockit asked for the document'sdidOpen, and the first such request reaches the synchronizedstart():DocumentContentSynchronizer.getDidOpenFuture→getDidOpenFutureSync→LanguageServerWrapper.getInitializedServer→start()A thread dump from the hung check run:
The lock order is old:
getDidOpenFutureSynclast changed in May 2025.The fix
connectDocumentnow only looks up, or creates and registers, the document's synchronizer underdocumentsLock, and asks for itsdidOpenafter 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 callsgetInitializedServer()before takingdocumentsLock, for the workspace folder notification, with a comment explaining why.If
stop()closes the document in between, thedidOpenrequest fails as for a document closed before it was opened, and the caller already handles that.Test
New
ConnectDuringStopTest:stop()does, and connects a second document.getDidOpenFutureSync.documentsLock, which the test then takes, asstop()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.