Skip to content
Merged
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
18 changes: 17 additions & 1 deletion internal/project/extendedconfigcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ func (c *extendedConfigCache) Acquire(fh FileHandle, path tspath.Path, parse fun
func (c *extendedConfigCache) Ref(path tspath.Path) {
if entry, ok := c.entries.Load(path); ok {
entry.mu.Lock()
if entry.refCount <= 0 {
// Entry was deleted while we were acquiring the lock
Copy link
Member

Choose a reason for hiding this comment

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

Isn't possible that the entry got deleted before the cal to c.entries.Load(path) as well?

Copy link
Member Author

Choose a reason for hiding this comment

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

If the entry got deleted from the map before Load, then ok would be false.

Copy link
Member

Choose a reason for hiding this comment

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

Duh, sorry.

newEntry, loaded := c.loadOrStoreNewLockedEntry(path)
if !loaded {
newEntry.entry = entry.entry
newEntry.hash = entry.hash
}
entry.mu.Unlock()
newEntry.mu.Unlock()
return
}
entry.refCount++
entry.mu.Unlock()
}
Expand All @@ -48,10 +59,10 @@ func (c *extendedConfigCache) Deref(path tspath.Path) {
entry.mu.Lock()
entry.refCount--
remove := entry.refCount <= 0
entry.mu.Unlock()
if remove {
c.entries.Delete(path)
}
entry.mu.Unlock()
}
}

Expand All @@ -68,6 +79,11 @@ func (c *extendedConfigCache) loadOrStoreNewLockedEntry(path tspath.Path) (*exte
entry.mu.Lock()
if existing, loaded := c.entries.LoadOrStore(path, entry); loaded {
existing.mu.Lock()
if existing.refCount <= 0 {
// Entry was deleted while we were acquiring the lock
existing.mu.Unlock()
return c.loadOrStoreNewLockedEntry(path)
}
existing.refCount++
return existing, true
}
Expand Down
18 changes: 17 additions & 1 deletion internal/project/parsecache.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ func (c *ParseCache) Ref(file *ast.SourceFile) {
key := newParseCacheKey(file.ParseOptions(), file.ScriptKind)
if entry, ok := c.entries.Load(key); ok {
entry.mu.Lock()
if entry.refCount <= 0 && !c.Options.DisableDeletion {
// Entry was deleted while we were acquiring the lock
newEntry, loaded := c.loadOrStoreNewLockedEntry(key)
if !loaded {
newEntry.sourceFile = entry.sourceFile
newEntry.hash = entry.hash
}
entry.mu.Unlock()
newEntry.mu.Unlock()
return
}
entry.refCount++
entry.mu.Unlock()
} else {
Expand All @@ -76,10 +87,10 @@ func (c *ParseCache) Deref(file *ast.SourceFile) {
entry.mu.Lock()
entry.refCount--
remove := entry.refCount <= 0
entry.mu.Unlock()
if !c.Options.DisableDeletion && remove {
c.entries.Delete(key)
}
entry.mu.Unlock()
}
}

Expand All @@ -92,6 +103,11 @@ func (c *ParseCache) loadOrStoreNewLockedEntry(key parseCacheKey) (*parseCacheEn
existing, loaded := c.entries.LoadOrStore(key, entry)
if loaded {
existing.mu.Lock()
if existing.refCount <= 0 && !c.Options.DisableDeletion {
// Existing entry was deleted while we were acquiring the lock
existing.mu.Unlock()
return c.loadOrStoreNewLockedEntry(key)
}
existing.refCount++
return existing, true
}
Expand Down