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

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

Deadlock while waiting for piped io.Copy in Goroutine

问题

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

以下是代码:

  1. func main() {
  2. read := strings.NewReader("abcdefghij")
  3. pipeReader, pipeWriter := io.Pipe()
  4. var wg sync.WaitGroup
  5. wg.Add(1)
  6. go func() {
  7. println("Start copy")
  8. _, err := io.Copy(os.Stdout, pipeReader)
  9. if err != nil {
  10. println(err.Error())
  11. }
  12. println("End copy")
  13. wg.Done()
  14. }()
  15. _, err := io.Copy(pipeWriter, read)
  16. if err != nil {
  17. println(err.Error())
  18. }
  19. wg.Wait()
  20. }

输出:

  1. Start copy
  2. abcdefghij
  3. fatal error: all goroutines are asleep - deadlock!
  4. goroutine 1 [semacquire]:
  5. sync.runtime_Semacquire(0xc0000b0018?)
  6. /usr/local/go-faketime/src/runtime/sema.go:62 +0x25
  7. sync.(*WaitGroup).Wait(0x4969c8?)
  8. /usr/local/go-faketime/src/sync/waitgroup.go:139 +0x52
  9. main.main()
  10. /tmp/sandbox4108076976/prog.go:31 +0x23c
  11. goroutine 18 [select]:
  12. io.(*pipe).read(0xc0000a6120, {0xc0000b6000, 0x8000, 0xc00009e101?})
  13. /usr/local/go-faketime/src/io/pipe.go:57 +0xb1
  14. io.(*PipeReader).Read(0x10?, {0xc0000b6000?, 0xc00009e1e0?, 0x4f75a0?})
  15. /usr/local/go-faketime/src/io/pipe.go:136 +0x25
  16. io.copyBuffer({0x496aa8, 0xc00009e1e0}, {0x4969a8, 0xc0000a4018}, {0x0, 0x0, 0x0})
  17. /usr/local/go-faketime/src/io/io.go:427 +0x1b2
  18. io.Copy(...)
  19. /usr/local/go-faketime/src/io/io.go:386
  20. os.genericReadFrom(0xb000000006018ab?, {0x4969a8, 0xc0000a4018})
  21. /usr/local/go-faketime/src/os/file.go:162 +0x67
  22. os.(*File).ReadFrom(0xc0000a4008, {0x4969a8, 0xc0000a4018})
  23. /usr/local/go-faketime/src/os/file.go:156 +0x1b0
  24. io.copyBuffer({0x496a28, 0xc0000a4008}, {0x4969a8, 0xc0000a4018}, {0x0, 0x0, 0x0})
  25. /usr/local/go-faketime/src/io/io.go:413 +0x14b
  26. io.Copy(...)
  27. /usr/local/go-faketime/src/io/io.go:386
  28. main.main.func1()
  29. /tmp/sandbox4108076976/prog.go:18 +0x71
  30. created by main.main
  31. /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.

  1. func main() {
  2. read := strings.NewReader("abcdefghij")
  3. pipeReader, pipeWriter := io.Pipe()
  4. var wg sync.WaitGroup
  5. wg.Add(1)
  6. go func() {
  7. println("Start copy")
  8. _, err := io.Copy(os.Stdout, pipeReader)
  9. if err != nil {
  10. println(err.Error())
  11. }
  12. println("End copy")
  13. wg.Done()
  14. }()
  15. _, err := io.Copy(pipeWriter, read)
  16. if err != nil {
  17. println(err.Error())
  18. }
  19. wg.Wait()
  20. }

Output:

  1. Start copy
  2. abcdefghij
  3. fatal error: all goroutines are asleep - deadlock!
  4. goroutine 1 [semacquire]:
  5. sync.runtime_Semacquire(0xc0000b0018?)
  6. /usr/local/go-faketime/src/runtime/sema.go:62 +0x25
  7. sync.(*WaitGroup).Wait(0x4969c8?)
  8. /usr/local/go-faketime/src/sync/waitgroup.go:139 +0x52
  9. main.main()
  10. /tmp/sandbox4108076976/prog.go:31 +0x23c
  11. goroutine 18 [select]:
  12. io.(*pipe).read(0xc0000a6120, {0xc0000b6000, 0x8000, 0xc00009e101?})
  13. /usr/local/go-faketime/src/io/pipe.go:57 +0xb1
  14. io.(*PipeReader).Read(0x10?, {0xc0000b6000?, 0xc00009e1e0?, 0x4f75a0?})
  15. /usr/local/go-faketime/src/io/pipe.go:136 +0x25
  16. io.copyBuffer({0x496aa8, 0xc00009e1e0}, {0x4969a8, 0xc0000a4018}, {0x0, 0x0, 0x0})
  17. /usr/local/go-faketime/src/io/io.go:427 +0x1b2
  18. io.Copy(...)
  19. /usr/local/go-faketime/src/io/io.go:386
  20. os.genericReadFrom(0xb000000006018ab?, {0x4969a8, 0xc0000a4018})
  21. /usr/local/go-faketime/src/os/file.go:162 +0x67
  22. os.(*File).ReadFrom(0xc0000a4008, {0x4969a8, 0xc0000a4018})
  23. /usr/local/go-faketime/src/os/file.go:156 +0x1b0
  24. io.copyBuffer({0x496a28, 0xc0000a4008}, {0x4969a8, 0xc0000a4018}, {0x0, 0x0, 0x0})
  25. /usr/local/go-faketime/src/io/io.go:413 +0x14b
  26. io.Copy(...)
  27. /usr/local/go-faketime/src/io/io.go:386
  28. main.main.func1()
  29. /tmp/sandbox4108076976/prog.go:18 +0x71
  30. created by main.main
  31. /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

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

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

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

英文:

Close the writing end of the pipe when done:

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

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:

确定