GOLANG: fatal error: all goroutines are asleep – deadlock


package main
import (

func runTheCommand(ch chan<- string, cmD string)  {
    ouT,_ := exec.Command("sh","-c",cmD).Output()
    ch <- string(ouT)
// Readln returns a single line (without the ending \n) from the input buffered reader. An error is returned iff there is an error with the buffered reader.
func Readln(r *bufio.Reader) (string, error) {
    var (isPrefix bool = true
        err error = nil 
        line, ln []byte
    for isPrefix && err == nil {
        line, isPrefix, err = r.ReadLine()
        ln = append(ln, line...)
    return string(ln),err

func main() {
    var chans = []chan string{}
    f, _ := os.Open("../tmpStatus.config")
    r := bufio.NewReader(f)
    cmD, e := Readln(r)
    for e == nil {
        ch := make(chan string)
        chans = append(chans, ch) 
        go runTheCommand(ch,cmD)
        cmD,e = Readln(r)

    cases := make([]reflect.SelectCase, len(chans))
    for i, ch := range chans {
        cases[i] = reflect.SelectCase{Dir: reflect.SelectRecv, Chan: reflect.ValueOf(ch)}

    remaining := len(cases)
    for remaining > 0 { 
        chosen, value, ok := reflect.Select(cases)
        if !ok {
            cases[chosen].Chan = reflect.ValueOf(nil)
            remaining -= 1
        fmt.Printf("%s", value.String())

This code is correctly printing out all the received values from the channels.
But in the end it is exiting with the below error :

fatal error: all goroutines are asleep – deadlock!

goroutine 1 [select]: reflect.rselect(0xc200283480, 0x22, 0x22,
0xffffffffffffffff, 0x0, …)
/usr/local/go/src/pkg/runtime/chan.c:1212 +0x10d
reflect.Select(0xc200066800, 0x22, 0x22, 0x1, 0x0, …)
/usr/local/go/src/pkg/reflect/value.go:1957 +0x1fb

I am a newbee at GO. I referred google and somehow managed to write a working code. I didn’t understand fully how this script works especially the reflect package commands. My intention here is to execute the unix commands listed in ../tmpStatus.config file parallely and print out the results. I am excited by the concurrency features offered by GO and hence decided to give it a try. Now it is printing the results so fast. Till now, I was doing this in python one by one.
I ran this script in go version go1.1.2 linux/amd64 installed to a custom location.
Any idea why the deadlock is happening ?


It looks like you are trying to to read from a file and then split the work across multiple workers (fan out). I do not see any closing of channels in your code. See https://gist.github.com/kylewolfe/7c54018a9ed16517c0dd for an example of the pattern I’m referring to. For each line of your file, you would read into the one channel, where x workers would read from it (and reflect / do work).

Answered By – kwolfe

Answer Checked By – Jay B. (GoLangFix Admin)

Leave a Reply

Your email address will not be published.