irom
irom

Reputation: 3596

golang sync.WaitGroup doesn't complete on Linux

I have ping function which works fine on Windows, but not on Linux. On Linux it pings few hosts and stops (doesn't exit).

    func main() {
    ...
wg.Add(len(hosts))
    for _, ip := range hosts {
            go ping(ip, &wg, os)        
        }
        wg.Wait()
    ...
    }

I can ping hundreds of hosts on Windows but not on Linux. Look at https://github.com/irom77/go-public/blob/master/gping/main.go for the whole thing

    func ping(ip string, wg *sync.WaitGroup, os string ) {  
        _ , err := exec.Command("ping", os, *PINGCOUNT, "-w", *PINGTIMEOUT, ip).Output()    
        if err == nil {
            count++
            fmt.Printf("%d %s \n", count, ip)
        } 
wg.Done()
    }

When printing result (adding 'result' inside func ping)

result , err := exec.Command("ping", os, *PINGCOUNT, "-w", *PINGTIMEOUT, ip).Output()
fmt.Printf("%s\n", result)

I just got correct output , but it doesnt continue to ping next IPs

....
--- 10.192.167.1 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 476.564/476.564/476.564/0.000 ms

49 10.192.167.1

expecting more IPs here (fine in Windows)

Upvotes: 0

Views: 325

Answers (1)

David Budworth
David Budworth

Reputation: 11626

You may be ignoring panics here, change your ping function to:

func ping(ip string, wg *sync.WaitGroup, os string ) {
    defer wg.Done()
    defer func(){
       if err := recover(); err != nil {
           fmt.Println("Error with:",ip,"err:",err)
       }
    }()
    result , err := exec.Command("ping", os, *PINGCOUNT, "-w", PINGTIMEOUT, ip).Output()
    fmt.Printf("%s\n", result)
    if err == nil {
        count++
        fmt.Printf("%d %s \n", count, ip)
    } else {
        //fmt.Printf("%s is dead\n", ip)
    }
}

this should print the panic, if it happens, as well as guarantee to call wg.Done()

// note: didnt run this, but it's directionally correct

Upvotes: 1

Related Questions