Skip to content

Commit

Permalink
runtime: fix map iterator concurrent map check
Browse files Browse the repository at this point in the history
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 <bradfitz@golang.org>
Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com>
  • Loading branch information
randall77 committed Aug 16, 2016
1 parent a16a189 commit e492d9f
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 3 deletions.
37 changes: 37 additions & 0 deletions src/runtime/crash_test.go
Expand Up @@ -6,6 +6,7 @@ package runtime_test

import (
"bytes"
"flag"
"fmt"
"internal/testenv"
"io/ioutil"
Expand Down Expand Up @@ -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)
}
}
6 changes: 3 additions & 3 deletions src/runtime/hashmap.go
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
77 changes: 77 additions & 0 deletions src/runtime/testdata/testprog/map.go
@@ -0,0 +1,77 @@
// 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
}

0 comments on commit e492d9f

Please sign in to comment.