Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why is this a deadlock in golang / waitgroup?

Tags:

go

I'm not sure what I'm missing but I get a deadlock error. I'm using a buffered channel that I range over after all go routines complete. The channel has the capacity of 4 and I'm running 4 go routines so I'm expecting it to be "closed" automatically once it reaches the max capacity.

package main

import "fmt"
import "sync"

func main() {
    ch := make(chan []int, 4)
    var m []int

    var wg sync.WaitGroup
    for i := 0; i < 5; i++ {
        wg.Add(1)
        go func() {
            defer wg.Done()
            ch <- m
            return
        }()
    }
    wg.Wait()

    for c := range ch {
        fmt.Printf("c is %v", c)
    }
}
like image 633
hey Avatar asked Jul 07 '14 11:07

hey


2 Answers

You have two problems :

  • there's not enough place for all the goroutines as your channel is too small : when your channel is full, the remaining goroutines must wait for a slot to be freed
  • range ch is still waiting for elements to come in the channel and there's no goroutine left to write on it.

Solution 1 :

Make the channel big enough and, close it so that range stops waiting :

ch := make(chan []int, 5)
...
wg.Wait()
close(ch)

Demonstration

This works but this mostly defeats the purpose of channels here as you don't start printing before all tasks are done.

Solution 2 : This solution, which would allow a real pipelining (that is a smaller channel buffer), would be to do the Done() when printing :

func main() {
    ch := make(chan []int, 4)
    var m []int

    var wg sync.WaitGroup
    for i := 0; i < 5; i++ {
        wg.Add(1)
        go func() {
            ch <- m
            return
        }()
    }
    go func() {
        for c := range ch {
            fmt.Printf("c is %v\n", c)
            wg.Done()
        }
    }()
    wg.Wait()
}

Demonstration

like image 123
Denys Séguret Avatar answered Sep 29 '22 02:09

Denys Séguret


I'm running 4 go routines

No, you're running 5 - thus the deadlock, as the channel buffers only 4 messages.

However, iterating over the channel will later on deadlock the program too. Since the channel isn't closed, it'll block forever once it read your 5 values.

So,

ch := make(chan []int, 5)

and

close(ch) 

before the range loop.

like image 25
nos Avatar answered Sep 29 '22 02:09

nos