dqq46733 2015-09-08 14:38
浏览 450
已采纳

Golang 1.5 io.Copy被两个TCPConn阻止

http://play.golang.org/p/gZo5RqgY4F

I have a question with io.Copy method. The link above will block in line 44 under Go 1.5. But will pass in 1.4.2. I have no idea with this issue.

Here is my go version: go version go 1.5 darwin/amd64.

When did the io.Copy return in go 1.5?

  • 写回答

1条回答 默认 最新

  • duanjiangzhi6851 2015-09-08 15:38
    关注

    Previously, you were getting lucky when the timing of the syscalls would cause a write error in your second io.Copy [line 41]. (Ignoring errors tends to hide bugs)

    This was purely by accident (and may even be incorrect). Since the source connection of that copy (conn2) is never closed, the io.Copy never receives an io.EOF and doesn't return. You need to close the opposing connection in each of the copy goroutines to unblock the other's call to io.Copy.

    wg.Add(1)
    go func() {
        io.Copy(conn1, conn2)
        // conn2 has returned EOF or an error, so we need to shut down the
        // other half of the duplex copy.
        conn1.Close()
        wg.Done()
    }()
    
    wg.Add(1)
    go func() {
        io.Copy(conn2, conn1)
        conn2.Close()
        wg.Done()
    }()
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?