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

crash with 'concurrent map read and map write' error #74

Open
shuxiao9058 opened this issue Jan 14, 2019 · 1 comment
Open

crash with 'concurrent map read and map write' error #74

shuxiao9058 opened this issue Jan 14, 2019 · 1 comment

Comments

@shuxiao9058
Copy link

fatal error: concurrent map read and map write

goroutine 264720 [running]:
runtime.throw(0x79cf89, 0x21)
        /usr/local/golang/go/src/runtime/panic.go:608 +0x72 fp=0xc0006ed770 sp=0xc0006ed740 pc=0x42c262
runtime.mapaccess2(0x72b9c0, 0xc0002b59e0, 0xc000ac4b30, 0x7f9d80, 0x496b00)
        /usr/local/golang/go/src/runtime/map.go:453 +0x223 fp=0xc0006ed7b8 sp=0xc0006ed770 pc=0x40ed83
reflect.mapaccess(0x72b9c0, 0xc0002b59e0, 0xc000ac4b30, 0x7980da)
        /usr/local/golang/go/src/runtime/map.go:1249 +0x3f fp=0xc0006ed7f0 sp=0xc0006ed7b8 pc=0x410d4f
reflect.Value.MapIndex(0x72b9c0, 0xc0002b59e0, 0x15, 0x719f60, 0xc000ac4b30, 0x98, 0x72a2e0, 0xc0003f86e0, 0x94)
        /usr/local/golang/go/src/reflect/value.go:1111 +0x11d fp=0xc0006ed870 sp=0xc0006ed7f0 pc=0x4b221d
encoding/json.(*mapEncoder).encode(0xc00026c140, 0xc0009e0160, 0x72b9c0, 0xc0002b59e0, 0x15, 0x720100)
        /usr/local/golang/go/src/encoding/json/encode.go:692 +0x329 fp=0xc0006ed9e0 sp=0xc0006ed870 pc=0x652159
encoding/json.(*mapEncoder).encode-fm(0xc0009e0160, 0x72b9c0, 0xc0002b59e0, 0x15, 0xc0002b0100)
        /usr/local/golang/go/src/encoding/json/encode.go:708 +0x64 fp=0xc0006eda20 sp=0xc0006ed9e0 pc=0x65a6c4
encoding/json.(*encodeState).reflectValue(0xc0009e0160, 0x72b9c0, 0xc0002b59e0, 0x15, 0xc0002b0100)
        /usr/local/golang/go/src/encoding/json/encode.go:333 +0x82 fp=0xc0006eda58 sp=0xc0006eda20 pc=0x64f3f2
encoding/json.interfaceEncoder(0xc0009e0160, 0x72a2e0, 0xc000011210, 0x194, 0xc000010100)
        /usr/local/golang/go/src/encoding/json/encode.go:619 +0xab fp=0xc0006edaa0 sp=0xc0006eda58 pc=0x65164b
encoding/json.(*ptrEncoder).encode(0xc00026c0e8, 0xc0009e0160, 0x70b200, 0xc000011210, 0x16, 0x700100)
        /usr/local/golang/go/src/encoding/json/encode.go:788 +0xb3 fp=0xc0006edae8 sp=0xc0006edaa0 pc=0x6530d3
encoding/json.(*ptrEncoder).encode-fm(0xc0009e0160, 0x70b200, 0xc000011210, 0x16, 0xc000770100)
        /usr/local/golang/go/src/encoding/json/encode.go:793 +0x64 fp=0xc0006edb28 sp=0xc0006edae8 pc=0x65a844
encoding/json.(*encodeState).reflectValue(0xc0009e0160, 0x70b200, 0xc000011210, 0x16, 0xa10100)
        /usr/local/golang/go/src/encoding/json/encode.go:333 +0x82 fp=0xc0006edb60 sp=0xc0006edb28 pc=0x64f3f2
encoding/json.(*encodeState).marshal(0xc0009e0160, 0x70b200, 0xc000011210, 0x100, 0x0, 0x0)
        /usr/local/golang/go/src/encoding/json/encode.go:305 +0xf4 fp=0xc0006edba8 sp=0xc0006edb60 pc=0x64eea4
encoding/json.Marshal(0x70b200, 0xc000011210, 0xc000941470, 0x445787, 0x0, 0xc000941488, 0x3)
        /usr/local/golang/go/src/encoding/json/encode.go:160 +0x52 fp=0xc0006edc20 sp=0xc0006edba8 pc=0x64e942
github.com/bitly/go-simplejson.(*Json).MarshalJSON(0xc000011210, 0xc0009414d8, 0x507afd, 0x0, 0xc00081c847, 0xf)
        /usr/local/golang/gopath/src/github.com/bitly/go-simplejson/simplejson.go:53 +0x39 fp=0xc0006edc68 sp=0xc0006edc20 pc=0x65b549
github.com/bitly/go-simplejson.(*Json).Encode(0xc000011210, 0x4, 0x0, 0xf, 0xf, 0xc00081c847)
        /usr/local/golang/gopath/src/github.com/bitly/go-simplejson/simplejson.go:43 +0x2b fp=0xc0006edca8 sp=0xc0006edc68 pc=0x65b4cb
@ljluestc
Copy link

import (
    "sync"
)

// Define a struct that includes the map and a mutex.
type MapWithMutex struct {
    m   map[string]int
    mu  sync.Mutex
}

func main() {
    // Create an instance of MapWithMutex.
    mwm := MapWithMutex{
        m: make(map[string]int),
    }

    // Create and start multiple goroutines.
    for i := 0; i < 10; i++ {
        go func(i int) {
            key := strconv.Itoa(i)

            // Lock the mutex before accessing the map.
            mwm.mu.Lock()
            defer mwm.mu.Unlock()

            // Perform read or write operations on the map.
            mwm.m[key] = i
            value := mwm.m[key]
            fmt.Printf("Goroutine %d: Key %s, Value %d\n", i, key, value)
        }(i)
    }

    // Wait for the goroutines to finish.
    time.Sleep(time.Second)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants