Skip to content

Freeze when language server is started from the EDT #878

Closed

Description

@SCWells72

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:

  1. 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 a null value for timeout when invoked on the EDT.
  2. 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 a started state, 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.

Activity

  1. changed the title [-]Deadlock when language server is started from the EDT[/-] [+]Freeze when language server is started from the EDT[/+] on Mar 4, 2025
  2. angelozerr commented on Mar 7, 2025

    @angelozerr
    Contributor

    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.

  3. added
    bug
    on Mar 7, 2025
  4. angelozerr commented on Mar 7, 2025

    @angelozerr
    Contributor

    @SCWells72 do you think you could create a test which reproduce this super annoying issue?

  5. angelozerr commented on Mar 8, 2025

    @angelozerr
    Contributor

    @SCWells72 do you think you could create a test which reproduce this super annoying issue?

    When you say "freeze", it freezes indefinitely or just few some milliseconds?

  6. SCWells72 commented on Mar 8, 2025

    @SCWells72
    ContributorAuthor

    When you say "freeze", it freezes indefinitely or just few some milliseconds?

    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() a null timeout 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.

  7. angelozerr commented on Mar 8, 2025

    @angelozerr
    Contributor

    When you say "freeze", it freezes indefinitely or just few some milliseconds?

    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() a null timeout 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.

    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?

  8. SCWells72 commented on Mar 8, 2025

    @SCWells72
    ContributorAuthor

    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.

    With what I've proposed, the timeout is automatically added in waitUntilDone() when timeout is null and execution is on the EDT. There's no ripple effect to callers aside from potentially handling TimeoutException better.

    vscode works without timeout, my goal is to have this same feature.

    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.

    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.

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

    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?

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

  9. angelozerr commented on Mar 8, 2025

    @angelozerr
    Contributor

    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.

    With what I've proposed, the timeout is automatically added in waitUntilDone() when timeout is null and execution is on the EDT. There's no ripple effect to callers aside from potentially handling TimeoutException better.

    Indeed we could do that to be safe but I would like really understand the problem. Why infinite freeze?

    vscode works without timeout, my goal is to have this same feature.

    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.

    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.

    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.

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

    If you can write a test with this behaviour it would be very nice.

    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?

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

    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.

  10. SCWells72 commented on Mar 8, 2025

    @SCWells72
    ContributorAuthor

    Indeed but mu challenge was to hide this edt complexity and try to do the same thing than vscode.

    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.

    If you can write a test with this behaviour it would be very nice.

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

    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.

  11. angelozerr commented on Mar 8, 2025

    @angelozerr
    Contributor

    Indeed but mu challenge was to hide this edt complexity and try to do the same thing than vscode.

    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.

    I trust you.

    If you can write a test with this behaviour it would be very nice.

    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.

    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.

    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.

    If you could help me to write this test, it will help me.

    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.

    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.

    No :) thanks for your great explanation!

    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.

    I hope.

  12. angelozerr commented on Mar 9, 2025

    @angelozerr
    Contributor

    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.

    Could you explain me please where I can find those actions?

  13. angelozerr commented on Mar 9, 2025

    @angelozerr
    Contributor

    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.

    Could you explain me please where I can find those actions?

    I can reproduce it and I have found the problem. Let me try to do a fix.

  14. added 2 commits that reference this issue on Mar 9, 2025
    4e127d0
    4e9f63e
  15. 9 remaining items

  16. self-assigned this
    on Mar 11, 2025
  17. modified the milestones: 0.0.1, 0.12.0 on Mar 11, 2025
  18. moved this to In Progress in Java Toolingon Mar 12, 2025
  19. added 4 commits that reference this issue on Mar 14, 2025
    a0ed78d
    32a061c
    362048b
    b9ec872
  20. moved this from In Progress to Done in Java Toolingon Mar 15, 2025
  21. added a commit that references this issue on Mar 16, 2025
    915ca20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions