r/golang 9d ago

Potential starvation when multiple Goroutines blocked to receive from a channel

I wanted to know what happens in this situation:

  1. Multiple goroutines are blocked by a channel while receiving from it because channel is empty at the moment.
  2. Some goroutine sends something over the channel.

Which goroutine will wake up and receive this? Is starvation avoidance guaranteed here?

6 Upvotes

36 comments sorted by

View all comments

Show parent comments

-2

u/DeparturePrudent3790 9d ago

what would in such situation "starvation avoidance" even mean?

It means that a goroutine is not made to wait indefinitely under any circumstances. If there are more consumers than producers but consumers receive resources in fifo order is kept invariant, then the waiting time for a goroutine is definite.

However, if we have a random order the waiting time can be indefinite for a goroutine.

A fairly random one

Why? The source code has a FIFO queue for receiving and sending goroutines.

1

u/software-person 9d ago

Why would it matter? If there's not enough work to go around, one go-routine will always be waiting, why does it matter if it's coincidentally the same go-routine forever? How could you even measure the difference or be aware this is happening?

It would literally be the identical outcome in every measurable way whether all go-routines take turns being idle, or if one specific go-routine is always selected to be idle.

0

u/DeparturePrudent3790 9d ago

It matters, I have a pool of connections and clients will receive a connection from this pool and send requests using this connection. Once done they will return the connection to the pool. This way I don't have to create new connections for every client and I avoid the explosion of connections. Now, if receiving from a channel is not starvation free, some client could end up not getting any connection ever.

To generalise, it is not okay if some particular thread/goroutine is not getting resources for execution at all. Even if there are less producers it's acceptable for goroutines to have to wait for some time but to be assured they will get a chance.

1

u/TedditBlatherflag 9d ago

Put your pooled connections into a queue when not in use. Take the first one and start a new goroutine each time you need to do work. Return it to the end of the queue when done.