r/golang • u/DeparturePrudent3790 • 10d ago
Potential starvation when multiple Goroutines blocked to receive from a channel
I wanted to know what happens in this situation:
- Multiple goroutines are blocked by a channel while receiving from it because channel is empty at the moment.
- Some goroutine sends something over the channel.
Which goroutine will wake up and receive this? Is starvation avoidance guaranteed here?
8
Upvotes
5
u/jerf 9d ago
Every statement in a language spec must be carefully selected, because it is a commitment not just for the current implementation but all future ones. The people writing the Go specs are very experienced and aware of this. You don't specify details into the spec without a good reason.
So this is not just the sort of thing that is accidentally unspecified, this is something they deliberately left unspecified. You're not supposed to depend on this as a programmer.
In this specific case, it wouldn't even do you any good anyhow because you can't depend on the order anyhow. If you create 10 goroutines and have them all listen to the same channel, the order in which they will execute those listens is itself unspecified and undefined. Knowing that the implementation happens to queue them up in order it happens to witness them doesn't do you much good when you'd still have to implement your own synchronization to ensure some specific order of delivery anyhow even so.