mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
runtime: prevent garbage collection during hashmap insertion
Inserting a key-value pair into a hashmap storing keys or values indirectly can cause the garbage collector to find the hashmap in an inconsistent state. Fixes #5074. R=golang-dev, minux.ma, rsc CC=golang-dev https://golang.org/cl/7913043
This commit is contained in:
parent
b79afe1b71
commit
54dffda2b6
3 changed files with 33 additions and 4 deletions
|
|
@ -7,6 +7,7 @@ package runtime_test
|
|||
import (
|
||||
"os"
|
||||
"runtime"
|
||||
"runtime/debug"
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
|
@ -82,3 +83,17 @@ func TestGcDeepNesting(t *testing.T) {
|
|||
t.Fail()
|
||||
}
|
||||
}
|
||||
|
||||
func TestGcHashmapIndirection(t *testing.T) {
|
||||
defer debug.SetGCPercent(debug.SetGCPercent(1))
|
||||
runtime.GC()
|
||||
type T struct {
|
||||
a [256]int
|
||||
}
|
||||
m := make(map[T]T)
|
||||
for i := 0; i < 2000; i++ {
|
||||
var a T
|
||||
a.a[0] = i
|
||||
m[a] = T{}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue