usethesource / rascal-language-servers

An LSP server for Rascal which includes an easy-to-use LSP generator for languages implemented in Rascal, and an interactive terminal REPL.
BSD 2-Clause "Simplified" License
10 stars 7 forks source link

Races in TextDocumentState #358

Open sungshik opened 5 months ago

sungshik commented 5 months ago

Describe the bug

The fields in TextDocumentState are volatile but not race-free. For instance, in method update:

https://github.com/usethesource/rascal-language-servers/blob/3051c6b95157a563f6fba1517127a580ed7a68a9/rascal-lsp/src/main/java/org/rascalmpl/vscode/lsp/TextDocumentState.java#L63-L67

When two threads execute this method concurrently (e.g., as part of two firings of didChange), the following race can happen:

currentContent = text1; // by thread 1
currentContent = text2; // by thread 2
currentTree = newContents(text2); // by thread 2
currentTree = newContents(text1); // by thread 1
// at this point, currentContent and currentTree are inconsistent

Depending on usage patterns, the reads of these fields (via the getters) might need synchronization as well.

DavyLandman commented 4 months ago

@sungshik are there still races int he current implementation?