From: Matt Joiner Date: Fri, 29 Dec 2017 01:15:33 +0000 (+1100) Subject: Survive panics while writing chunks X-Git-Tag: v1.0.0~310 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=f408e19a7a712913b05731300389383e5cc9a9f2;p=btrtrc.git Survive panics while writing chunks Also improve the comment on that code --- diff --git a/connection.go b/connection.go index b8756215..7d94f08b 100644 --- a/connection.go +++ b/connection.go @@ -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()