0

As per the definition here, deadlock is related to resource contention.

In an operating system, a deadlock occurs when a process or thread enters a waiting state because a requested system resource is held by another waiting process, which in turn is waiting for another resource held by another waiting process. If a process is unable to change its state indefinitely because the resources requested by it are being used by another waiting process, then the system is said to be in a deadlock.


In the below code:

package main

import "fmt"

func main() {
    c := make(chan string)

    c <- "John"
    fmt.Println("main() stopped")

}

main() go-routine blocks until any other go-routine(no such) reads the same data from that channel.

but the output shows:

$ bin/cs61a 
fatal error: all goroutines are asleep - deadlock!

goroutine 1 [chan send]:
main.main()
        /home/user/../myhub/cs61a/Main.go:8 +0x54
$

edit:

For the point: "the main goroutine–which is blocked, hence all goroutines are blocked, hence it's a deadlock." in the below code, non-main goroutine is also blocked on channel, aren't all goroutines supposed to get blocked?

package main

import (
    "fmt"
    "time"
)

func makeRandom(randoms chan int) {
    var ch chan int
    fmt.Printf("print 1\n")
    <-ch
    fmt.Printf("print 2\n")
}

func main() {

    randoms := make(chan int)

    go makeRandom(randoms)


}

Edit 2:

For your point in the answer: "not all your goroutines are blocked so it's not a deadlock". In the below code, only main() goroutine is blocked, but not worker():

package main

import (
    "fmt"
)

func worker() {

    fmt.Printf("some work\n")

}

func main() {

    ch := make(chan int)

    go worker()
    <-ch

}

and the output says deadlock:

$ bin/cs61a 
some work
fatal error: all goroutines are asleep - deadlock!

goroutine 1 [chan receive]:
main.main()
        /home/user/code/src/github.com/myhub/cs61a/Main.go:18 +0x6f
$

Ideally main() should not exit, because channel resource is used by any one go-routine.

Why a go-routine block on channel considered as deadlock?

2
  • it does not panic because the program exits before the race is caught.
    – user4466350
    Commented May 13, 2020 at 11:18
  • 1
    And removing the sleep still doesn't change anything, because the main goroutine returns. Just because you personally don't think that returning from main should exit the prgram, doesn't change how the language was designed.
    – JimB
    Commented May 13, 2020 at 12:25

2 Answers 2

8

In Go a deadlock is when all existing goroutines are blocked.

Your example has a single goroutine–the main goroutine–which is blocked, hence all goroutines are blocked, hence it's a deadlock.

Note: since all goroutines are blocked, new goroutines will not (cannot) be launched (because they can only be launched from running goroutines). And if all goroutines are blocked and cannot do anything, there is no point in waiting forever for nothing. So the runtime exits.

Edit:

Your edited code where you use a sleep in main is a duplicate of this: Go channel deadlock is not happening. Basically a sleep is not a blocking forever operation (the sleep duration is finite), so a goroutine sleeping is not considered in deadlock detection.

Edit #2:

Since then you removed the sleep() but it doesn't change anything. You have 2 goroutines: the main and the one executing makeRandom(). makeRandom() is blocked and main() isn't. So not all your goroutines are blocked so it's not a deadlock.

Edit #3:

In your last example when the runtime detects the deadlock, then there is only a single goroutine still running: the main(). It's true that you launch a goroutine executing worker(), but that only prints a text and terminates. "Past" goroutines do not count, terminated goroutines also can't do anything to change the blocked state of existing goroutines. Only existing goroutines count.

9
  • Exits or exists? Looks like a typo Commented May 12, 2020 at 19:20
  • @overexchange Right, fixed it.
    – icza
    Commented May 12, 2020 at 19:22
  • 1
    @overexchange Yes, that's correct. What is unclear? If you have a blocked goroutine out of 1 goroutine, then all goroutines are blocked, and that is the definition of Go's deadlock state.
    – icza
    Commented May 12, 2020 at 21:53
  • 1
    @overexchange It doesn't change anything. You have 2 goroutines: the main and the one executing makeRandom(). makeRandom() is blocked and main() isn't. So not all your goroutines are blocked so it's not a deadlock.
    – icza
    Commented May 13, 2020 at 10:47
  • 1
    @overexchange See edited answer. I'm not sure what you want to prove or what you don't understand.
    – icza
    Commented May 13, 2020 at 22:58
0

Check out this article to understand exactly why a go-routine block on channel considered as deadlock: http://dmitryvorobev.blogspot.com/2016/08/golang-channels-implementation.html

In your example above, the main goroutine gets added to the waiting queue(sendq) and cannot be released until Go runs some goroutine that receives a value from the channel.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.