connection.go | 9 ++++++--- diff --git a/connection.go b/connection.go index 419f6a9fd850312e3f5c24748b9fb6a6a4ba1a55..9050c4cfa079997ac9a1f8d6eea0169f33f0af14 100644 --- a/connection.go +++ b/connection.go @@ -241,6 +241,7 @@ 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 @@ ) // 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 @@ } connectionWriterWrite.Add(1) _, err := buf.Write(b) if err != nil { - conn.Close() return } case <-conn.closed.C(): @@ -434,7 +439,6 @@ } connectionWriterWrite.Add(1) _, err := buf.Write(b) if err != nil { - conn.Close() return } case <-conn.closed.C(): @@ -443,7 +447,6 @@ default: connectionWriterFlush.Add(1) err := buf.Flush() if err != nil { - conn.Close() return } }