]> Sergey Matveev's repositories - btrtrc.git/commitdiff
connection.Close must be called under the Client lock
authorMatt Joiner <anacrolix@gmail.com>
Mon, 1 Feb 2016 13:45:26 +0000 (00:45 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Mon, 1 Feb 2016 13:45:26 +0000 (00:45 +1100)
connection.go

index 419f6a9fd850312e3f5c24748b9fb6a6a4ba1a55..9050c4cfa079997ac9a1f8d6eea0169f33f0af14 100644 (file)
@@ -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
                                }
                        }