-
Notifications
You must be signed in to change notification settings - Fork 650
[LSP] Fix data races on dirty state and script info version #897
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
andrewbranch
merged 5 commits into
microsoft:main
from
andrewbranch:bug/lsp-concurrency
May 21, 2025
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ac44c95
[LSP] Fix data races on dirty state and script info version
andrewbranch 79fbc2b
updateGraph and updateIfDirty are the same thing
andrewbranch 253516a
Load config without marking as dirty during updateGraph
andrewbranch 696806f
Fix remaining locks
andrewbranch 9dbf06f
Log updateGraph time
andrewbranch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ import ( | |
"slices" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
"github.com/microsoft/typescript-go/internal/ast" | ||
"github.com/microsoft/typescript-go/internal/collections" | ||
|
@@ -92,15 +93,13 @@ type Project struct { | |
name string | ||
kind Kind | ||
|
||
dirtyStateMu sync.Mutex | ||
initialLoadPending bool | ||
dirty bool | ||
version int | ||
hasAddedOrRemovedFiles bool | ||
hasAddedOrRemovedSymlinks bool | ||
deferredClose bool | ||
pendingReload PendingReload | ||
dirtyFilePath tspath.Path | ||
mu sync.Mutex | ||
initialLoadPending bool | ||
dirty bool | ||
version int | ||
deferredClose bool | ||
pendingReload PendingReload | ||
dirtyFilePath tspath.Path | ||
|
||
comparePathsOptions tspath.ComparePathsOptions | ||
currentDirectory string | ||
|
@@ -114,7 +113,6 @@ type Project struct { | |
rootFileNames *collections.OrderedMap[tspath.Path, string] | ||
compilerOptions *core.CompilerOptions | ||
parsedCommandLine *tsoptions.ParsedCommandLine | ||
programMu sync.Mutex | ||
program *compiler.Program | ||
checkerPool *checkerPool | ||
|
||
|
@@ -211,7 +209,7 @@ func (p *Project) GetSourceFile(fileName string, path tspath.Path, languageVersi | |
|
||
// Updates the program if needed. | ||
func (p *Project) GetProgram() *compiler.Program { | ||
p.updateIfDirty() | ||
p.updateGraph() | ||
return p.program | ||
} | ||
|
||
|
@@ -372,9 +370,9 @@ func (p *Project) getScriptKind(fileName string) core.ScriptKind { | |
return core.GetScriptKindFromFileName(fileName) | ||
} | ||
|
||
func (p *Project) markFileAsDirty(path tspath.Path) { | ||
p.dirtyStateMu.Lock() | ||
defer p.dirtyStateMu.Unlock() | ||
func (p *Project) MarkFileAsDirty(path tspath.Path) { | ||
p.mu.Lock() | ||
defer p.mu.Unlock() | ||
if !p.dirty { | ||
p.dirty = true | ||
p.dirtyFilePath = path | ||
|
@@ -385,69 +383,57 @@ func (p *Project) markFileAsDirty(path tspath.Path) { | |
} | ||
|
||
func (p *Project) markAsDirty() { | ||
p.dirtyStateMu.Lock() | ||
defer p.dirtyStateMu.Unlock() | ||
p.mu.Lock() | ||
defer p.mu.Unlock() | ||
p.markAsDirtyLocked() | ||
} | ||
|
||
func (p *Project) markAsDirtyLocked() { | ||
p.dirtyFilePath = "" | ||
if !p.dirty { | ||
p.dirty = true | ||
p.version++ | ||
} | ||
} | ||
|
||
// updateIfDirty returns true if the project was updated. | ||
func (p *Project) updateIfDirty() bool { | ||
// !!! p.invalidateResolutionsOfFailedLookupLocations() | ||
return p.dirty && p.updateGraph() | ||
} | ||
|
||
func (p *Project) onFileAddedOrRemoved(isSymlink bool) { | ||
p.dirtyStateMu.Lock() | ||
defer p.dirtyStateMu.Unlock() | ||
p.hasAddedOrRemovedFiles = true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These weren’t yet used for anything but logging, and they complicate the locking because they are sometimes, but not always, called during program construction when the mutex is already locked. |
||
if isSymlink { | ||
p.hasAddedOrRemovedSymlinks = true | ||
} | ||
} | ||
|
||
// updateGraph updates the set of files that contribute to the project. | ||
// Returns true if the set of files in has changed. NOTE: this is the | ||
// opposite of the return value in Strada, which was frequently inverted, | ||
// as in `updateProjectIfDirty()`. | ||
func (p *Project) updateGraph() bool { | ||
p.programMu.Lock() | ||
defer p.programMu.Unlock() | ||
p.mu.Lock() | ||
defer p.mu.Unlock() | ||
|
||
if !p.dirty { | ||
return false | ||
} | ||
|
||
start := time.Now() | ||
p.log("Starting updateGraph: Project: " + p.name) | ||
var writeFileNames bool | ||
oldProgram := p.program | ||
hasAddedOrRemovedFiles := p.hasAddedOrRemovedFiles | ||
p.initialLoadPending = false | ||
|
||
if p.kind == KindConfigured && p.pendingReload != PendingReloadNone { | ||
switch p.pendingReload { | ||
case PendingReloadFileNames: | ||
p.parsedCommandLine = tsoptions.ReloadFileNamesOfParsedCommandLine(p.parsedCommandLine, p.host.FS()) | ||
p.setRootFiles(p.parsedCommandLine.FileNames()) | ||
writeFileNames = p.setRootFiles(p.parsedCommandLine.FileNames()) | ||
case PendingReloadFull: | ||
if err := p.LoadConfig(); err != nil { | ||
if err := p.loadConfig(); err != nil { | ||
panic(fmt.Sprintf("failed to reload config: %v", err)) | ||
} | ||
} | ||
p.pendingReload = PendingReloadNone | ||
} | ||
|
||
p.hasAddedOrRemovedFiles = false | ||
p.hasAddedOrRemovedSymlinks = false | ||
oldProgramReused := p.updateProgram() | ||
p.dirty = false | ||
p.dirtyFilePath = "" | ||
p.log(fmt.Sprintf("Finishing updateGraph: Project: %s version: %d", p.name, p.version)) | ||
if hasAddedOrRemovedFiles { | ||
if writeFileNames { | ||
p.log(p.print(true /*writeFileNames*/, true /*writeFileExplanation*/, false /*writeFileVersionAndText*/)) | ||
} else if p.program != oldProgram { | ||
p.log("Different program with same set of files") | ||
p.log("Different program with same set of root files") | ||
} | ||
if !oldProgramReused { | ||
if oldProgram != nil { | ||
|
@@ -461,6 +447,7 @@ func (p *Project) updateGraph() bool { | |
// but in Strada we throttle, so at least sometimes this should be considered top-level? | ||
p.updateWatchers(context.TODO()) | ||
} | ||
p.log(fmt.Sprintf("Finishing updateGraph: Project: %s version: %d in %s", p.name, p.version, time.Since(start))) | ||
return true | ||
} | ||
|
||
|
@@ -509,6 +496,13 @@ func (p *Project) isRoot(info *ScriptInfo) bool { | |
return p.rootFileNames.Has(info.path) | ||
} | ||
|
||
func (p *Project) RemoveFile(info *ScriptInfo, fileExists bool, detachFromProject bool) { | ||
p.mu.Lock() | ||
defer p.mu.Unlock() | ||
p.removeFile(info, fileExists, detachFromProject) | ||
p.markAsDirtyLocked() | ||
} | ||
|
||
func (p *Project) removeFile(info *ScriptInfo, fileExists bool, detachFromProject bool) { | ||
if p.isRoot(info) { | ||
switch p.kind { | ||
|
@@ -530,7 +524,13 @@ func (p *Project) removeFile(info *ScriptInfo, fileExists bool, detachFromProjec | |
if detachFromProject { | ||
info.detachFromProject(p) | ||
} | ||
p.markAsDirty() | ||
} | ||
|
||
func (p *Project) AddRoot(info *ScriptInfo) { | ||
p.mu.Lock() | ||
defer p.mu.Unlock() | ||
p.addRoot(info) | ||
p.markAsDirtyLocked() | ||
} | ||
|
||
func (p *Project) addRoot(info *ScriptInfo) { | ||
|
@@ -544,10 +544,17 @@ func (p *Project) addRoot(info *ScriptInfo) { | |
} | ||
p.rootFileNames.Set(info.path, info.fileName) | ||
info.attachToProject(p) | ||
p.markAsDirty() | ||
} | ||
|
||
func (p *Project) LoadConfig() error { | ||
if err := p.loadConfig(); err != nil { | ||
return err | ||
} | ||
p.markAsDirty() | ||
return nil | ||
} | ||
|
||
func (p *Project) loadConfig() error { | ||
if p.kind != KindConfigured { | ||
panic("loadConfig called on non-configured project") | ||
} | ||
|
@@ -582,12 +589,12 @@ func (p *Project) LoadConfig() error { | |
p.compilerOptions = &core.CompilerOptions{} | ||
return fmt.Errorf("could not read file %q", p.configFileName) | ||
} | ||
|
||
p.markAsDirty() | ||
return nil | ||
} | ||
|
||
func (p *Project) setRootFiles(rootFileNames []string) { | ||
// setRootFiles returns true if the set of root files has changed. | ||
func (p *Project) setRootFiles(rootFileNames []string) bool { | ||
var hasChanged bool | ||
newRootScriptInfos := make(map[tspath.Path]struct{}, len(rootFileNames)) | ||
for _, file := range rootFileNames { | ||
scriptKind := p.getScriptKind(file) | ||
|
@@ -597,6 +604,7 @@ func (p *Project) setRootFiles(rootFileNames []string) { | |
scriptInfo := p.host.GetOrCreateScriptInfoForFile(file, path, scriptKind) | ||
newRootScriptInfos[path] = struct{}{} | ||
isAlreadyRoot := p.rootFileNames.Has(path) | ||
hasChanged = hasChanged || !isAlreadyRoot | ||
|
||
if !isAlreadyRoot && scriptInfo != nil { | ||
p.addRoot(scriptInfo) | ||
|
@@ -610,6 +618,7 @@ func (p *Project) setRootFiles(rootFileNames []string) { | |
} | ||
|
||
if p.rootFileNames.Size() > len(rootFileNames) { | ||
hasChanged = true | ||
for root := range p.rootFileNames.Keys() { | ||
if _, ok := newRootScriptInfos[root]; !ok { | ||
if info := p.host.GetScriptInfoByPath(root); info != nil { | ||
|
@@ -620,6 +629,7 @@ func (p *Project) setRootFiles(rootFileNames []string) { | |
} | ||
} | ||
} | ||
return hasChanged | ||
} | ||
|
||
func (p *Project) clearSourceMapperCache() { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically this should be one atomic access
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you want to address this now or later?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just tried protecting all the ScriptInfo fields with a mutex and accessing correlated fields within the same lock, but that had a ~20% penalty on project initialization