]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Prevent webseed request update while receiving chunk
authorMatt Joiner <anacrolix@gmail.com>
Wed, 21 May 2025 02:43:23 +0000 (12:43 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Wed, 21 May 2025 02:43:23 +0000 (12:43 +1000)
peer.go
webseed-requesting.go

diff --git a/peer.go b/peer.go
index c75577e2bb5d07bf0e93438e46d60684b8c7ae37..6cf7785f0ce7eb2f402debd47c51f8922463f389 100644 (file)
--- a/peer.go
+++ b/peer.go
@@ -11,12 +11,13 @@ import (
 
        "github.com/RoaringBitmap/roaring"
        "github.com/anacrolix/chansync"
-       . "github.com/anacrolix/generics"
        "github.com/anacrolix/log"
        "github.com/anacrolix/missinggo/iter"
-       "github.com/anacrolix/missinggo/v2/bitmap"
        "github.com/anacrolix/multiless"
 
+       . "github.com/anacrolix/generics"
+       "github.com/anacrolix/missinggo/v2/bitmap"
+
        "github.com/anacrolix/torrent/internal/alloclim"
        "github.com/anacrolix/torrent/mse"
        pp "github.com/anacrolix/torrent/peer_protocol"
@@ -69,11 +70,8 @@ type (
 
                lastStartedExpectingToReceiveChunks time.Time
                cumulativeExpectedToReceiveChunks   time.Duration
-               _chunksReceivedWhileExpecting       int64
 
-               choking                                bool
-               piecesReceivedSinceLastRequestUpdate   maxRequests
-               maxPiecesReceivedBetweenRequestUpdates maxRequests
+               choking bool
                // Chunks that we might reasonably expect to receive from the peer. Due to latency, buffering,
                // and implementation differences, we may receive chunks that are no longer in the set of
                // requests actually want. This could use a roaring.BSI if the memory use becomes noticeable.
@@ -508,7 +506,8 @@ func (cn *Peer) onNeedUpdateRequests(reason updateRequestReason) {
                return
        }
        cn.needRequestUpdate = reason
-       cn.handleOnNeedUpdateRequests()
+       // Run this before the Client lock is released.
+       cn.locker().Defer(cn.handleOnNeedUpdateRequests)
 }
 
 // Emits the indices in the Bitmaps bms in order, never repeating any index.
@@ -663,9 +662,6 @@ func (c *Peer) receiveChunk(msg *pp.Message) error {
                // Request has been satisfied.
                if c.deleteRequest(req) || c.requestState.Cancelled.CheckedRemove(req) {
                        intended = true
-                       if !c.peerChoking {
-                               c._chunksReceivedWhileExpecting++
-                       }
                        if c.isLowOnRequests() {
                                c.onNeedUpdateRequests("Peer.receiveChunk deleted request")
                        }
@@ -689,7 +685,6 @@ func (c *Peer) receiveChunk(msg *pp.Message) error {
        c.allStats(add(1, func(cs *ConnStats) *Count { return &cs.ChunksReadUseful }))
        c.allStats(add(int64(len(msg.Piece)), func(cs *ConnStats) *Count { return &cs.BytesReadUsefulData }))
        if intended {
-               c.piecesReceivedSinceLastRequestUpdate++
                c.allStats(add(int64(len(msg.Piece)), func(cs *ConnStats) *Count { return &cs.BytesReadUsefulIntendedData }))
        }
        for _, f := range c.t.cl.config.Callbacks.ReceivedUsefulData {
@@ -707,6 +702,7 @@ func (c *Peer) receiveChunk(msg *pp.Message) error {
        // Cancel pending requests for this chunk from *other* peers.
        if p := t.requestingPeer(req); p != nil {
                if p == c {
+                       p.logger.Slogger().Error("received chunk but still pending request", "peer", p, "req", req)
                        panic("should not be pending request from conn that just received it")
                }
                p.cancel(req)
index 383b14372989303b622ad2a4c9fe9740122f2a6c..66893c119c182c2a5a16cc6ff45524a855dd598f 100644 (file)
@@ -26,10 +26,10 @@ func (cl *Client) abandonedUpdateWebSeedRequests() {
        }
 }
 
-func (cl *Client) updateWebSeedRequests() {
+func (cl *Client) updateWebSeedRequests(reason updateRequestReason) {
        for t := range cl.torrents {
                for _, p := range t.webSeeds {
-                       p.updateRequests()
+                       p.peer.updateRequestsWithReason(reason)
                }
        }
 }