[英]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? 给定上面的功能,如果select
语句选择上下文取消,那么goroutine是否试图永远推入doneChan
阻塞? Is the solution to simply always have a buffered channel in such cases? 在这种情况下,是否始终仅提供一个缓冲通道的解决方案?
要结束这个问题...简单的答案是。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.