Search code examples
gochannelgoroutine

Go routines and Defer


func main() {
    defer fmt.Println("Main defer")
    go t1()
    go t2()
    go t3()
    time.Sleep(20 * time.Second)

}

func t1() {
    defer fmt.Println("t1 defer")
    time.Sleep(20 * time.Second)
}
func t2() {
    defer fmt.Println("t2 defer")
    time.Sleep(5 * time.Second)
    panic(New("T2"))
}
func t3() {
    defer fmt.Println("t3 defer")
    time.Sleep(20 * time.Second)
}

1 thread (t2) calls panic, t2 defer gets called. when t2 panics, every other thread is also terminated. I want every thread's defer to be called. It's a scenario where panic is must, panic is in one thread.. So I want every thread to be aware that program is going to exit. Any approach I can achieve that?

present output:

t2 defer
panic: T2

goroutine 19 [running]:
main.t2()
        C:/Users/Talha.Irfan/OneDrive - Bentley Systems, Inc/Desktop/go_test/src/main2/main.go:34 +0x105
created by main.main
        C:/Users/Talha.Irfan/OneDrive - Bentley Systems, Inc/Desktop/go_test/src/main2/main.go:21 +0xb0

Solution

  • Nothing will be called after code panics. That's how panic works, your program will exists after panic. You can use channels to send the values to other go routines if panic occurs. One more thing, It is better to use Wait Groups to wait for all go routines to finish rather using time.Sleep.

    package main
    
    import (
        "fmt"
        "sync"
    )
    
    func main() {
        var wg sync.WaitGroup
        defer fmt.Println("Main defer")
        ch := make(chan int)
        wg.Add(1)
        go t1(ch, &wg)
        wg.Add(1)
        go t2(ch, &wg)
        wg.Add(1)
        go t3(ch, &wg)
        ch <- 1
        close(ch)
        wg.Wait()
    }
    
    func t1(ch chan int, wg *sync.WaitGroup) {
        defer fmt.Println("t1 defer")
        defer wg.Done()
    }
    
    func t2(ch chan int,wg *sync.WaitGroup) {
        defer fmt.Println("t2 defer")
        for {
             foo, ok := <- ch
             if !ok {
                    println("done")
                    wg.Done()
                    return
             }
             println(foo)
        }
    }
    func t3(ch chan int, wg *sync.WaitGroup) {
        defer fmt.Println("t3 defer")
        defer wg.Done()
    }
    

    Playground Example

    In case you are getting an error inside the code you can use recover to catch the error. This will led your code to run all goroutines.

    package main
    
    import (
        "fmt"
        "time"
    )
    
    func main() {
        defer fmt.Println("Main defer")
        go t1()
        go t2()
        go t3()
        time.Sleep(2 * time.Second)
    
    }
    
    func t1() {
        defer fmt.Println("t1 defer")
    }
    func t2() {
        defer fmt.Println("t2 defer")
            defer func() {
                if r := recover(); r != nil {
                    println("panic:" + r.(string))
                }
            }()
    }
    func t3() {
        defer fmt.Println("t3 defer")
    }
    

    Working Code on Go playground