这个goroutine会永远泄漏/阻塞吗?

时间:2018-05-20 20:10:07

标签: go

对以下行为感到好奇

func test() error {
        ctx, cancel := context.WithCancel(context.Background())
        cancel()
        doneChan := make(chan bool)
        go func() {
            // emulate a long running function
            time.Sleep(time.Minute)
            // never exits?
            doneChan <- true
        }()
        select {
        case <- ctx.Done():
            return ctx.Err()
        case <- doneChan:
            return nil
        }
}

鉴于上述功能,如果select语句选择了上下文取消,那么goroutine是否会尝试永久阻止doneChan被阻止?在这种情况下,解决方案是否总是只有一个缓冲通道?

1 个答案:

答案 0 :(得分:0)

要结束这个问题......简单的答案是肯定的。