Skip to content

internal/lsp: fix deadlocks loading lots of files at once #129

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions internal/lsp/cache/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,17 @@ func (v *view) SetContent(ctx context.Context, uri span.URI, content []byte) err
// invalidateContent invalidates the content of a Go file,
// including any position and type information that depends on it.
func (f *goFile) invalidateContent(ctx context.Context) {
f.handleMu.Lock()
defer f.handleMu.Unlock()

// Mutex acquisition order here is important. It must match the order
// in loadParseTypecheck to avoid deadlocks.
f.view.mcache.mu.Lock()
defer f.view.mcache.mu.Unlock()

f.view.pcache.mu.Lock()
defer f.view.pcache.mu.Unlock()

f.handleMu.Lock()
defer f.handleMu.Unlock()

f.invalidateAST(ctx)
f.handle = nil
}
Expand Down
10 changes: 8 additions & 2 deletions internal/lsp/cache/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,15 @@ func (w *WatchMap) Watch(key interface{}, callback func()) func() {
}

func (w *WatchMap) Notify(key interface{}) {
// Make a copy of the watcher callbacks so we don't need to hold
// the mutex during the callbacks (to avoid deadlocks).
w.mu.Lock()
defer w.mu.Unlock()
for _, entry := range w.watchers[key] {
entries := w.watchers[key]
entriesCopy := make([]watcher, len(entries))
copy(entriesCopy, entries)
w.mu.Unlock()

for _, entry := range entriesCopy {
entry.callback()
}
}