by sirclueless · 13y ago · 0 comments
Maybe to solve this I use a "quit" channel as they do. Do I need to worry about what happens if a quit message is never sent, or if two routines send one, or if the receiver is multiplexed and only one of many readers gets the message and cleans up correctly. It sounds like the whole malloc() and free() dance all over again, except this time I don't have any nice invariants to reason about such as "Everything that gets malloc()ed needs to be free()d, exactly once". Instead I need to worry that everyone is playing nice and will terminate when asked, and I need to remember to ask exactly once.
If you choose to select indefinitely, yes, you will need another channel to signal when you should return, or the goroutine will continue to run, and continue to hold a reference to that first channel.
A concrete example, slide 34 [1] contains this code:
func main() {
c := boring("Joe")
for {
select {
case s := <-c:
fmt.Println(s)
case <-time.After(1 * time.Second):
fmt.Println("You're too slow.")
return
}
}
}
This is code that looks really simple, but it's only correct because the process is known to terminate. You couldn't, for example, have the body of this function as a subroutine somewhere: every time you execute it a boring() goroutine is going to be left hanging, which is a classic memory leak. What's worse, "c" looks like an average variable I don't need to worry about (yay garbage collection!) but it's not because it escapes to a running goroutine.Correct me if I'm wrong but even if boring() was a much simpler function that returned after generating one value, if the timeout happened first it would be left hanging. In that case the only thing that would make the program correct is that of two racing goroutines, one is expected to return first. That's the kind of thing that keeps me up at night, especially when Go makes it so easy to treat goroutines and channels like every other GC-managed data structure when in fact they are every bit as leaky and difficult as threads.
Such is the case of the time.After() function, which easily solves the problem by returning a buffered channel. That would not work for an infinite generator of course.
The power and orthogonality of Go's concurrency primitives is actually encouraging. I'm not picking holes in it because I think Go is a bad language. On the contrary, I just think that it's not the endgame of concurrent programming. Using channels is not a safe operation in the same way that passing around memory references in a GC-managed language is safe, or passing values in a type-safe language is safe. You can write a correct, concurrent program with pretty much any behavior you want in Go, but it's still a minefield of deadlocks and memory leaks. I think there's room for improvement there.
...
ch := make(chan int)
defer close(ch) //I can timeout later if I want
go consumer(ch)
...
//I timeout down here and return
}
func consumer(input chan int) { //I will terminate when the input channel is closed
for x := range input {
fmt.Printf("I got %d!\n", x)
}
}- Goroutines are not garbage collected.
- In this case and many others, you can just use a buffered channel. But of course in many other cases it doesn't solve the problem.
- If you solve this with a "quit" channel but don't use it, obviously you have a problem
- Several goroutines can try to send to the same "quit" channel. Make it buffered and have every goroutine try to send.
- In the case of several listeners, you can just close the channel and all receivers will return.
- However if you have several senders and several receivers on the "quit" channel, then you have a problem: closing a closed channel will panic.
See also:
https://groups.google.com/forum/?fromgroups=#!msg/golang-nut...
https://groups.google.com/forum/?fromgroups=#!msg/golang-nut...
Essentially, it gets garbage collected without you having to worry about it unless you're doing something fairly special.
Qoute: "Goroutines are not garbage-collected. They must return from the top-level function (or panic) to have all their resources recovered."
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.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).