A peer connection parses the messages already in its buffer even when the socket has nothing new to read. libtorrent 0.16.21 and later (rakshasa/libtorrent b0915e09, "performance: size IDLE peer reads by connection type", 2026-07-20; still in 0.16.28 and in master on 2026-11-24) return from PeerConnection::event_read() as soon as a read gives 0 bytes, before read_message() looks at the buffer. Bytes that are already there wait for the peer's next byte: - HandshakeManager::receive_succeeded() hands the new connection what the handshake read past the bitfield (push_unread()) and calls event_read(). qBittorrent and other libtorrent-rasterbar clients send INTERESTED right after their bitfield, in the same packet, so the read finds the socket empty and the INTERESTED stays unparsed. The seed never queues the peer for an upload slot until the peer sends something else: 60 s later from qBittorrent. - the same for any message left in the buffer after an extension message or a piece body. Found 2026-11-24 from the owner's report that qBittorrent seeds better on the same line (lane rt16-upload-cap-1124, docs/notes/upload-cap-2026-11.md): under a 2 MiB/s cap Rtorrent 16 alone sent 92 % of the cap where qBittorrent sent 97 %. The throttle held the cap to the hundredth; the loss was 13 s with nothing sent while the qBittorrent leechers, connected since second 8, waited for their first unchoke until second 66. libtorrent's choke log shows the seed's choke queue empty (queued:0) for that minute and the four peers unchoked together 60 s after their handshake. The change: when the read gives 0 bytes and the buffer still holds data, the messages in it are parsed; an incomplete one waits for the next read event as before, and a new state (a piece body, an extension message) takes what is buffered and then reads the socket itself. The choice is PeerConnectionBase::idle_read_step(), so a test can reach it. Verified by 4 cppunit tests in tests/libtorrent-read-buffered-messages.tests.patch, upstream's suites with every patch (tools/libtorrent-test.sh) and a loopback lab (tools/client-bench/caplab.py: one rtorrent seed capped at 512 KiB/s, four qBittorrent 5.2.4 leechers, 75 s): the seed unchoked them in the second of their handshake and sent 101 % of the cap (38.0 MiB), where the same build without this patch unchoked them 60 s later and sent 23 % (8.6 MiB). Upstream: rakshasa/libtorrent#1087 (the issue) and #1089 (the pull request, 2026-11-24; the same logic without the helper); retire at the first release that contains it. Ported to 0.16.28 (D926): upstream's d21eb535 and 7b882708 moved the connection's up and down members into protocol::StateUp and StateDown (m_up, m_down). Only those names changed here, no logic. diff --git a/src/protocol/peer_connection_base.h b/src/protocol/peer_connection_base.h index 01fe3ba9..554034ec 100644 --- a/src/protocol/peer_connection_base.h +++ b/src/protocol/peer_connection_base.h @@ -110,6 +110,18 @@ public: void push_unread(const void* data, uint32_t size); + // What event_read() does in IDLE after a read gave `length` bytes, with `buffered` bytes already in the buffer + // (push_unread()'s handshake bytes, or what was left after an extension message or a piece body): wait for the + // socket, parse the buffer and then wait, or parse it and read on. + enum idle_read_type { idle_read_wait, idle_read_parse_then_wait, idle_read_parse }; + + static idle_read_type idle_read_step(uint32_t length, uint32_t buffered) { + if (length != 0) + return idle_read_parse; + + return buffered != 0 ? idle_read_parse_then_wait : idle_read_wait; + } + void cancel_transfer(BlockTransfer* transfer); // Insert into the poll unless we're blocking for throttling etc. diff --git a/src/protocol/peer_connection_leech.cc b/src/protocol/peer_connection_leech.cc index 4a0f5639..c44ba9e6 100644 --- a/src/protocol/peer_connection_leech.cc +++ b/src/protocol/peer_connection_leech.cc @@ -373,19 +373,31 @@ PeerConnection::event_read() { return ProtocolRead::buffer_size; }(); + bool socket_empty = false; + if (m_down.protocol->buffer()->size_end() < target) { unsigned int length = read_stream_throws(m_down.protocol->buffer()->end(), target - m_down.protocol->buffer()->size_end()); m_down.protocol->throttle()->node_used_unthrottled(length); - if (length == 0) { + // Nothing new on the socket does not mean nothing to do: the buffer can hold whole messages, such as the + // INTERESTED a peer sent with its bitfield, which the handshake hands over with push_unread(). Left + // unparsed, they wait for the peer's next byte, which can be a minute away. + switch (idle_read_step(length, m_down.protocol->buffer()->remaining())) { + case idle_read_wait: m_down.protocol->buffer()->move_unused(); return; - } - if (is_encrypted()) - m_encryption.decrypt(m_down.protocol->buffer()->end(), length); + case idle_read_parse_then_wait: + socket_empty = true; + break; + + case idle_read_parse: + if (is_encrypted()) + m_encryption.decrypt(m_down.protocol->buffer()->end(), length); - m_down.protocol->buffer()->move_end(length); + m_down.protocol->buffer()->move_end(length); + break; + } } else { // At fill target with no room left but still an incomplete message // (corrupt length / hostile peer). Without this, remaining > 0 would @@ -397,6 +409,17 @@ PeerConnection::event_read() { while (read_message()) ; // Do nothing. + // The socket is empty: an incomplete message waits for the next read event, a new state (a piece body, an + // extension message) takes what is buffered and then reads the socket itself. + if (socket_empty) { + m_down.protocol->buffer()->move_unused(); + + if (m_down.protocol->get_state() == ProtocolRead::IDLE) + return; + + break; + } + // Continue for another fill when we hit the budget, or when an // incomplete message still needs more bytes (poll-churn fix). if (m_down.protocol->buffer()->remaining() > 0 || m_down.protocol->buffer()->size_end() == target) {