简体   繁体   中英

select with channel <- <- channel

I am curious why the following does not work. In general select with a default: prevents deadlock, but not in this case:

package main

import "fmt"

func main () {
    a := make(chan int)
    b := make(chan int)

    select {
    case a <- <- b:
        fmt.Println("this is impossible")
    default:
        fmt.Println("select worked as naively expected")
    }
}

Obviously it doesn't like the <- <- but I'm wondering what's going on behind the surface here. In other situations <- <- is allowed (though perhaps not recommended).

a <- <- b is the same as a<- (<-b) , because the <- operator associates with the leftmost chan possible.

So the select has a case with a send operation (in the form of a<- (something) ). And what happens here is that the right-hand-side expression of the send statement (the value to be sent) is evaluated first - which is <-b . But this will block forever (because no one is sending anything on b ), so:

fatal error: all goroutines are asleep - deadlock!

Relevant section form the 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. ...

So if default is present, the select does prevent blocking if none of the communications can proceed in step 2 , but your code gets stuck in step 1 .


Just to be complete, if there would be a goroutine that would send a value on b , then evaluation of <- b would not block, so the execution of select would not stuck in step 2, and you would see the expected "select worked as naively expected" (because receiving from a could still not proceed therefore default would be chosen):

go func() { b <- 1 }()

select {
    // ...
}

Try it on the Go Playground .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM