Commit e492d9f0 authored by Keith Randall's avatar Keith Randall

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/24749Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: default avatarJosh Bleecher Snyder <josharian@gmail.com>
parent a16a189f
......@@ -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)
}
}
......@@ -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)<<h.B - 1
......@@ -685,6 +682,9 @@ func mapiternext(it *hiter) {
callerpc := getcallerpc(unsafe.Pointer(&it))
racereadpc(unsafe.Pointer(h), callerpc, funcPC(mapiternext))
}
if h.flags&hashWriting != 0 {
throw("concurrent map iteration and map write")
}
t := it.t
bucket := it.bucket
b := it.bptr
......
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import "runtime"
func init() {
register("concurrentMapWrites", concurrentMapWrites)
register("concurrentMapReadWrite", concurrentMapReadWrite)
register("concurrentMapIterateWrite", concurrentMapIterateWrite)
}
func concurrentMapWrites() {
m := map[int]int{}
c := make(chan struct{})
go func() {
for i := 0; i < 10000; i++ {
m[5] = 0
runtime.Gosched()
}
c <- struct{}{}
}()
go func() {
for i := 0; i < 10000; i++ {
m[6] = 0
runtime.Gosched()
}
c <- struct{}{}
}()
<-c
<-c
}
func concurrentMapReadWrite() {
m := map[int]int{}
c := make(chan struct{})
go func() {
for i := 0; i < 10000; i++ {
m[5] = 0
runtime.Gosched()
}
c <- struct{}{}
}()
go func() {
for i := 0; i < 10000; i++ {
_ = m[6]
runtime.Gosched()
}
c <- struct{}{}
}()
<-c
<-c
}
func concurrentMapIterateWrite() {
m := map[int]int{}
c := make(chan struct{})
go func() {
for i := 0; i < 10000; i++ {
m[5] = 0
runtime.Gosched()
}
c <- struct{}{}
}()
go func() {
for i := 0; i < 10000; i++ {
for range m {
}
runtime.Gosched()
}
c <- struct{}{}
}()
<-c
<-c
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment