关于 defer

2020-04-15 19:32:46 +08:00
 lasuar

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

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

Out

=== RUN   Test_X
panic: 222
2695 次点击
所在节点    Go 编程语言
21 条回复
Mohanson
2020-04-15 19:43:20 +08:00
panic 后面加个 sleep,你应该能看到 111
lasuar
2020-04-15 19:45:07 +08:00
@Mohanson 不行的,都试过
yoshiyuki
2020-04-15 19:46:57 +08:00
主协程偶尔会先于子协程结束,因为协程的运行是异步的,主协程并不会去”等待“子协程的运行结束

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

note:这不是一个 bug,而是协程以及异步本身的特性
lasuar
2020-04-15 19:50:08 +08:00
@yoshiyuki 我知道这不是 bug 。你可以用你说的方式让主协程的 defer 执行,并贴出你的代码。
ClarkAbe
2020-04-15 19:51:35 +08:00
应该是需要等待狗肉挺执行结束
lasuar
2020-04-15 19:53:47 +08:00
贴出一个相同输出的片段:
```
func Test_X(t *testing.T) {
defer func() {
log.Println(111)
}()
go func() {
panic(222)
}()
time.Sleep(time.Second)
}
```
thefack
2020-04-15 19:59:05 +08:00
多运行几次,你就知道 defer 到底有没有执行过
hallDrawnel
2020-04-15 20:00:18 +08:00
不会,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
2020-04-15 20:03:54 +08:00
@hallDrawnel 给力,要的就是这个
hallDrawnel
2020-04-15 20:05:49 +08:00
话说那个小心心是怎么点出来的??网页端可以吗?
lasuar
2020-04-15 20:08:01 +08:00
鼠标移到楼层号左边再左边的位置就会显示了
lasuar
2020-04-15 20:08:12 +08:00
@hallDrawnel 鼠标移到楼层号左边再左边的位置就会显示了
yoshiyuki
2020-04-15 20:21:02 +08:00
func main() {
defer log.Println(111)
go func() {
defer func() {
recover()
}()
panic(222)
}()
}

抱歉对你的问题有曲解,如 8#所言,defer 的执行条件是有 return,所以你需要 recover 以保障主协程中 return 的执行
CEBBCAT
2020-04-15 22:07:41 +08:00
@yoshiyuki 铁子,可以试用下贴 gist 链接的功能,很棒嗷
yoshiyuki
2020-04-15 22:15:29 +08:00
@CEBBCAT 谢谢推荐,有操作指南之类的东西吗?
CEBBCAT
2020-04-16 00:12:30 +08:00
@yoshiyuki 你也许可以站内搜索一下,我明天也有可能发个教程
JamesMackerel
2020-04-16 10:06:01 +08:00
go 这种是对称协程,没有老爸儿子的概念。
davidyanxw
2020-04-16 14:01:49 +08:00
defer ... // 压栈
go func() {panic(222)}() // 子协程

main 协程和子协程执行顺序未知,都有可能先执行结束
main 先执行完:会执行 main 协程中的 defer
子协程先执行完:panic 信息
lasuar
2020-04-17 09:39:42 +08:00
@davidyanxw 你测试一下
davidyanxw
2020-04-17 18:41:33 +08:00
@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 向上抛出

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/662800

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX