SeattleOrBayArea
SeattleOrBayArea

Reputation: 3118

Why does my code deadlock and how to fix it

Why does my code deadlock (followed by crash) on read from channel, I would expect it to block on read after the channel is read completely, but not crash. I understand it is a deadlock state as no one is writing to the channel and read blocks on it.

How can I change the code to read all the channel content and then exit from main instead of crash.

Go playground: https://play.golang.org/p/rjXZZOx1FFZ

package main

import (
    "fmt"
    "sync"
)

func main() {
    var wg sync.WaitGroup
    news := make(chan int, 10)

    wg.Add(1)

    go foo(&wg, news) 

    wg.Wait()   

    for {
        fmt.Printf("reading: %v\n", <-news) 
        //crashes here after printing 0-9
    }

}

func foo(wg *sync.WaitGroup, news chan int) {
    for i:=0; i<10;i++ {
        fmt.Printf("Writing\n")
        news <- i
    }
    (*wg).Done()
}

Upvotes: 3

Views: 174

Answers (3)

Thundercat
Thundercat

Reputation: 121049

The program deadlocks because main blocks on channel receive and there are no other goroutines that will send to that channel.

Use this approach to read all the channel content and then exit from main: main reads the channel until closed; foo writes all values and closes the channel.

func main() {
    news := make(chan int, 10)
    go foo(news)
    // range breaks when the channel is closed
    for v := range news { 

        fmt.Printf("reading: %v\n", v)
    }

}

func foo(news chan int) {
    for i := 0; i < 10; i++ {
        fmt.Printf("Writing\n")
        news <- i
    }
    // close channel to indicate that no more values will be sent.
    close(news) 
}

Run it on the Playground.

Upvotes: 2

stderr
stderr

Reputation: 8732

The code crashes due to deadlock.

fatal error: all goroutines are asleep - deadlock!

You have a couple interesting problems with the original code.

If you close the news channel in your foo goroutine instead of deadlocking you instead will just run your infinite loop in main forever.

2 simple code changes should make this a bit more clear and will cause it to exit after processing the news items.

https://play.golang.org/p/qSU7sV7Wrov

package main

import (
    "fmt"
    "sync"
)

func main() {
    var wg sync.WaitGroup
    news := make(chan int, 10)

    wg.Add(1)

    go foo(&wg, news)

    wg.Wait()

    for n := range news {
        fmt.Printf("reading: %v\n", n)
    }
    fmt.Println("We're out of news!")
}

func foo(wg *sync.WaitGroup, news chan int) {
    for i := 0; i < 10; i++ {
        fmt.Printf("Writing\n")
        news <- i
    }
    close(news)
    wg.Done()
}

Of course, you can rewrite it without using WaitGroups and just use the channels as others have pointed out and that may be a more elegant solution.

Upvotes: 1

kbolino
kbolino

Reputation: 1744

For an explanation of why this happens, the error message you get from the panic tells you the gist of it:

fatal error: all goroutines are asleep - deadlock!

You cannot have all of your goroutines waiting for other goroutines to do something. In this case, when the goroutine running foo is done, and the goroutine running main has received all of the messages (0 to 9) that were sent to the news channel, your program is left with one goroutine waiting to receive a message on a channel that will never have a message sent to it again because there are no other goroutines around to do it.

Upvotes: 2

Related Questions