From a00916dd39a53f9d7c0617efa74ac2aaf6bc4716 Mon Sep 17 00:00:00 2001 From: Muir Manders Date: Wed, 3 Jul 2019 16:46:23 +0000 Subject: [PATCH] internal/lsp: fix deadlocks loading lots of files at once The first deadlock involved differing mutex acquisition order in two code paths: 1. loadParseTypecheck() holds the "mcache" mutex then eventually acquires the "handleMu" file mutex. 2. (*goFile).invalidateContent() acquires the "handleMu" mutex first and then the "mcache" mutex. Fix by changing the acquisition order in invalidateContent(). The second deadlock involved the file watcher. The two code paths involved were: 1. (*goFile).GetPackages() holds the view mutex and eventually calls (*WatchMap).Watch, which acquires the watcher mutex. 2. (*session).openOverlay acquires the watcher mutex as it triggers a file's callbacks, and then the callback "(*goFile).invalidateContent" acquires the view mutex. Fix by not holding the watcher mutex as we invoke the callbacks. Fixes golang/go#32910 Change-Id: I9d060e0d80fd86a317a1d6c7aaa736a8ce10bd07 GitHub-Last-Rev: 04944fa0249c0e6f1022a415787e23abce21bc2e GitHub-Pull-Request: golang/tools#129 Reviewed-on: https://go-review.googlesource.com/c/tools/+/184880 Reviewed-by: Rebecca Stambler Run-TryBot: Rebecca Stambler TryBot-Result: Gobot Gobot --- internal/lsp/cache/view.go | 8 +++++--- internal/lsp/cache/watcher.go | 10 ++++++++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/internal/lsp/cache/view.go b/internal/lsp/cache/view.go index 189a8d18236..326cf5c73c5 100644 --- a/internal/lsp/cache/view.go +++ b/internal/lsp/cache/view.go @@ -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 } diff --git a/internal/lsp/cache/watcher.go b/internal/lsp/cache/watcher.go index c8f619b1eaf..f788e88e0bf 100644 --- a/internal/lsp/cache/watcher.go +++ b/internal/lsp/cache/watcher.go @@ -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() } }