client.go | 2 +- connection.go | 11 +++++++++-- diff --git a/client.go b/client.go index 11e855765382edb6196b28d4fbb4e41d2f5f6c91..c12d297b3a72f203403d3b78a9e98fb009bb2b66 100644 --- a/client.go +++ b/client.go @@ -325,7 +325,6 @@ Socket: sock, 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 @@ defer me.mu.Unlock() 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 f507806ad2e36f34ab6f103865ce49006bac9f5c..d7654e7d0ea98e939d03b116935d031cfc2a7490 100644 --- a/connection.go +++ b/connection.go @@ -131,13 +131,20 @@ } 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 {