]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Survive panics while writing chunks
authorMatt Joiner <anacrolix@gmail.com>
Fri, 29 Dec 2017 01:15:33 +0000 (12:15 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Fri, 29 Dec 2017 01:15:33 +0000 (12:15 +1100)
Also improve the comment on that code

connection.go

index b87562157eca88fee3ffc406919c68f53f26f74e..7d94f08b95aa9c6b95f5dcfe65c48b9f72db9c41 100644 (file)
@@ -1061,11 +1061,17 @@ func (c *connection) receiveChunk(msg *pp.Message) {
                c.postCancel(req)
        }
 
-       cl.mu.Unlock()
-       // Write the chunk out. Note that the upper bound on chunk writing
-       // concurrency will be the number of connections.
-       err := t.writeChunk(int(msg.Index), int64(msg.Begin), msg.Piece)
-       cl.mu.Lock()
+       err := func() error {
+               cl.mu.Unlock()
+               defer cl.mu.Lock()
+               // Write the chunk out. Note that the upper bound on chunk writing
+               // concurrency will be the number of connections. We write inline with
+               // receiving the chunk (with this lock dance), because we want to
+               // handle errors synchronously and I haven't thought of a nice way to
+               // defer any concurrency to the storage and have that notify the
+               // client of errors. TODO: Do that instead.
+               return t.writeChunk(int(msg.Index), int64(msg.Begin), msg.Piece)
+       }()
 
        piece.decrementPendingWrites()