Skip to content

An edit made while a file connects to a starting language server never reaches the server #2126

Closed

Description

@lagergren

What happens

LSP4IJ opens a file at a language server with the text the file had when the connection was asked for, but listens to the file's document only once the server has initialized. An edit made in between never reaches the server:

  • the server keeps the text without the edit;
  • with incremental sync, every later change applies to text the server doesn't have, so the server's copy of the file stays wrong until the file is opened again;
  • with full sync, the next edit sends the whole text and repairs it.

The window is the start of the language server, so it is widest after a restart, or when the server's process died and the next request starts it again. We hit it in a test that edits a file right after killing the server's process: the new server opened the file without the edit, never got it, and so reported nothing about it.

Steps

  1. Open a file of a language server that syncs incrementally (TextDocumentSyncKind.Incremental) and is slow to start.
  2. Kill the server's process. The next request in the file, such as a hover, makes LSP4IJ start the server again.
  3. Type in the file while the server starts.
  4. Look at the didOpen and didChange in the LSP console.

Expected

The server gets the edit: the didOpen carries the current text, or a didChange with the edit follows it.

Cause

On main (40c958d):

  • LanguageServiceAccessor.getFileConnectionInfoMap captures the document's text and modification stamp with createFileConnectionInfo (LanguageServiceAccessor.java:204), and then connects.
  • LanguageServerWrapper.connectDocument waits for the server's initialize (initialized.thenComposeAsync, LanguageServerWrapper.java:1235). Only then does it create the DocumentContentSynchronizer and add it as the document's listener (LanguageServerWrapper.java:1317-1322).
  • DocumentContentSynchronizer.getDidOpenFutureSync sends the captured text (DocumentContentSynchronizer.java:164). No listener heard the edits made between the capture and the connection, so none of them is queued.
  • isCurrentTextAssigned (DocumentContentSynchronizer.java:639) already sees the mismatch: the document's stamp differs from the didOpen's. Since fix: validate document versions before applying workspace edits #1698 a versioned workspace edit for that version is refused, but nothing sends the text.

The text is captured early on purpose (#878, 915ca20): a didOpen that reads the document needs read access, and must not wait for a write action, as for the move of a file. The move of a document captures its text the same way (LSPFileListener.java:265). Its window is shorter, as its server already runs.

Suggested fix

In getDidOpenFutureSync, use the captured text only while the document still has the captured stamp. Otherwise read the current text, as when no text was captured. The didOpen then reads only when the document changed after the capture.

That leaves a much smaller window of the same kind: a change that is under way while the listener is added, before it has updated the stamp. On 2024.2 and 2025.3 the listener hears only its documentChanged, which queues no incremental change. On 2026.3, which takes the listeners once for both events, it hears neither.

Related

#1698, #878

Activity

  1. added a commit that references this issue on Nov 23, 2026
  2. added
    bug
    on Nov 23, 2026
  3. added this to the 0.30.2 milestone on Nov 23, 2026
  4. added a commit that references this issue on Nov 23, 2026
    0f8c24c
  5. 3 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions