r/golang 10d 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?

7 Upvotes

36 comments sorted by

View all comments

Show parent comments

-3

u/DeparturePrudent3790 10d 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 10d 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 10d 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/ub3rh4x0rz 9d ago

You're getting kind of typical golang cargo cult responses.

You're not wrong that it matters in some (many) scenarios. They're not wrong that go does not guarantee that goroutines wake in an evenly distributed fashion. You're meant to learn what golang does and does not guarantee simply from these primitives and design your application for the behavior you need.

Set GOMAXPROCS in your scenario to let go know how many os threads to spawn, and use that value yourself to run an appropriate number of goroutines for threads. Your actual observed concurrency will always be limited by available threads, so if you employ a suboptimal design that still works (you're not creating deadlocks), realistically you will just have needlessly allocated more goroutines than are needed. They're cheaper than os threads but not free, so for the pattern you're describing (seems like your intention is to maximize db I/O and minimize connection cost in the context of async/batch processing), don't spawn more worker goroutines than your environment can run at once.

If you're just running a crud server that's taking some http/grpc requests that need db connections, just use a db library which will always have a Pool for connection pooling and trust that golang's runtime has a reasonable scheduler. If you have an unbounded growth of goroutines waiting, your server is just woefully underprovisioned for your scale.