Skip to content

Commit

Permalink
sync: delete dirty keys inside Map.LoadAndDelete
Browse files Browse the repository at this point in the history
Fixes #40999

Change-Id: Ie32427e5cb5ed512b976b554850f50be156ce9f2
Reviewed-on: https://go-review.googlesource.com/c/go/+/250197
Run-TryBot: Emmanuel Odeke <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
Reviewed-by: Bryan C. Mills <[email protected]>
  • Loading branch information
changkun authored and Bryan C. Mills committed Aug 24, 2020
1 parent bb99874 commit 94953d3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/sync/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ func (m *Map) LoadAndDelete(key interface{}) (value interface{}, loaded bool) {
e, ok = read.m[key]
if !ok && read.amended {
e, ok = m.dirty[key]
delete(m.dirty, key)
// Regardless of whether the entry was present, record a miss: this key
// will take the slow path until the dirty map is promoted to the read
// map.
Expand Down
24 changes: 24 additions & 0 deletions src/sync/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"reflect"
"runtime"
"sync"
"sync/atomic"
"testing"
"testing/quick"
)
Expand Down Expand Up @@ -171,3 +172,26 @@ func TestConcurrentRange(t *testing.T) {
}
}
}

func TestIssue40999(t *testing.T) {
var m sync.Map

// Since the miss-counting in missLocked (via Delete)
// compares the miss count with len(m.dirty),
// add an initial entry to bias len(m.dirty) above the miss count.
m.Store(nil, struct{}{})

var finalized uint32

// Set finalizers that count for collected keys. A non-zero count
// indicates that keys have not been leaked.
for atomic.LoadUint32(&finalized) == 0 {
p := new(int)
runtime.SetFinalizer(p, func(*int) {
atomic.AddUint32(&finalized, 1)
})
m.Store(p, struct{}{})
m.Delete(p)
runtime.GC()
}
}

0 comments on commit 94953d3

Please sign in to comment.