Home > Backend Development > Golang > Why Does My Go Program Deadlock with the 'all goroutines are asleep' Error?

Why Does My Go Program Deadlock with the 'all goroutines are asleep' Error?

DDD
Release: 2024-12-23 02:11:14
Original
905 people have browsed it

Why Does My Go Program Deadlock with the

Go Program deadlock: "throw: all goroutines are asleep"

In a Go program, a deadlock occurs when two or more goroutines (concurrently running functions) wait indefinitely for each other to complete. One such situation can arise when working with channels, as seen in the following program:

package main

import (
    "fmt"
)

func total(ch chan int) {
    res := 0
    for iter := range ch {
        res += iter
    }
    ch <- res
}

func main() {
    ch := make(chan int)
    go total(ch)
    ch <- 1
    ch <- 2
    ch <- 3
    fmt.Println("Total is ", <-ch)
}
Copy after login

Running this program will result in the panic message:

throw: all goroutines are asleep - deadlock!
Copy after login

The root cause of this deadlock lies in the fact that the total goroutine attempts to send a value back to the same channel it's receiving from (ch). Because the channel is not closed (signaling completion), the range loop in the total goroutine will continue indefinitely, blocking any further sends or receives.

To fix this issue, we can introduce another channel to receive the result. Here's an updated program:

package main

import (
    "fmt"
)

func total(in chan int, out chan int) {
    res := 0
    for iter := range in {
        res += iter
    }
    out <- res // Send result on separate channel
}

func main() {
    ch := make(chan int)
    rch := make(chan int) // New channel to receive result
    go total(ch, rch)
    ch <- 1
    ch <- 2
    ch <- 3
    close(ch) // Explicitly close channel to end loop in `total`
    result := <-rch // Wait for result on `rch`
    fmt.Println("Total is ", result)
}
Copy after login

By sending the result on a separate channel and closing the original channel, we break the deadlock and allow the main goroutine to receive the calculated total.

The above is the detailed content of Why Does My Go Program Deadlock with the 'all goroutines are asleep' Error?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template