Compare commits

...

3 Commits

Author SHA1 Message Date
Acaibrid e54db4ae66
Merge c689440320 into d14d1d0a9c 2024-06-13 10:30:28 +00:00
chai2010 d14d1d0a9c
Update index.hbs 2024-06-12 15:19:50 +08:00
Acaibrid c689440320
Update ch8-05.md
更详细的描述携程泄漏
2024-02-08 07:43:12 +08:00
2 changed files with 4 additions and 3 deletions

View File

@ -98,7 +98,8 @@ func makeThumbnails4(filenames []string) error {
}
```
这个程序有一个微妙的bug。当它遇到第一个非nil的error时会直接将error返回到调用方使得没有一个goroutine去排空errors channel。这样剩下的worker goroutine在向这个channel中发送值时都会永远地阻塞下去并且永远都不会退出。这种情况叫做goroutine泄露§8.4.4可能会导致整个程序卡住或者跑出out of memory的错误。
这个程序有一个微妙的bug。在 makeThumbnails4 函数中,它使用了一个 errors 通道来收集每个协程的错误。然后,在 for range filenames 循环中,它从 errors 通道接收错误,并在遇到非空错误时立即返回该错误。
然而,如果其中一个协程在处理过程中发生了错误,导致向 errors 通道发送了错误,但其他协程仍在继续处理。由于没有其他协程接收这些错误,这些协程将一直阻塞在 errors <- err 退
最简单的解决办法就是用一个具有合适大小的buffered channel这样这些worker goroutine向channel中发送错误时就不会被阻塞。一个可选的解决办法是创建一个另外的goroutine当main goroutine返回第一个错误的同时去排空channel。

View File

@ -186,8 +186,8 @@
<main>
<!-- 头部 -->
<ul dir="auto">
<li><em>凹语言(专为 WebAssembly 设计): <a href="https://github.com/wa-lang/wa">https://github.com/wa-lang/wa</a></em></li>
<li><em>KCL 配置语言(Rust): <a href="https://github.com/kcl-lang/kcl">https://github.com/kcl-lang/kcl</a></em></li>
<li><em>Go语言圣经读书笔记(挖坑中...): <a href="https://github.com/chai2010/gopl-notes-zh">https://github.com/chai2010/gopl-notes-zh</a></em></li>
<li><em>凹语言(Go实现, 面向WASM设计): <a href="https://github.com/wa-lang/wa">https://github.com/wa-lang/wa</a></em></li>
</ul>
<hr>