From 5b443fb63d8ea42b9826b64456d92d2bf298038c Mon Sep 17 00:00:00 2001
From: Matt Joiner <anacrolix@gmail.com>
Date: Sun, 21 Feb 2016 17:24:59 +1100
Subject: [PATCH] Refresh all piece completion states when data is missing

---
 reader.go  | 4 ++--
 torrent.go | 7 +++++++
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/reader.go b/reader.go
index 0f92343a..f0b81668 100644
--- a/reader.go
+++ b/reader.go
@@ -136,8 +136,8 @@ func (r *Reader) readOnceAt(b []byte, pos int64) (n int, err error) {
 		}
 		log.Printf("%s: error reading from torrent storage pos=%d: %s", r.t, pos, err)
 		r.t.cl.mu.Lock()
-		r.t.torrent.updatePieceCompletion(pi)
-		r.t.torrent.updatePiecePriority(pi)
+		r.t.torrent.updateAllPieceCompletions()
+		r.t.torrent.updatePiecePriorities()
 		r.t.cl.mu.Unlock()
 	}
 }
diff --git a/torrent.go b/torrent.go
index daca65c0..3bbbcef8 100644
--- a/torrent.go
+++ b/torrent.go
@@ -17,6 +17,7 @@ import (
 	"github.com/anacrolix/missinggo/itertools"
 	"github.com/anacrolix/missinggo/perf"
 	"github.com/anacrolix/missinggo/pubsub"
+	"github.com/bradfitz/iter"
 
 	"github.com/anacrolix/torrent/bencode"
 	"github.com/anacrolix/torrent/metainfo"
@@ -1050,3 +1051,9 @@ func (t *torrent) readAt(b []byte, off int64) (n int, err error) {
 	}
 	return t.data.ReadAt(b, off)
 }
+
+func (t *torrent) updateAllPieceCompletions() {
+	for i := range iter.N(t.numPieces()) {
+		t.updatePieceCompletion(i)
+	}
+}
-- 
2.51.0