From 7dd532d46fa84144c63e5ad6aa4d976a08d0b25b Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Tue, 26 Jun 2018 13:04:15 +1000 Subject: [PATCH] Check that chunks we request aren't being hashed or queued for hash --- connection.go | 6 ++++++ misc.go | 2 ++ torrent.go | 8 ++++++++ 3 files changed, 16 insertions(+) diff --git a/connection.go b/connection.go index 6e9fd028..cea8dab6 100644 --- a/connection.go +++ b/connection.go @@ -498,6 +498,12 @@ func (cn *connection) request(r request, mw messageWriter) bool { panic("requesting while choked and not allowed fast") } } + if cn.t.hashingPiece(pieceIndex(r.Index)) { + panic("piece is being hashed") + } + if cn.t.pieceQueuedForHash(pieceIndex(r.Index)) { + panic("piece is queued for hash") + } if cn.requests == nil { cn.requests = make(map[request]struct{}) } diff --git a/misc.go b/misc.go index 10df1244..bab40e17 100644 --- a/misc.go +++ b/misc.go @@ -168,3 +168,5 @@ func min(as ...int64) int64 { } var unlimited = rate.NewLimiter(rate.Inf, 0) + +type pieceIndex = int diff --git a/torrent.go b/torrent.go index fe3df93f..5736dee5 100644 --- a/torrent.go +++ b/torrent.go @@ -1776,3 +1776,11 @@ func (t *Torrent) allStats(f func(*ConnStats)) { f(&t.stats) f(&t.cl.stats) } + +func (t *Torrent) hashingPiece(i pieceIndex) bool { + return t.pieces[i].hashing +} + +func (t *Torrent) pieceQueuedForHash(i pieceIndex) bool { + return t.piecesQueuedForHash.Get(bitmap.BitIndex(i)) +} -- 2.48.1