TruckDriver
TruckDriver

Reputation: 1456

Why the below go program gives deadlock error "fatal error: all goroutines are asleep - deadlock!"

I am new to golang and was trying out some typical producer consumer using channels. I understand that both the producer and consumer should write to and read from same channel. But just for experimentation I made them write to and read from different channels like below

package main
    
    import (
        "fmt"
        "sync"
    )
    
    func main() {
    
        var wg sync.WaitGroup
        wg.Add(2)
    
        fmt.Println("Starting the main application")
    
        channel :=make( chan int)
        channel1 :=make( chan int)
    
    
        go generateNumbersV2(&wg, channel)
        go printNumbersV2(&wg, channel1)
    
    
        fmt.Println("Waiting for other goroutines")
        wg.Wait()
        //close()
        //close(channel)
    
        fmt.Println("All goroutines finished")
    }
    
    func printNumbersV2(wg *sync.WaitGroup, rc <-chan int) {
        defer wg.Done()
    
        for idx:=0 ;idx<3;idx++ {
    
            val := <-rc
            fmt.Println("******value received from channel ",val)
        }
    }
    
    func generateNumbersV2(wg *sync.WaitGroup, wc chan<- int) {
        defer wg.Done()
    
        for idx:=0 ;idx<3;idx++ {
    
             wc<-idx
            fmt.Println("###value written to channel ",idx)
        }
    
    }

And when I run the program , I get the below error.

fatal error: all goroutines are asleep - deadlock!

Now , While I understand that both the goroutines are blocked one on the read call to channel1 and the other on write call to channel, so the program would never terminate. But my question is what if actually am waiting for the values in those channels, should not the program wait indefinitely rather than declaring it as deadlock. What if later on maybe , due to some network read/write, the values arrive and and some other go routines write to these channels.

Upvotes: 0

Views: 1147

Answers (2)

Mads Akselsen
Mads Akselsen

Reputation: 429

generateNumbersV2 is sending data to channel - but there are no receiver detected by Go.

printNumbersV2 is waiting to receive data from channel1 - but there are no producer detected by Go.

It is correct that the program will wait for data that might arrive at some point, e.g. a network request. If you create such a function, the deadlock error will go away.

A simple fix for illustration purposes:

channel: = make(chan int)

go generateNumbersV2(& wg, channel)
go printNumbersV2(& wg, channel)

Upvotes: 1

Burak Serdar
Burak Serdar

Reputation: 51512

A running program will terminate with deadlock panic only if all goroutines are blocked on a synchronization primitive. If all goroutines are blocked waiting for a channel operation and/or mutex lock, there cannot be a network reception because there are no goroutines listening from a network connection. That also means, in a program with many goroutines, you may have many deadlocked goroutine groups, but the program keeps on running, because there are other goroutines than can still progress.

Upvotes: 2

Related Questions