Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit df7acff

Browse files
committed
I win at debugging deadlocks today
1 parent ec53459 commit df7acff

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

coderd/files/cache.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ type cacheEntry struct {
114114
value *lazy.ValueWithError[CacheEntryValue]
115115

116116
close func()
117+
purge func()
117118
}
118119

119120
type CacheEntryValue struct {
@@ -149,7 +150,8 @@ func (c *Cache) Acquire(ctx context.Context, db database.Store, fileID uuid.UUID
149150
e := c.prepare(db, fileID)
150151
ev, err := e.value.Load()
151152
if err != nil {
152-
c.purge(fileID)
153+
e.close()
154+
e.purge()
153155
return nil, err
154156
}
155157

@@ -193,9 +195,9 @@ func (c *Cache) prepare(db database.Store, fileID uuid.UUID) *cacheEntry {
193195
if !ok {
194196
hitLabel = "false"
195197

196-
var releaseOnce sync.Once
197-
release := func() {
198-
releaseOnce.Do(func() {
198+
var purgeOnce sync.Once
199+
purge := func() {
200+
purgeOnce.Do(func() {
199201
c.purge(fileID)
200202
})
201203
}
@@ -204,10 +206,6 @@ func (c *Cache) prepare(db database.Store, fileID uuid.UUID) *cacheEntry {
204206
value: lazy.NewWithError(func() (CacheEntryValue, error) {
205207
val, err := fetch(db, fileID)
206208
if err != nil {
207-
// Force future calls to Acquire to trigger a new fetch as soon as
208-
// a fetch has failed, even if references are still held.
209-
entry.close()
210-
release()
211209
return val, err
212210
}
213211

@@ -228,8 +226,10 @@ func (c *Cache) prepare(db database.Store, fileID uuid.UUID) *cacheEntry {
228226
return
229227
}
230228

231-
release()
229+
purge()
232230
},
231+
232+
purge: purge,
233233
}
234234
c.data[fileID] = entry
235235

@@ -245,7 +245,8 @@ func (c *Cache) prepare(db database.Store, fileID uuid.UUID) *cacheEntry {
245245
return entry
246246
}
247247

248-
// purge immediately removes an entry from the cache. It should be called
248+
// purge immediately removes an entry from the cache, even if it has open references.
249+
// It should only be called from the `close` function in a `cacheEntry`.
249250
func (c *Cache) purge(fileID uuid.UUID) {
250251
c.lock.Lock()
251252
defer c.lock.Unlock()

0 commit comments

Comments
 (0)