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

huangapple go评论82阅读模式
英文:

Deadlock while waiting for piped io.Copy in Goroutine

问题

在下面的代码中,对io.Copy的调用永远不会返回;它只会无限期地阻塞,导致死锁。只有在使用io.Pipereadio.Reader连接到os.Stdoutio.Writer时才会出现这种行为。然而,我需要使用管道,因为在我的完整代码中,我正在使用io.MultiWriterio.Pipe将一个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

这是代码的 playground 链接:https://goplay.tools/snippet/70UbGIz8fTV

有没有办法在保持io.Pipe的情况下避免死锁呢?

英文:

In the code below, the call to io.Copy never returns; it just blocks indefinitely, causing a deadlock. This behaviour only occurs when the io.Pipe is used to connect the read io.Reader to the os.Stdout io.Writer. However, I need to use the pipe since, in my full code, I am using io.MultiWriter with io.Pipes to connect one io.Reader to many functions expecting an 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()
}

Output:

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

Here is a playground link to the code: https://goplay.tools/snippet/70UbGIz8fTV

Is there any way to avoid the deadlock while keeping the io.Pipe?

答案1

得分: 5

当完成时,关闭管道的写入端:

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

如果没有这样做,读取端将会无限期等待。

英文:

Close the writing end of the pipe when done:

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

Without that, the reader end will wait indefinitely.

huangapple
  • 本文由 发表于 2023年1月7日 02:04:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75034559.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定