3 ms·
It took me a while to get what you were asking. Given (Syntax may be wonky): func b(chan x) { time.Sleep(60 seconds); x <- true } func a() { x :
by nemothekid 14y ago
It took me a while to get what you were asking.
Given (Syntax may be wonky):
func b(chan x) { time.Sleep(60 seconds); x <- true }
func a() {
x := make(chan bool)
t := timeoutChannelCruft(30 seconds) // don't remember the syntax
go b(x)
select {
case <-x: return;
case <-t: return;
}
}
What happens to goroutine b and channel x? Does goroutine b block for the remainder of the program? Does channel x ever get cleaned up?
I'd guess b() blocks forever so in your timeout case you could probably just do
go func(){ <- x }()
to make sure that the b() eventually returns and is cleaned up.
- sirclueless 14y agoRight, but be careful, if you execute that in the <-x case you just created a totally different memory leak since there won't be any more values coming down that channel. The only reason the timeout channel itself is safe is because the channel is buffered. Which incidentally is a good way to handle this particular example: make a buffer of size 1, and then be sure to never ever send more than 1 value. This is kind of my point: with Go's concurrency, all the contracts are implicit and dangerous, exactly the kind of thing that are supposed to be solved by a good type system and garbage collection (incidentally, Go gets all of that stuff right).