Why Go is a dead end with "fatal error: all gorut sleeps"

Here is the relevant excerpt from my code:

func main() {
    quit := make(chan int)

    readyQueue := make(chan Proc)
    runQueue := make(chan Proc)
    waitQueue := make(chan Proc)

    procList := getInitialProcList()
    fmt.Println(procList)

    for _, proc := range(procList) {
        switch {
            case proc.Status == READY:
                readyQueue <- proc
                tick(quit, readyQueue, runQueue, waitQueue)
            case proc.Status == RUN:
                runQueue <- proc
                tick(quit, readyQueue, runQueue, waitQueue)
            case proc.Status == WAIT:
                waitQueue <- proc
                tick(quit, readyQueue, runQueue, waitQueue)
        }
    }

    <-quit // blocks to keep main thread alive
}

func tick(quit chan int, readyQueue chan Proc, runQueue chan Proc, waitQueue chan Proc) {
    select {
    case p := <-readyQueue:
        fmt.Println(p)
    default:
        fmt.Println("[tick] nothing in ready queue")
    }

    select {
    case p := <-waitQueue:
        fmt.Println(p)
    default:
        fmt.Println("[tick] nothing in wait queue")
    }

    select {
    case p := <-runQueue:
        fmt.Println(p)
    default:
        fmt.Println("[tick] nothing in run queue")
    }

    quit <- 0
}

I do not understand why I am getting an error message fatal error: all goroutines are asleep - deadlock!in a line readyQueue <- procin the above code.

+4
source share
1 answer

As for the code, you never run a parallel reader for any of the channels you create. Since they are not buffered, any writes to them are blocked until someone reads them from the other end. This does not apply to your code.

tick , , . , .

go tick(quit, readyQueue, runQueue, waitQueue)

for _, proc := range(procList) {
    ....
}

1.

quit := make(chan int, 1)

readyQueue := make(chan Proc, 1)
runQueue := make(chan Proc, 1)
waitQueue := make(chan Proc, 1)

, . , , .

+13

Source: https://habr.com/ru/post/1532069/


All Articles