News for hackers. Stuff that matters.
Re:Go channels are bad by hamburglar on Wednesday March 02, 2016 @12:10PM (#11211264)
Non Go programmer here. Can someone explain the initial goroutine leak that is being addressed? I don't see the issue.
Re:Go channels are bad by divan on Wednesday March 02, 2016 @12:53PM (#11211630)
It's not actually a leak. It's a program explicitly doing 'run goroutine and don't care of it anymore'. If the program logic wants this - it's ok. If author wants it to finish on some condition, but didn't write the condition code (like in this article) - it's a leak, but it's purely author's mistake.
Re:Go channels are bad by jtolds on Wednesday March 02, 2016 @01:02PM (#11211684)
There is a deliberate leak in the example program. The author was attempting to illustrate that fixing the leak using just channels would be a challenge.
Re:Go channels are bad by courtf on Wednesday March 02, 2016 @01:13PM (#11211763)
Synchronizing access to a memory address isn't really the use-case for channels. I think that's fairly well understood by the Go programmers I work with. This example demonstrates why, but it prefaces the discussion by implying this is the standard practice, which I think is misleading.
Re:Go channels are bad by jtolds on Wednesday March 02, 2016 @06:17PM (#11213854)
I perhaps communicated poorly, but the point of that section was to try and explain that the CSP model (only using channels) was untenable in Go (even though it doesn't necessarily have to be in general), and that you'd almost certainly end up not just using channels in a real program, which it seems you agree with.
Re:Go channels are bad by r_sreeram on Wednesday March 02, 2016 @12:38PM (#11211510)
> Can someone explain the initial goroutine leak that is being addressed?

The "for score := range g.scores {" loop runs forever, since nothing ever closes the g.scores channel. I.e., the "range" only terminates when the channel is explicitly closed. Even if there are no current senders on the channel, and even if nobody else holds a reference to the channel (and thus nobody else could potentially create a new sender and start sending on that channel), Go doesn't realize it (garbage collection doesn't help here). The "range" waits forever.

Thus, all goroutines that run this code (via calls to NewGame(), via "go g.run()") will run forever, and leak, as long as something else in the program is running. When the rest of the program is done, Go will correctly detect that all these leaked goroutines are blocked and thus it's a deadlock, leading Go to panic ("no goroutines can proceed") and terminate.

Re:Go channels are bad by aardvark179 on Wednesday March 02, 2016 @12:31PM (#11211441)
I'm not a go programmer, but think of it like this.

You start a game, and that starts a goroutine that goes round in a loop getting scores from a channel. You have players which also have references to the channel and who put scores onto it.

When all the players have left the only thing that has access to the channel is the game's goroutine. It's not consuming CPU itself because it's simply waiting for something to be put on its channel, but it does still have its stack and other resources, and it now has no way to exit.

You can get this sort of resource leak in lots of ways in concurrent systems, and they all essentially boil down to the same thing, a thread or goroutine, or whatever, is waiting on a resource that nothing else has a reference to anymore, and there is no other way end it.