跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • 关于 defer
未分類
10 4 月 2020

关于 defer

关于 defer

資深大佬 : lasuar 27

平时没注意,今天偶然发现子协程 panic 后,主协程的 defer 内的操作不会执行?要说捕捉不到子协程的异常可以理解,请问下有相关的文档说明这个现象的吗?

func Test_X(t *testing.T) {  defer log.Println(111)  go func() {   panic(222)  }() } 

Out

=== RUN   Test_X panic: 222 

大佬有話說 (21)

  • 資深大佬 : Mohanson

    panic 后面加个 sleep,你应该能看到 111

  • 主 資深大佬 : lasuar

    @Mohanson 不行的,都试过

  • 資深大佬 : yoshiyuki

    主协程偶尔会先于子协程结束,因为协程的运行是异步的,主协程并不会去”等待“子协程的运行结束

    要解决这个问题,使用 waitGroup 或者 channel & …

    note:这不是一个 bug,而是协程以及异步本身的特性

  • 主 資深大佬 : lasuar

    @yoshiyuki 我知道这不是 bug 。你可以用你说的方式让主协程的 defer 执行,并贴出你的代码。

  • 資深大佬 : ClarkAbe

    应该是需要等待狗肉挺执行结束

  • 主 資深大佬 : lasuar

    贴出一个相同输出的片段:
    “`
    func Test_X(t *testing.T) {
    defer func() {
    log.Println(111)
    }()
    go func() {
    panic(222)
    }()
    time.Sleep(time.Second)
    }
    “`

  • 資深大佬 : thefack

    多运行几次,你就知道 defer 到底有没有执行过

  • 資深大佬 : hallDrawnel

    不会,defer 就是这样设计的,只有在调用 defer 的 goroutine 中 panic 才会执行,其他 goroutine 没有提及,那么就是没有保证。
    文档在 https://golang.org/ref/spec#Defer_statements

    A “defer” statement invokes a function whose execution is deferred to the moment the surrounding function returns, either because the surrounding function executed a return statement, reached the end of its function body, or because the corresponding goroutine is panicking.

    文档指明了在“the corresponding goroutine” panic 的时候才会 defer 。

    为了确定 panic 在一个 goroutine 中的行为,文档也描述了,在 https://golang.org/ref/spec#Handling_panics,我把它贴过来

    While executing a function F, an explicit call to panic or a run-time panic terminates the execution of F. Any functions deferred by F are then executed as usual. Next, any deferred functions run by F’s caller are run, and so on up to any deferred by the top-level function in the executing goroutine. At that point, the program is terminated and the error condition is reported, including the value of the argument to panic. This termination sequence is called panicking.

    注意到”in the executing goroutine”的限制,那么就是说 panic 会触发在它所运行的 goroutine 中调用链上的 defer,然后整个程序就退出了。所以 panic 不会管其他 goroutine 中的 defer 。

  • 主 資深大佬 : lasuar

    @hallDrawnel 给力,要的就是这个

  • 資深大佬 : hallDrawnel

    话说那个小心心是怎么点出来的??网页端可以吗?

  • 主 資深大佬 : lasuar

    鼠标移到层号左边再左边的位置就会显示了

  • 主 資深大佬 : lasuar

    @hallDrawnel 鼠标移到层号左边再左边的位置就会显示了

  • 資深大佬 : yoshiyuki

    func main() {
    defer log.Println(111)
    go func() {
    defer func() {
    recover()
    }()
    panic(222)
    }()
    }

    抱歉对你的问题有曲解,如 8#所言,defer 的执行条件是有 return,所以你需要 recover 以保障主协程中 return 的执行

  • 資深大佬 : CEBBCAT

    @yoshiyuki 铁子,可以试用下贴 gist 链接的功能,很棒嗷

  • 資深大佬 : yoshiyuki

    @CEBBCAT 谢谢推荐,有操作指南之类的东西吗?

  • 資深大佬 : CEBBCAT

    @yoshiyuki 你也许可以站内搜索一下,我明天也有可能发个教程

  • 資深大佬 : JamesMackerel

    go 这种是对称协程,没有老爸儿子的概念。

  • 資深大佬 : davidyanxw

    defer … // 压栈
    go func() {panic(222)}() // 子协程

    main 协程和子协程执行顺序未知,都有可能先执行结束
    main 先执行完:会执行 main 协程中的 defer
    子协程先执行完:panic 信息

  • 主 資深大佬 : lasuar

    @davidyanxw 你测试一下

  • 資深大佬 : davidyanxw

    @lasuar
    我之前的理解有问题。

    输出结果有几种可能:
    1. main 协程先于子协程结束
    输出:111
    2. 子协程先 panic,main 协程未执行到 println(111)
    输出:panic 222 。。。
    3. 子协程先 panic,main 协程执行到 println(111)
    输出:
    111
    panic 222 。。。

    2/3 是同一个逻辑,panic 忽略了上层的 defer

    结论是:
    1. 8 说的,panic 只处理当前协程的 defer 函数
    2. panic 向上抛出

  • 資深大佬 : CEBBCAT

    @yoshiyuki #15 https://www.v2ex.com/t/663565

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具