Skip to content

Commit 11d19e3

Browse files
Fix issue with cancellation corrupting LS state.
The problem here was as follows: 1) Host calls into the LS to do some sort of operation. 2) LS tries to synchronize with the host. 3) During synchronization we attempt to create a new program. 4) Creating the new program causes us to incrementally update some source files. 5) Incrementally updating a source file produces a new source file, and invalidates the old one. 6) *Then* the host asks to cancel this operation. 7) THe synchronization process cancels itself, leaving the LS in an inconsistent state where some of its source files have had their trees updated, but the information about the source file still thinks that we have the previous version. The fix is to not allow cancellation during host synchronization. Once we start, we have to go all the way to completion.
1 parent b277695 commit 11d19e3

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/services/services.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2022,6 +2022,12 @@ module ts {
20222022
return;
20232023
}
20242024

2025+
// IMPORTANT - It is critical from this moment onward that we do not check
2026+
// cancellation tokens. We are about to mutate source files from a previous program
2027+
// instance. If we cancel midway through, we may end up in an inconsistent state where
2028+
// the program points to old source files that have been invalidated because of
2029+
// incremental parsing.
2030+
20252031
var oldSettings = program && program.getCompilerOptions();
20262032
var newSettings = hostCache.compilationSettings();
20272033
var changesInCompilationSettingsAffectSyntax = oldSettings && oldSettings.target !== newSettings.target;
@@ -2056,8 +2062,6 @@ module ts {
20562062
return;
20572063

20582064
function getOrCreateSourceFile(fileName: string): SourceFile {
2059-
cancellationToken.throwIfCancellationRequested();
2060-
20612065
// The program is asking for this file, check first if the host can locate it.
20622066
// If the host can not locate the file, then it does not exist. return undefined
20632067
// to the program to allow reporting of errors for missing files.
@@ -5363,9 +5367,6 @@ module ts {
53635367
cancellationToken.throwIfCancellationRequested();
53645368

53655369
var fileContents = sourceFile.text;
5366-
5367-
cancellationToken.throwIfCancellationRequested();
5368-
53695370
var result: TodoComment[] = [];
53705371

53715372
if (descriptors.length > 0) {

0 commit comments

Comments
 (0)