Skip to content

sync: document that double RLock isn't safe #15418

@ghost

Description

Please answer these questions before submitting your issue. Thanks!

  1. What version of Go are you using (go version)?
    go version go1.6 darwin/amd64
  2. What operating system and processor architecture are you using (go env)?
    GOARCH="amd64"
    GOBIN=""
    GOEXE=""
    GOHOSTARCH="amd64"
    GOHOSTOS="darwin"
    GOOS="darwin"
    GOPATH=""
    GORACE=""
    GOROOT="/usr/local/go"
    GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
    GO15VENDOREXPERIMENT="1"
    CC="clang"
    GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fno-common"
    CXX="clang++"
    CGO_ENABLED="1"
  3. What did you do?
package main

import (
    "fmt"
    "sync"
)

func main() {
    ch := make(chan int)
    testFunc := func() {
        weirdLock := sync.RWMutex{}
        go func() {
            weirdLock.RLock()
            go func() {
                weirdLock.RLock()
                for i := 0; i < 3; i++ {
                    func() {
                        weirdLock.RLock()
                        ch <- 1
                        weirdLock.RUnlock()
                    }()
                }
                weirdLock.RUnlock()
            }()
            weirdLock.RUnlock()
        }()
        go func() {
            weirdLock.Lock()
            ch <- 1
            weirdLock.Unlock()
        }()
    }
    runNum := 1000
    for i := 0; i < runNum; i++ {
        testFunc()
    }
    total := 0
    for i := 0; i < 4*runNum; i++ {
        total += <-ch
    }
    //Expect 4*runNum
    fmt.Println(total)
}

http://play.golang.org/p/UWIAmPCRCu

  1. What did you expect to see?
    output 4000
  2. What did you see instead?
    fatal error: all goroutines are asleep - deadlock!
    runNum more big,the probability of deadlock more often

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocumentationIssues describing a change to documentation.FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions