Why goroutine leak

I read Twelve Go Best Practices and came across an interesting example on page 30.

func sendMsg(msg, addr string) error {
    conn, err := net.Dial("tcp", addr)
    if err != nil {
        return err
    }
    defer conn.Close()
    _, err = fmt.Fprint(conn, msg)
    return err
} 

func broadcastMsg(msg string, addrs []string) error {
    errc := make(chan error)
    for _, addr := range addrs {
        go func(addr string) {
            errc <- sendMsg(msg, addr)
            fmt.Println("done")
        }(addr)
    }

    for _ = range addrs {
        if err := <-errc; err != nil {
            return err
        }
    }
    return nil
}

func main() {
    addr := []string{"localhost:8080", "http://google.com"}
    err := broadcastMsg("hi", addr)

    time.Sleep(time.Second)

    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println("everything went fine")
}

The programmer noted that this happens with the code above:

the goroutine is blocked on the chan write
the goroutine holds a reference to the chan
the chan will never be garbage collected

Why is gorutin blocked here? The main thread is blocked until it receives data from goroutine. After that, the for loop. Not?

Why will errc chan never pick up trash? Because I do not close the channel after gorutin is finished?

+4
source share
2 answers

One problem that I see is that inside broadcastMsg()after starting goroutines:

for _ = range addrs {
    if err := <-errc; err != nil {
        return err
    }
}

errc nil error, broadcastMsg() , , , errc .

, , goroutines, :

errc := make(chan error, len(addrs))

nil error, , goroutines:

var errRec error
for _ = range addrs {
    if err := <-errc; err != nil {
        if errRec == nil {
            errRec = err
        }
    }
}
return errRec

, # 33: "", goroutines broadcastMsg() .

+9

(localhost, google). (), goroutine . goroutine ( nil) errc.

- , -, , ( , ).

, :

for _ = range addrs {
    if err := <-errc; err != nil {
        return err
    }
}

, , . , .

+2

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


All Articles