Skip to content

Fix use-before-initialize (NPE) in TextDocumentState#1055

Merged
DavyLandman merged 4 commits intomainfrom
fix/use-before-init-in-textdocumentstate
Apr 15, 2026
Merged

Fix use-before-initialize (NPE) in TextDocumentState#1055
DavyLandman merged 4 commits intomainfrom
fix/use-before-init-in-textdocumentstate

Conversation

@sungshik
Copy link
Copy Markdown
Contributor

@sungshik sungshik commented Apr 15, 2026

The UI tests on Ubuntu have become flaky (#1034 and #1037). This PR fixes a bug in the constructor of TextDocumentState to remove one cause of the flakiness.

Problem

Before this PR, the constructor of TextDocumentState:

  1. Creates an initial Update object. The constructor of Update:
    1. Submits the parse call for the initial content to the worker pool.
    2. After parsing, stores the tree in an AtomicReference field of TextDocumentState.
  2. Initializes an AtomicReference object to store a tree after parsing.

Due to scheduling, it could happen that step 1.ii was executed before step 2. In that case, an NPE occurs, as an attempt is made to set the AtomicReference with the tree, but it hasn't been initialized yet. This NPE is swallowed inside a completion stage, though, so only the fallout (of not having stored the tree) is observable -- this is what happened in the UI tests.

Solution

This PR:

  • Makes sure that the AtomicReference object is initialized before use. This addresses the root cause of the issue.
  • Makes sure that the post-parsing logic is executed in the worker pool (instead of the request pool). This doesn't address the root cause of the issue, but it is adjacent to it.
  • Improves exception reporting to make future debugging easier.

Related PRs (subsumed by this PR):

sungshik and others added 3 commits April 15, 2026 09:41
…roperly initialized yet when an inner class instance (`Update`) was created. The inner class instance would use an uninitialized field of the outer class instance, resulting in an NPE.
…xplicitly executed on the worker pool yet (but possibly on the request pool)
Comment thread rascal-lsp/src/main/java/org/rascalmpl/vscode/lsp/TextDocumentState.java Outdated
…State.java

Co-authored-by: Toine Hartman <toinehartman@users.noreply.github.com>
@sonarqubecloud
Copy link
Copy Markdown

@DavyLandman DavyLandman merged commit 6033ab0 into main Apr 15, 2026
15 checks passed
@sungshik sungshik deleted the fix/use-before-init-in-textdocumentstate branch April 15, 2026 09:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants