doupai5450 2018-12-27 16:04
浏览 24
已采纳

如何有效地关闭两个goroutines?

I'm using two concurrent goroutines to copy stdin/stdout from my terminal to a net.Conn target. For some reason, I can't manage to completely stop the two go routines without getting a panic error (for trying to close a closed connection). This is my code:

func interact(c net.Conn, sessionMap map[int]net.Conn) {
    quit := make(chan bool) //the channel to quit

    copy := func(r io.ReadCloser, w io.WriteCloser) {
        defer func() {
            r.Close()
            w.Close()
            close(quit) //this is how i'm trying to close it
        }()

        _, err := io.Copy(w, r)

        if err != nil {
            //
        }

    }

    go func() {
        for {
            select {
            case <-quit:
                return
            default:
                copy(c, os.Stdout)
            }
        }
    }()

    go func() {
        for {
            select {
            case <-quit:
                return
            default:
                copy(os.Stdin, c)
            }
        }
    }()
}

This errors as panic: close of closed channel I want to terminate the two go routines, and then normally proceed to another function. What am I doing wrong?

  • 写回答

2条回答 默认 最新

  • dougou5852 2018-12-27 16:27
    关注

    You can't call close on a channel more than once, there's no reason to call copy in a for loop, since it can only operate one time, and you're copying in the wrong direction, writing to stdin and reading from stdout.

    Simply asking how to quit 2 goroutines is simple, but that's not the only thing you need to do here. Since io.Copy is blocking, you don't need the extra synchronization to determine when the call is complete. This lets you simplify the code significantly, which will make it a lot easier to reason about.

    func interact(c net.Conn) {
        go func() {
            // You want to close this outside the goroutine if you
            // expect to send data back over a half-closed connection
            defer c.Close()
    
            // Optionally close stdout here if you need to signal the
            // end of the stream in a pipeline.
            defer os.Stdout.Close()
    
            _, err := io.Copy(os.Stdout, c)
            if err != nil {
                //
            }
        }()
    
        _, err := io.Copy(c, os.Stdin)
        if err != nil {
            //
        }
    }
    

    Also note that you may not be able to break out of the io.Copy from stdin, so you can't expect the interact function to return. Manually doing the io.Copy in the function body and checking for a half-closed connection on every loop may be a good idea, then you can break out sooner and ensure that you fully close the net.Conn.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看