From e492d9f01890cf61cb009b3b3617238a8947ebbe Mon Sep 17 00:00:00 2001 From: Keith Randall Date: Wed, 6 Jul 2016 15:02:49 -0700 Subject: [PATCH] runtime: fix map iterator concurrent map check We should check whether there is a concurrent writer at the start of every mapiternext, not just in mapaccessK (which is only called during certain map growth situations). Tests turned off by default because they are inherently flaky. Fixes #16278 Change-Id: I8b72cab1b8c59d1923bec6fa3eabc932e4e91542 Reviewed-on: https://go-review.googlesource.com/24749 Reviewed-by: Brad Fitzpatrick Reviewed-by: Josh Bleecher Snyder --- src/runtime/crash_test.go | 37 +++++++++++++ src/runtime/hashmap.go | 6 +-- src/runtime/testdata/testprog/map.go | 77 ++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+), 3 deletions(-) create mode 100644 src/runtime/testdata/testprog/map.go diff --git a/src/runtime/crash_test.go b/src/runtime/crash_test.go index a2f7ff7dec..2f6afb60f6 100644 --- a/src/runtime/crash_test.go +++ b/src/runtime/crash_test.go @@ -6,6 +6,7 @@ package runtime_test import ( "bytes" + "flag" "fmt" "internal/testenv" "io/ioutil" @@ -482,3 +483,39 @@ func TestMemPprof(t *testing.T) { t.Error("missing MemProf in pprof output") } } + +var concurrentMapTest = flag.Bool("run_concurrent_map_tests", false, "also run flaky concurrent map tests") + +func TestConcurrentMapWrites(t *testing.T) { + if !*concurrentMapTest { + t.Skip("skipping without -run_concurrent_map_tests") + } + testenv.MustHaveGoRun(t) + output := runTestProg(t, "testprog", "concurrentMapWrites") + want := "fatal error: concurrent map writes" + if !strings.HasPrefix(output, want) { + t.Fatalf("output does not start with %q:\n%s", want, output) + } +} +func TestConcurrentMapReadWrite(t *testing.T) { + if !*concurrentMapTest { + t.Skip("skipping without -run_concurrent_map_tests") + } + testenv.MustHaveGoRun(t) + output := runTestProg(t, "testprog", "concurrentMapReadWrite") + want := "fatal error: concurrent map read and map write" + if !strings.HasPrefix(output, want) { + t.Fatalf("output does not start with %q:\n%s", want, output) + } +} +func TestConcurrentMapIterateWrite(t *testing.T) { + if !*concurrentMapTest { + t.Skip("skipping without -run_concurrent_map_tests") + } + testenv.MustHaveGoRun(t) + output := runTestProg(t, "testprog", "concurrentMapIterateWrite") + want := "fatal error: concurrent map iteration and map write" + if !strings.HasPrefix(output, want) { + t.Fatalf("output does not start with %q:\n%s", want, output) + } +} diff --git a/src/runtime/hashmap.go b/src/runtime/hashmap.go index 509cab2f0f..f756e7b603 100644 --- a/src/runtime/hashmap.go +++ b/src/runtime/hashmap.go @@ -382,9 +382,6 @@ func mapaccessK(t *maptype, h *hmap, key unsafe.Pointer) (unsafe.Pointer, unsafe if h == nil || h.count == 0 { return nil, nil } - if h.flags&hashWriting != 0 { - throw("concurrent map read and map write") - } alg := t.key.alg hash := alg.hash(key, uintptr(h.hash0)) m := uintptr(1)<