Skip to content

Commit

Permalink
db: wait for table cache releasing goroutines to exit
Browse files Browse the repository at this point in the history
During tableCache.Close, wait for each shard's releasing goroutine to exit.

Fix cockroachdb#1898.
  • Loading branch information
jbowens committed Aug 26, 2022
1 parent c751cef commit 682d5e1
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions table_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,9 @@ type tableCacheShard struct {
sizeCold int
sizeTest int
}
releasing sync.WaitGroup
releasingCh chan *tableCacheValue
releasing sync.WaitGroup
releasingCh chan *tableCacheValue
releaseLoopExit sync.WaitGroup
}

func (c *tableCacheShard) init(size int) {
Expand All @@ -309,6 +310,7 @@ func (c *tableCacheShard) init(size int) {
c.mu.nodes = make(map[tableCacheKey]*tableCacheNode)
c.mu.coldTarget = size
c.releasingCh = make(chan *tableCacheValue, 100)
c.releaseLoopExit.Add(1)
go c.releaseLoop()

if invariants.RaceEnabled {
Expand All @@ -318,6 +320,7 @@ func (c *tableCacheShard) init(size int) {

func (c *tableCacheShard) releaseLoop() {
pprof.Do(context.Background(), tableCacheLabels, func(context.Context) {
defer c.releaseLoopExit.Done()
for v := range c.releasingCh {
v.release(c)
}
Expand Down Expand Up @@ -853,10 +856,14 @@ func (c *tableCacheShard) Close() error {
// complete. This behavior is used by iterator leak tests. Leaking the
// goroutine for these tests is less bad not closing the iterator which
// triggers other warnings about block cache handles not being released.
if err == nil {
close(c.releasingCh)
if err != nil {
c.releasing.Wait()
return err
}

close(c.releasingCh)
c.releasing.Wait()
c.releaseLoopExit.Wait()
return err
}

Expand Down

0 comments on commit 682d5e1

Please sign in to comment.