Repository navigation
Closed
Description
I've run into this a few times, most specifically if I try to use the Previous/Next Method actions -- which are based on textDocument/documentSymbol information -- before the language server for the file is started. Here's an example stack trace from the freeze:
"AWT-EventQueue-0@3310" tid=0x42 nid=NA runnable
java.lang.Thread.State: RUNNABLE
at jdk.internal.misc.Unsafe.park(Unsafe.java:-1)
at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:269)
at java.util.concurrent.CompletableFuture$Signaller.block(CompletableFuture.java:1866)
at java.util.concurrent.ForkJoinPool.unmanagedBlock(ForkJoinPool.java:4013)
at java.util.concurrent.ForkJoinPool.managedBlock(ForkJoinPool.java:3961)
at java.util.concurrent.CompletableFuture.timedGet(CompletableFuture.java:1939)
at java.util.concurrent.CompletableFuture.get(CompletableFuture.java:2095)
at com.redhat.devtools.lsp4ij.internal.CompletableFutures.waitUntilDone(CompletableFutures.java:143)
at com.redhat.devtools.lsp4ij.internal.CompletableFutures.waitUntilDone(CompletableFutures.java:110)
at com.redhat.devtools.lsp4ij.features.documentSymbol.LSPDocumentSymbolStructureViewModel$LSPFileStructureViewElement.collectElements(LSPDocumentSymbolStructureViewModel.java:107)
at com.redhat.devtools.lsp4ij.features.documentSymbol.LSPDocumentSymbolStructureViewModel$LSPFileStructureViewElement.getChildrenBase(LSPDocumentSymbolStructureViewModel.java:82)
at com.intellij.ide.structureView.impl.common.PsiTreeElementBase.doGetChildren(PsiTreeElementBase.java:85)
at com.intellij.ide.structureView.impl.common.PsiTreeElementBase.getChildren(PsiTreeElementBase.java:74)
at com.intellij.ide.structureView.impl.common.PsiTreeElementBase.getChildren(PsiTreeElementBase.java:23)
at com.intellij.codeInsight.navigation.MethodUpDownUtil.addStructureViewElements(MethodUpDownUtil.java:62)
at com.intellij.codeInsight.navigation.MethodUpDownUtil.addNavigationElements(MethodUpDownUtil.java:53)
at com.intellij.codeInsight.navigation.MethodUpDownUtil.getNavigationOffsets(MethodUpDownUtil.java:33)
at com.intellij.codeInsight.navigation.MethodDownHandler.invoke(MethodDownHandler.java:25)
I see (at least) two aspects to this issue:
- LSP4IJ should never wait indefinitely for futures to finish from the EDT, and it is here. It seems that
CompleteableFutures#waitUntilDone()should use a (very?) short default timeout if presented anullvalue fortimeoutwhen invoked on the EDT. LSPDocumentSymbolStructureViewModel.LSPFileStructureViewElement#collectElements()might perhaps check whether it's invoked on the EDT and, if so, if any language server for the file isn't yet in astartedstate, it should return an empty list just as it does if indexing.
I have a small, shelved set of changes to address exactly these two items, but it's possible that I'm missing some part of the larger picture in terms of how/when language servers should be automatically started and the relationship of that behavior with the EDT, so I'm logging this issue as a way of discussing and delivering the correct resolution.
Metadata
Metadata
Assignees
Labels
bugSomething isn't working
Type
Projects
- StatusShow more project fieldsDone
Activity
[-]Deadlock when language server is started from the EDT[/-][+]Freeze when language server is started from the EDT[/+]angelozerr commentedon Mar 7, 2025
It is a super annoying problem.
Is there any chance that you create a draft in order to I understand how you would like to fix this issue please.
angelozerr commentedon Mar 7, 2025
@SCWells72 do you think you could create a test which reproduce this super annoying issue?
angelozerr commentedon Mar 8, 2025
When you say "freeze", it freezes indefinitely or just few some milliseconds?
SCWells72 commentedon Mar 8, 2025
Indefinite freeze of the app. For that reason you wouldn't want a unit test that reproduces it, of course. It's pretty easy to reproduce. Just press Alt+Up/Down (Windows/Linux) in a source file before the associated language server has started.
I think that, at a minimum my first recommendation of not allowing
waitUntilDone()anulltimeout should be implemented. Some default timeout would need to be selected, and that would still potentially lead to slight pauses on the EDT until the language server is started, but at least it wouldn't lock up the entire app.angelozerr commentedon Mar 8, 2025
I would like really avoid using timeout because it will require to adjust timeout for each lsp request. For instance workspace symbol can takes many times compare to documentHighlight.
If we use timeout, user will need to adjust timeout for a given language server which is IMHO boring.
vscode works without timeout, my goal is to have this same feature.
It seems you can reproduce easily the problem, that is super nice. If freezes is infinite it means that there is a bug somewhere when language is starting.
Hope I can reproduce easily on my side to understand what it happens. Perhaps there is a problem with ReadAction.compute which blocks the start of the language server?
SCWells72 commentedon Mar 8, 2025
With what I've proposed, the timeout is automatically added in
waitUntilDone()whentimeoutisnulland execution is on the EDT. There's no ripple effect to callers aside from potentially handlingTimeoutExceptionbetter.VS Code isn't implemented in Swing which is single-threaded with special considerations for the EDT or potentially risking temporary or even (as in this case) permanent UI freezes due to deadlock on otherwise disjoint resources.
Yes, it's trivial to reproduce, and it's 100% about actions that initiate on the EDT and could result in starting a language server. LSP4IJ just need to ensure that that particular behavior is never allowed to happen...
...or, as you said here, perhaps the issue is in how the language server is started and the relationship between that thread and the EDT that's resulting in this effective deadlock.
angelozerr commentedon Mar 8, 2025
Indeed we could do that to be safe but I would like really understand the problem. Why infinite freeze?
Indeed but mu challenge was to hide this edt complexity and try to do the same thing than vscode.
My fear with timeout is to loose some lsp features.
If you can write a test with this behaviour it would be very nice.
I suspect that there is a bug somewhere. I could understand that there is a freeze but not infinite. I fear that fixing this issue just with timeout will hide a poyential bug.
SCWells72 commentedon Mar 8, 2025
It's pretty much impossible to hide/ignore EDT complexity in Swing programming, even more so in the JetBrains plugin SDK which is constantly verifying thread state. It's best to work within the constraints/behaviors of the actual framework.
As I said, I could perhaps(/likely?) write a test that reproduces this behavior...and that test would never finish. Probably not what we want until it's being used to verify the fix. But it should be pretty simple...just invoke the Next Method or Previous Method action before the language server has been started such that the action results in trying to start the language server.
I agree that it would be best to understand/fix the issue at its root cause and not add workarounds. I'm sure you're aware of this, but just in case, it's unfortunately pretty easy to introduce deadlocks in Swing programming by having the EDT waiting on some thread state and another thread waiting on the EDT, e.g., via
invokeAndWait(). Different types of mutexes, but the same general type of mutual wait that will never complete.Hopefully with what I've provided, you'll be able to reproduce this quite easily. Then just get a thread dump and see if you can make sense of the resulting state...and more to the point, ideally find a way to fix/avoid it.
angelozerr commentedon Mar 8, 2025
I trust you.
We could write test which does that and stop the test after some times. Without the fix, test will be cancelled and withbthe fix (hope I will find it) it will execute the test.
If you could help me to write this test, it will help me.
No :) thanks for your great explanation!
I hope.
angelozerr commentedon Mar 9, 2025
Could you explain me please where I can find those actions?
angelozerr commentedon Mar 9, 2025
I can reproduce it and I have found the problem. Let me try to do a fix.
fix: Freeze when language server is started from the EDT
fix: Freeze when language server is started from the EDT
9 remaining items
fix: Freeze when language server is started from the EDT
fix: Freeze when language server is started from the EDT
fix: Freeze when language server is started from the EDT
fix: Freeze when language server is started from the EDT
fix: Freeze when language server is started from the EDT
fix: open a file at a starting language server with the edits made wh…