在 Goroutine 中等待管道 io.Copy 时发生死锁

2024年 2月 15日 36.1k 0

在 goroutine 中等待管道 io.copy 时发生死锁

在 Goroutine 中等待管道 io.Copy 时发生死锁是一个常见的问题。当我们在一个 Goroutine 中等待 io.Copy 的完成时,如果管道没有被正确地关闭,就会导致死锁。这种情况下,Goroutine 会一直在等待数据,而无法继续执行下去。解决这个问题的方法是,在 io.Copy 完成后,手动关闭管道,以确保 Goroutine 可以正确结束。php小编苹果为你详细介绍了这个问题的原因和解决方法,希望能帮助你更好地处理这类死锁情况。

问题内容

在下面的代码中,对 io.copy 的调用永远不会返回;它只是无限期地阻塞,导致死锁。仅当使用 io.pipeio.reader 连接到 os.stdout io.writer 时,才会发生此行为。但是,我需要使用管道,因为在我的完整代码中,我使用 io.multiwriterio.pipes 将一个 io.reader 连接到许多需要 io.reader 的函数。

func main() {
read := strings.newreader("abcdefghij")
pipereader, pipewriter := io.pipe()

var wg sync.waitgroup
wg.add(1)
go func() {
println("start copy")
_, err := io.copy(os.stdout, pipereader)
if err != nil {
println(err.error())
}
println("end copy")
wg.done()
}()

_, err := io.copy(pipewriter, read)
if err != nil {
println(err.error())
}

wg.wait()
}

登录后复制

输出:

Start copy
abcdefghij
fatal error: all goroutines are asleep - deadlock!

goroutine 1 [semacquire]:
sync.runtime_Semacquire(0xc0000b0018?)
/usr/local/go-faketime/src/runtime/sema.go:62 +0x25
sync.(*WaitGroup).Wait(0x4969c8?)
/usr/local/go-faketime/src/sync/waitgroup.go:139 +0x52
main.main()
/tmp/sandbox4108076976/prog.go:31 +0x23c

goroutine 18 [select]:
io.(*pipe).read(0xc0000a6120, {0xc0000b6000, 0x8000, 0xc00009e101?})
/usr/local/go-faketime/src/io/pipe.go:57 +0xb1
io.(*PipeReader).Read(0x10?, {0xc0000b6000?, 0xc00009e1e0?, 0x4f75a0?})
/usr/local/go-faketime/src/io/pipe.go:136 +0x25
io.copyBuffer({0x496aa8, 0xc00009e1e0}, {0x4969a8, 0xc0000a4018}, {0x0, 0x0, 0x0})
/usr/local/go-faketime/src/io/io.go:427 +0x1b2
io.Copy(...)
/usr/local/go-faketime/src/io/io.go:386
os.genericReadFrom(0xb000000006018ab?, {0x4969a8, 0xc0000a4018})
/usr/local/go-faketime/src/os/file.go:162 +0x67
os.(*File).ReadFrom(0xc0000a4008, {0x4969a8, 0xc0000a4018})
/usr/local/go-faketime/src/os/file.go:156 +0x1b0
io.copyBuffer({0x496a28, 0xc0000a4008}, {0x4969a8, 0xc0000a4018}, {0x0, 0x0, 0x0})
/usr/local/go-faketime/src/io/io.go:413 +0x14b
io.Copy(...)
/usr/local/go-faketime/src/io/io.go:386
main.main.func1()
/tmp/sandbox4108076976/prog.go:18 +0x71
created by main.main
/tmp/sandbox4108076976/prog.go:16 +0x1d3

登录后复制

这里是代码的游乐场链接:https://goplay.tools/snippet/70ubgiz8ftv

有没有办法在保留 io.pipe 的同时避免死锁?

解决方法

完成后关闭管道的写入端:

_, err := io.Copy(pipeWriter, read)
pipeWriter.Close()
if err != nil {
println(err.Error())
}

登录后复制

否则,读者端将无限期地等待。

以上就是在 Goroutine 中等待管道 io.Copy 时发生死锁的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

如何删除WordPress中的所有评论
检查WordPress服务器磁盘使用情况的7种简便方法(查找大文件和数据)
如何更改WordPress常量FS_METHOD
如何为区块编辑器、Elementor等构建WordPress文章模板
如何彻底地删除WordPress主题及相关内容
如何使用WordPress搭建一个内网

发布评论