A seed keeps sending pieces to a peer that has ut_pex switched off. Every two minutes (DownloadWrapper::receive_tick, ticks % 4) a torrent with PEX on marks its connections for a peer exchange (DownloadMain::do_peer_exchange, PEX_DO). PeerConnectionBase::send_pex_message() then writes the PEX message only while the peer's ut_pex id is not 0. A peer that announced ut_pex with id 0 (rtorrent does that unless PEX is on and active for the torrent when the connection starts: with PEX off, before the torrent's first two-minute tick, and on a torrent with many connections (PEX turns active at a tick only below half of d.peers_min connections, and off again at d.peers_min); Handshake::write_extension_handshake and ProtocolExtension::generate_handshake_message) fell into the last branch, which cleared the bits and returned true without writing anything. fill_write_buffer() takes true as "a PEX message went out" and skips the rest of its else-if chain: the pending extension reply and the next requested piece. The write buffer stayed empty, event_write() took the connection out of the write poll, and the peer's queued requests stayed unanswered. Without an upload cap the peer's next REQUEST puts the connection back into the write poll at once (read_request_piece, write_insert_poll_safe), so nothing shows. Under a cap the seed holds a full pipeline of the peer's requests (42 to 62 of each rtorrent leecher in the lab), the peer has nothing more to ask, and nothing else wakes the connection: every rtorrent leecher of the torrent stops getting data in the same second, at the first PEX tick after it connected, and gets nothing more. Found 2026-11-24 with tools/client-bench/caplab.py (2 MiB/s cap, three rtorrent and two qBittorrent leechers; qBittorrent with PEX off sends no ut_pex key and never reaches the branch); seen on .11/.12 by seedduel.py as the rtorrent leechers stopping 52-56 s after they connected. The change: the choice moves to PeerConnectionBase::pex_send_step() (toggle, message or nothing), so a test can check it, and send_pex_message() returns false when it wrote nothing, as it already did for an empty PEX message. fill_write_buffer() then goes on to the extension reply and the pieces. PeerConnectionMetadata::fill_write_buffer() has the same chain and now goes on to its pending extension reply as well. Private torrents never run a peer exchange (DownloadInfo::set_pex_enabled refuses them), so they were not affected. Upstream rakshasa/libtorrent master of 2026-11-23 (263efd79c1) has the same branch in send_pex_message(). Verified by 4 cppunit tests in tests/libtorrent-write-pieces-after-empty-pex.tests.patch, upstream's suites with every patch (tools/libtorrent-test.sh, 9 of 9) and docs/notes/upload-cap-2026-11.md (D923): three rtorrent leechers alone under a 2 MiB/s cap got 361.8 MiB in 180 s (100 % of the cap) against 176.0 MiB and nothing after 88 s without it; on the test units the package's rtorrent leechers got data to the end of a 150 s run instead of stopping 52-54 s in. Upstream: rakshasa/libtorrent#1088 (the issue) and #1090 (the pull request, 2026-11-24; the same logic without the helper); retire at the first release that contains it. Applied to 0.16.28 with the same lines at their new places (D926). diff --git a/src/protocol/peer_connection_base.cc b/src/protocol/peer_connection_base.cc index 0ed64ea4..2a95df70 100644 --- a/src/protocol/peer_connection_base.cc +++ b/src/protocol/peer_connection_base.cc @@ -1004,14 +1004,16 @@ PeerConnectionBase::send_pex_message() { return false; } - // Message to tell peer to stop/start doing PEX is small so send it first. - if (m_send_pex_mask & (PEX_ENABLE | PEX_DISABLE)) { + switch (pex_send_step(m_send_pex_mask, m_extensions->id(ProtocolExtension::UT_PEX))) { + case pex_send_toggle: + // Message to tell peer to stop/start doing PEX is small so send it first. write_prepare_extension(ProtocolExtension::HANDSHAKE, ProtocolExtension::generate_toggle_message(ProtocolExtension::UT_PEX, (m_send_pex_mask & PEX_ENABLE) != 0)); m_send_pex_mask &= ~(PEX_ENABLE | PEX_DISABLE); + return true; - } else if (m_send_pex_mask & PEX_DO && m_extensions->id(ProtocolExtension::UT_PEX)) { + case pex_send_message: { const DataBuffer& pexMessage = m_download->get_ut_pex(m_extensions->is_initial_pex()); m_extensions->clear_initial_pex(); @@ -1021,12 +1023,15 @@ PeerConnectionBase::send_pex_message() { return false; write_prepare_extension(ProtocolExtension::UT_PEX, pexMessage); + return true; + } - } else { + case pex_send_nothing: + default: + // The peer has ut_pex off (id 0): nothing written, so the caller goes on to the extension reply and the pieces. m_send_pex_mask = 0; + return false; } - - return true; } // Extension protocol needs to send a reply. diff --git a/src/protocol/peer_connection_base.h b/src/protocol/peer_connection_base.h index ec848724..b2a04074 100644 --- a/src/protocol/peer_connection_base.h +++ b/src/protocol/peer_connection_base.h @@ -125,6 +125,22 @@ public: return buffered != 0 ? idle_read_parse_then_wait : idle_read_wait; } + // What send_pex_message() writes for the pending PEX bits and the peer's ut_pex id: the enable/disable toggle first, + // a PEX message only while the peer has ut_pex on (id not 0), otherwise nothing. Nothing written must not count as a + // sent message: fill_write_buffer() would skip the pending extension reply and the requested pieces, and the + // connection would leave the write poll with the peer's requests unanswered. + enum pex_send_type { pex_send_nothing, pex_send_toggle, pex_send_message }; + + static pex_send_type pex_send_step(int mask, uint8_t remote_id) { + if (mask & (PEX_ENABLE | PEX_DISABLE)) + return pex_send_toggle; + + if ((mask & PEX_DO) && remote_id != 0) + return pex_send_message; + + return pex_send_nothing; + } + void cancel_transfer(BlockTransfer* transfer); // Insert into the poll unless we're blocking for throttling etc.