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?