]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Fix goroutine leak for connection.writer
authorMatt Joiner <anacrolix@gmail.com>
Thu, 17 Jul 2014 06:02:30 +0000 (16:02 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Thu, 17 Jul 2014 06:02:30 +0000 (16:02 +1000)
client.go
connection.go

index 11e855765382edb6196b28d4fbb4e41d2f5f6c91..c12d297b3a72f203403d3b78a9e98fb009bb2b66 100644 (file)
--- a/client.go
+++ b/client.go
@@ -325,7 +325,6 @@ func (me *Client) runConnection(sock net.Conn, torrent *torrent, discovery peerS
                Choked:          true,
                PeerChoked:      true,
                write:           make(chan []byte),
-               post:            make(chan pp.Message),
                PeerMaxRequests: 250, // Default in libtorrent is 250.
        }
        defer func() {
@@ -382,6 +381,7 @@ func (me *Client) runConnection(sock net.Conn, torrent *torrent, discovery peerS
        if !me.addConnection(torrent, conn) {
                return
        }
+       conn.post = make(chan pp.Message)
        go conn.writeOptimizer(time.Minute)
        if conn.PeerExtensions[5]&0x10 != 0 {
                conn.Post(pp.Message{
index f507806ad2e36f34ab6f103865ce49006bac9f5c..d7654e7d0ea98e939d03b116935d031cfc2a7490 100644 (file)
@@ -131,13 +131,20 @@ func (cn *connection) WriteStatus(w io.Writer) {
 
 func (c *connection) Close() {
        c.mu.Lock()
+       defer c.mu.Unlock()
        if c.closed {
                return
        }
        c.Socket.Close()
-       close(c.post)
+       if c.post == nil {
+               // writeOptimizer isn't running, so we need to signal the writer to
+               // stop.
+               close(c.write)
+       } else {
+               // This will kill the writeOptimizer, and it kills the writer.
+               close(c.post)
+       }
        c.closed = true
-       c.mu.Unlock()
 }
 
 func (c *connection) getClosed() bool {