Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

concurrent map delete and read not panic, read got key of default value #17922

Closed
Cas-pian opened this issue Nov 15, 2016 · 5 comments
Closed

Comments

@Cas-pian
Copy link

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go version go1.7.3 linux/amd64

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/opt/go_3rdparty"
GORACE=""
GOROOT="/opt/go"
GOTOOLDIR="/opt/go/pkg/tool/linux_amd64"
CC="gcc"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build972690274=/tmp/go-build -gno-record-gcc-switches"
CXX="g++"
CGO_ENABLED="1"

What did you do?

If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on play.golang.org is best.

concurrent map delete and read should panic, but it didn't panic and then read success with key of default value !
This is the test code:

package main

import (
        "fmt"
        "math"
        "time"
)

func main() {
        var m = make(map[int32]bool)

        go func() {
                for {
                        for k, v := range m {
                                if k == 0 {
                                        fmt.Println(k, v)
                                        panic(fmt.Errorf("No! got 0! key should >= 1 !"))
                                }
                                time.Sleep(1 * time.Millisecond)
                        }
                }
        }()
        i := int32(1) // make sure key>=1
        for i < math.MaxInt32 {
                m[i] = true
                delete(m, i)
                i++
                time.Sleep(1 * time.Millisecond)
        }
}

What did you expect to see?

panic info like this:
fatal error: concurrent map read and map write

What did you see instead?

0 false
panic: No! got 0! key should >= 1 !

goroutine 5 [running]:
panic(0x490fe0, 0xc42000a3b0)
        /opt/go/src/runtime/panic.go:500 +0x1a1
main.main.func1(0xc420014210)
        /home/caspian/goprojects/src/testmap.go:17 +0x1c8
created by main.main
        /home/caspian/goprojects/src/testmap.go:22 +0x73
exit status 2
@randall77
Copy link
Contributor

This is fixed in tip, https://go-review.googlesource.com/c/24749/ (treating iteration as read)
Note that this panic is best effort, you should not rely on it.

@Cas-pian
Copy link
Author

@randall77, Thanks for your reply!
I get it, this is fixed in the main branch, but 1.7.x release doesn't contain it.

@davecheney
Copy link
Contributor

Don't forget this part

Note that this panic is best effort, you should not rely on it.

If in doubt, run your code under the race detector.

On Tue, Nov 15, 2016 at 5:53 PM, Caspian notifications@github.com wrote:

@randall77 https://github.com/randall77, Thanks for your reply!

I get it, this is fixed in the main branch, but 1.7.x release doesn't
contain it.


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
#17922 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AAAcA6VdnYNj-CG6TCEriWfsyQBjA9Kbks5q-VbsgaJpZM4KyGwo
.

@Cas-pian
Copy link
Author

@davecheney,OK,thanks very much!

@bradfitz
Copy link
Contributor

I get it, this is fixed in the main branch, but 1.7.x release doesn't contain it.

And it never will. It will be in Go 1.8 and later.

@golang golang locked and limited conversation to collaborators Nov 15, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants