Skip to content

[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
merged 5 commits into from
May 21, 2025

Conversation

andrewbranch
Copy link
Member

Changes project to operate with a single mutex, and avoids reading scriptInfo.Version() multiple times during parsing.

@Copilot Copilot AI review requested due to automatic review settings May 21, 2025 17:04
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR addresses data race issues on dirty state and improvements to script info version usage by consolidating mutexes and reducing redundant method calls during file parsing.

  • Replace multiple mutexes with a single mutex for project state.
  • Remove redundant symlink-related method calls in script info.
  • Cache script info version and text to avoid multiple evaluations.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
internal/project/scriptinfo.go Removed redundant calls and eliminated an unused isSymlink function.
internal/project/project.go Consolidated mutexes and refactored logging based on updated file changes.
internal/project/documentregistry.go Cached script info version and text for consistent file parsing.
Comments suppressed due to low confidence (3)

internal/project/scriptinfo.go:125

  • Ensure that removing the symlink notification call aligns with the overall design and that any side effects previously handled by this call are now covered elsewhere.
project.onFileAddedOrRemoved(s.isSymlink())

internal/project/project.go:95

  • [nitpick] Since the mutex now governs multiple aspects of project state, consider renaming 'mu' to something more descriptive (e.g., 'stateMu') to clarify its purpose.
mu                 sync.Mutex

internal/project/project.go:114

  • While consolidating mutexes simplifies synchronization, verify that p.mu adequately protects the program field to avoid contention or race conditions previously managed by programMu.
programMu         sync.Mutex

func (p *Project) onFileAddedOrRemoved(isSymlink bool) {
p.dirtyStateMu.Lock()
defer p.dirtyStateMu.Unlock()
p.hasAddedOrRemovedFiles = true
Copy link
Member Author

Choose a reason for hiding this comment

The 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.

Comment on lines +92 to +93
scriptInfoVersion := scriptInfo.Version()
scriptInfoText := scriptInfo.Text()
Copy link
Member Author

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

Copy link
Member

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?

Copy link
Member Author

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

Copy link
Member

@jakebailey jakebailey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I couldn't reproduce the issue before, but I tested and it still works for me, so 😄

Comment on lines +92 to +93
scriptInfoVersion := scriptInfo.Version()
scriptInfoText := scriptInfo.Text()
Copy link
Member

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?

Copy link
Member

@ahejlsberg ahejlsberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can confirm that this fixes the issue on Windows. 🎉

@andrewbranch andrewbranch added this pull request to the merge queue May 21, 2025
Merged via the queue into microsoft:main with commit 52190eb May 21, 2025
23 checks passed
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.

None yet

3 participants