From b467f15bae2aed3629249a20dd7fcb2d0ded31d0 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Thu, 17 Jul 2014 16:02:30 +1000 Subject: [PATCH] Fix goroutine leak for connection.writer --- client.go | 2 +- connection.go | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/client.go b/client.go index 11e85576..c12d297b 100644 --- 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{ diff --git a/connection.go b/connection.go index f507806a..d7654e7d 100644 --- a/connection.go +++ b/connection.go @@ -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 { -- 2.48.1