From: Matt Joiner Date: Mon, 1 Feb 2016 13:45:26 +0000 (+1100) Subject: connection.Close must be called under the Client lock X-Git-Tag: v1.0.0~925 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=19a304b8ea2d7badd60d78778196b23053de06f6;p=btrtrc.git connection.Close must be called under the Client lock --- 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 } }