简体   繁体   中英

Does this goroutine leak/block forever?

Was curious about the following behavior

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
        }
}

Given the function above, if the select statement chooses the context cancellation is the goroutine trying to push onto the doneChan blocked forever? Is the solution to simply always have a buffered channel in such cases?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM