From 19a304b8ea2d7badd60d78778196b23053de06f6 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Tue, 2 Feb 2016 00:45:26 +1100 Subject: [PATCH] connection.Close must be called under the Client lock --- connection.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/connection.go b/connection.go index 419f6a9f..9050c4cf 100644 --- a/connection.go +++ b/connection.go @@ -241,6 +241,7 @@ func (cn *connection) WriteStatus(w io.Writer, t *torrent) { func (c *connection) Close() { c.closed.Set() c.discardPieceInclination() + c.pieceRequestOrder.Clear() // TODO: This call blocks sometimes, why? go c.conn.Close() } @@ -404,6 +405,11 @@ var ( // Writes buffers to the socket from the write channel. func (conn *connection) writer() { + defer func() { + conn.t.cl.mu.Lock() + defer conn.t.cl.mu.Unlock() + conn.Close() + }() // Reduce write syscalls. buf := bufio.NewWriter(conn.rw) for { @@ -417,7 +423,6 @@ func (conn *connection) writer() { connectionWriterWrite.Add(1) _, err := buf.Write(b) if err != nil { - conn.Close() return } case <-conn.closed.C(): @@ -434,7 +439,6 @@ func (conn *connection) writer() { connectionWriterWrite.Add(1) _, err := buf.Write(b) if err != nil { - conn.Close() return } case <-conn.closed.C(): @@ -443,7 +447,6 @@ func (conn *connection) writer() { connectionWriterFlush.Add(1) err := buf.Flush() if err != nil { - conn.Close() return } } -- 2.48.1