kalmant
kalmant

Reputation: 106

Unexpected behavior using an unusual select

I'm writing some code where I'm passing data from one channel to another one. Following some intuition and this answer I expected the following code to work (other is a sufficiently big buffered channel and out is the source channel):

for {
    select {
    case other <- (<-out):
        log.Warn("C")
    }
}

And it does! But the other cases don't trigger at all, e.g. there are no Ds in the logs for the code below:

for {
    select {
    case other <- (<-out):
        log.Warn("C")
    default:
        log.Warn("D")
    }
}

Using the more traditional solution, there are Ds all over the logs:

for {
    select {
    case msg := <-out:
        other <- msg
        log.Warn("C")
    default:
        log.Warn("D")
    }
}

Obviously, I'm going with the usual solution, but I still don't know why the unusual one does not work as expected.

I suspect the answer lies somewhere in The Go Memory Model but I can't quite figure out what is exactly happening in this case.

I have put together some playgrounds where you can check out this behavior:

Thanks in advance to anyone who can shed some light on this!

Upvotes: 2

Views: 68

Answers (1)

icza
icza

Reputation: 417472

When you have this:

ch := make(chan int, 10)
// ...

select {
case ch <- <-out:
    fmt.Println("C")
default:
    fmt.Println("D")
}

The communication op of the first case is ch <- something, where something is <-out. But the something is evaluated first, and only then is checked which communication op of the cases can proceed.

So <-out will block as long as it needs, and then ch <- something is checked if it can proceed. Since you used a big enough buffer, it can always proceed in your example, so default is never chosen.

Spec: Select statements:

Execution of a "select" statement proceeds in several steps:

  1. For all the cases in the statement, the channel operands of receive operations and the channel and right-hand-side expressions of send statements are evaluated exactly once, in source order, upon entering the "select" statement. The result is a set of channels to receive from or send to, and the corresponding values to send. Any side effects in that evaluation will occur irrespective of which (if any) communication operation is selected to proceed. Expressions on the left-hand side of a RecvStmt with a short variable declaration or assignment are not yet evaluated.
  2. If one or more of the communications can proceed, a single one that can proceed is chosen via a uniform pseudo-random selection. Otherwise, if there is a default case, that case is chosen. If there is no default case, the "select" statement blocks until at least one of the communications can proceed.
  3. Unless the selected case is the default case, the respective communication operation is executed.
  4. If the selected case is a RecvStmt with a short variable declaration or an assignment, the left-hand side expressions are evaluated and the received value (or values) are assigned.
  5. The statement list of the selected case is executed.

If you lower the buffer of ch, you will see occasional Ds printed in the output (try it on the Go Playground).

ch := make(chan int, 2)

Upvotes: 5

Related Questions