When my simple Go program run ,Why the result is deadlock?

Issue

This is my entire Go code! What confused me is that case balances <- balance: did’t occurs.I dont know why?

package main

import (
    "fmt"
)


func main() {

    done := make(chan int)

    var balance int
    balances := make(chan int)
    balance = 1

    go func() {
        fmt.Println(<-balances)
        done <- 1
    }()

    select {
    case balances <- balance:
        fmt.Println("done case")

    default:
        fmt.Println("default case")
    }

    <-done

}
default case
fatal error: all goroutines are asleep - deadlock!

goroutine 1 [chan receive]:
main.main()
    /tmp/sandbox575832950/prog.go:29 +0x13d

goroutine 18 [chan receive]:
main.main.func1()
    /tmp/sandbox575832950/prog.go:17 +0x38
created by main.main
    /tmp/sandbox575832950/prog.go:16 +0x97

Solution

What confused me is that case balances <- balance: did’t occurs

To be specific: it’s because of select with a default case.

Whenever you create a new goroutine with go ...(), there is no guarantee about whether the invoking goroutine, or the invoked goroutine, will run next.

In practice it’s likely that the next statements in the invoking goroutine will execute next (there being no particularly good reason to stop it). Of course, we should write programs that function correctly all the time, not just some, most, or even almost all the time! Concurrent programming with go ...() is all about synchronizing the goroutines so that the intended behavior must occur. Channels can do that, if used properly.

I think the balances channel can receive data

It’s an unbuffered channel, so it can receive data if someone is reading from it. Otherwise, that write to the channel will block. Which brings us back to select.

Since you provided a default case, it’s quite likely that the goroutine that invoked go ...() will continue to execute, and select that can’t immediately choose a different case, will choose default. So it would be very unlikely for the invoked goroutine to be ready to read from balances before the main goroutine had already proceeded to try to write to it, failed, and gone on to the default case.

You can solve this by either removing the default case from the select and allowing it to block until balances is ready to be written to.

You sure can, as @Schwern points out. But it’s important that you understand you don’t necessarily need to use select to use channels. Instead of a select with just one case, you could instead just write

balances <- balance
fmt.Println("done")

select is not required in this case, default is working against you, and there’s just one case otherwise, so there’s no need for select. You want the main function to block on that channel.

you can add a buffer to balances so it can be written to before it is read from.

Sure. But again, important to understand that the fact that a channel might block both sender and receiver until they’re both ready to communicate , is a valid, useful, and common use of channels. Unbuffered channels are not the cause of your problem – providing a default case for your select, and thus a path for unintended behavior, is the cause.

Answered By – Daniel Farrell

Answer Checked By – Dawn Plyler (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.