A downloading torrent dials again the peers it had a working connection with when those connections close. libtorrent (0.16.27 and 0.16.28) never puts a closed connection's peer back on the torrent's list of addresses to dial. The silence check (DownloadWrapper::receive_tick: 240 s without a byte read) asks for it with ConnectionList::disconnect_available, but ConnectionList::erase drops that flag and passes PeerList only disconnect_set_time, and PeerList::disconnected clears the address's port before the line that would put it on the list, so the flag would queue port 0. A close by the peer, a socket error or a poll error asks for nothing at all. Once every connection of a torrent closes at once, the torrent knows its peers and has nothing to dial: a public torrent can find peers again through the DHT, a private one only in its tracker's next answer, which comes on the tracker's schedule. Found 2026-11-13 in a user's diagnostic dump: rtorrent's main thread waited on the disk for 16 minutes (2026-11-12, 04:25 to 04:41 UTC), every peer of his private torrent closed, and the torrent stayed at 0 peers for the 32 minutes after that, until he announced by hand. Reproduced in tools/peer-redial-lab. The change: - A close libtorrent did not choose asks for the peer back (ConnectionList::disconnect_available): the peer closed or reset the connection (close_connection), the socket failed (connection_error, PeerConnectionBase::event_error), or the peer was silent for 240 s (receive_keepalive, which asked before). A protocol error, a blocked connection, a storage error, p.disconnect and p.disconnect_delayed, a torrent's stop, the seeds a finished torrent closes, a peer that sent bad pieces (disconnect_unwanted) and an idle drop (disconnect_idle, libtorrent-unused-peer-slots.patch) ask for nothing. - ConnectionList::erase passes the request on only while the download downloads (active, not complete, not a magnet's metadata) and only for a peer we dialled or one that sent us piece data (ConnectionList::redial_wanted). - PeerList::queue_redial puts the peer's address back on the list with its listen port, not the port the connection came from. Not for a peer without a listen port (it dialled us and never said where it listens), one that sent a bad piece or is banned (failed_counter), an unwanted one (the IP filter) or one dropped as idle; not past the list's size limit; and at most once every 300 s for a peer (PeerInfo::last_redial, PeerList::redial_interval). An address already on the list (a tracker named it before the peer dialled us) counts as queued. It clears the peer's last handshake time: a dial within 600 s of that handshake (a connection that closed in its first ten minutes) would be refused by connect_filter_recent, and the address lost. Each queued peer is a peer_list_events line ("back on the list to dial", or "already on the list to dial"), and so is each peer the interval holds back. - The torrent dials its list as it always did: DownloadMain::receive_connect_peers, on every 30-second tick, while it holds fewer than d.peers_min connections. No setting, and nothing here asks the tracker. Verified by 14 cppunit tests in tests/libtorrent-working-peers-redial.tests.patch (which closes ask for the peer back; the listen port for a peer we dialled, for one that dialled us, for IPv6; the recent-handshake filter after a queue, also for an address already on the list; once in 300 s; never for a bad, banned or idle-dropped peer, past the list's limit or after a plain disconnect), six parts of queue_redial each failing their test when taken out; upstream's suites with every patch (tools/libtorrent-test.sh 9 of 9, tools/rtorrent-test.sh 3 of 3); tools/peer-redial-test.sh (D441): a private torrent whose tracker allows one announce an hour, its client stopped for 5 minutes until every seed dropped it, stays at 0 seeds with 16.27-12P's rtorrent and has all 8 back 32 s after waking up with this patch, while the healthy control dials no seed again; tools/identify-test.sh and tools/peer-rules-test.sh on x86_64 and arm_64. Upstream: rakshasa/libtorrent#1070 (the issue) and #1071 (the pull request), merged 2026-11-14 as 1f6ec20b, after 0.16.28. This file is that commit on 0.16.28 plus the idle drop of libtorrent-unused-peer-slots.patch (disconnect_idle asks for nothing, an idle-dropped peer is not queued). At the first release that contains 1f6ec20b the rest goes into libtorrent-unused-peer-slots.patch and this file is retired. diff --git a/src/protocol/peer_connection_base.cc b/src/protocol/peer_connection_base.cc index eef3827f..0ed64ea4 100644 --- a/src/protocol/peer_connection_base.cc +++ b/src/protocol/peer_connection_base.cc @@ -413,7 +413,8 @@ PeerConnectionBase::cancel_transfer(BlockTransfer* transfer) { void PeerConnectionBase::event_error() { - m_download->connection_list()->erase(this, 0); + // The socket failed (a reset, an unreachable host): the peer's address can be dialled again. + m_download->connection_list()->erase(this, ConnectionList::disconnect_available); } bool diff --git a/src/protocol/peer_connection_leech.cc b/src/protocol/peer_connection_leech.cc index d7d23c7c..5afc120f 100644 --- a/src/protocol/peer_connection_leech.cc +++ b/src/protocol/peer_connection_leech.cc @@ -489,11 +489,16 @@ PeerConnection::event_read() { // Exception handlers: } catch (const close_connection&) { - m_download->connection_list()->erase(this, 0); + // The peer closed the connection (or reset it): its address can be dialled again. + m_download->connection_list()->erase(this, ConnectionList::disconnect_available); } catch (const blocked_connection&) { m_download->connection_list()->erase(this, 0); + } catch (const connection_error& e) { + LT_LOG_NETWORK_ERRORS("network read error: %s : %s", sa_pretty_str(m_peerInfo->socket_address()).c_str(), e.what()); + m_download->connection_list()->erase(this, ConnectionList::disconnect_available); + } catch (const network_error& e) { LT_LOG_NETWORK_ERRORS("network read error: %s : %s", sa_pretty_str(m_peerInfo->socket_address()).c_str(), e.what()); m_download->connection_list()->erase(this, 0); @@ -669,11 +674,16 @@ PeerConnection::event_write() { } while (true); } catch (const close_connection&) { - m_download->connection_list()->erase(this, 0); + // The peer closed the connection (or reset it): its address can be dialled again. + m_download->connection_list()->erase(this, ConnectionList::disconnect_available); } catch (const blocked_connection&) { m_download->connection_list()->erase(this, 0); + } catch (const connection_error& e) { + LT_LOG_NETWORK_ERRORS("network write error: %s : %s", sa_pretty_str(m_peerInfo->socket_address()).c_str(), e.what()); + m_download->connection_list()->erase(this, ConnectionList::disconnect_available); + } catch (const network_error& e) { LT_LOG_NETWORK_ERRORS("network write error: %s : %s", sa_pretty_str(m_peerInfo->socket_address()).c_str(), e.what()); m_download->connection_list()->erase(this, 0); diff --git a/src/torrent/peer/connection_list.cc b/src/torrent/peer/connection_list.cc index 5308bead..e6c7f5d4 100644 --- a/src/torrent/peer/connection_list.cc +++ b/src/torrent/peer/connection_list.cc @@ -8,6 +8,7 @@ #include "net/address_list.h" #include "protocol/peer_connection_base.h" #include "torrent/exceptions.h" +#include "torrent/data/file_list.h" #include "torrent/download_info.h" #include "torrent/download/choke_group.h" #include "torrent/download/choke_queue.h" @@ -95,6 +96,11 @@ ConnectionList::erase(iterator pos, int flags) { PeerConnectionBase* peerConnection = (*pos)->m_ptr(); + auto info = m_download->info(); + bool downloading = info->is_active() && !info->is_meta_download() && !m_download->file_list()->is_done(); + bool redial = redial_wanted(flags, downloading, peerConnection->peer_info()->is_incoming(), + peerConnection->c_peer_chunks()->download_throttle()->rate()->total()); + // The connection must be erased from the list before the signal is // emited otherwise some listeners might do stuff with the // assumption that the connection will remain in the list. @@ -111,7 +117,8 @@ ConnectionList::erase(iterator pos, int flags) { peerConnection->mutable_peer_info()->set_connection(NULL); m_download->peer_list()->disconnected(peerConnection->mutable_peer_info(), - PeerList::disconnect_set_time | ((flags & disconnect_idle) ? PeerList::disconnect_idle : 0)); + PeerList::disconnect_set_time | ((flags & disconnect_idle) ? PeerList::disconnect_idle : 0) | + (redial ? PeerList::disconnect_available : 0)); // Delete after the signal to ensure the address of 'v' doesn't get // allocated for a different PCB in the signal. @@ -119,6 +126,14 @@ ConnectionList::erase(iterator pos, int flags) { return pos; } +bool +ConnectionList::redial_wanted(int flags, bool downloading, bool incoming, uint64_t received) { + if (!(flags & disconnect_available) || (flags & (disconnect_unwanted | disconnect_idle))) + return false; + + return downloading && (!incoming || received != 0); +} + void ConnectionList::erase(Peer* p, int flags) { erase(std::find(begin(), end(), p), flags); diff --git a/src/torrent/peer/connection_list.h b/src/torrent/peer/connection_list.h index 9d464f47..f6a2af72 100644 --- a/src/torrent/peer/connection_list.h +++ b/src/torrent/peer/connection_list.h @@ -71,6 +71,12 @@ public: void erase(PeerInfo* peerInfo, int flags); void erase_remaining(iterator pos, int flags); + + // Whether erase() puts a closed connection's peer back on the download's list to dial (PeerList::queue_redial + // then checks the peer itself): a close libtorrent did not choose (disconnect_available: the peer closed the + // connection, the socket failed or the peer went silent), while the download still downloads, and only a peer we + // dialled or one that sent us piece data. Pure: the tests drive it. + static bool redial_wanted(int flags, bool downloading, bool incoming, uint64_t received); void erase_seeders(); iterator find(const char* id); diff --git a/src/torrent/peer/peer_info.h b/src/torrent/peer/peer_info.h index b223b265..12ac2fbc 100644 --- a/src/torrent/peer/peer_info.h +++ b/src/torrent/peer/peer_info.h @@ -63,6 +63,10 @@ public: uint32_t last_handshake() const { return m_lastHandshake; } void set_last_handshake(uint32_t tvsec) { m_lastHandshake = tvsec; } + // When PeerList last put the peer back on the list to dial after a connection closed (0: never). + uint32_t last_redial() const { return m_lastRedial; } + void set_last_redial(uint32_t tvsec) { m_lastRedial = tvsec; } + bool supports_dht() const { return m_options[7] & 0x01; } bool supports_extensions() const { return m_options[5] & 0x10; } @@ -106,6 +110,7 @@ private: uint32_t m_transferCounter{0}; uint32_t m_lastConnection{0}; uint32_t m_lastHandshake{0}; + uint32_t m_lastRedial{0}; uint16_t m_listenPort{0}; diff --git a/src/torrent/peer/peer_list.cc b/src/torrent/peer/peer_list.cc index 02206470..366ad176 100644 --- a/src/torrent/peer/peer_list.cc +++ b/src/torrent/peer/peer_list.cc @@ -392,8 +392,7 @@ PeerList::disconnected(iterator itr, int flags) { itr->second->unset_flags(PeerInfo::flag_connected); - // Replace the socket address port with the listening port so that - // future outgoing connections will connect to the right port. + // The port the connection came from is no use to a dial; queue_redial() takes the listen port. itr->second->set_port(0); if (flags & disconnect_set_time) @@ -404,13 +403,49 @@ PeerList::disconnected(iterator itr, int flags) { itr->second->set_flags(PeerInfo::flag_idle_dropped); } - if (flags & disconnect_available && itr->second->listen_port() != 0) - m_available_list->insert_unique(itr->second->socket_address()); + if (flags & disconnect_available) + queue_redial(itr->second.get()); // Do magic to get rid of unneeded entries. return ++itr; } +void +PeerList::queue_redial(PeerInfo* peer_info) { + auto now = static_cast(this_thread::cached_seconds().count()); + + if (peer_info->listen_port() == 0 || peer_info->failed_counter() != 0 || peer_info->is_unwanted() || + peer_info->is_idle_dropped()) + return; + + if (peer_info->last_redial() != 0 && now - peer_info->last_redial() < redial_interval) { + LT_LOG_EVENTS("disconnected: not dialled again, dialled again %" PRIu32 " s ago : %s", + now - peer_info->last_redial(), sa_pretty_str(peer_info->socket_address()).c_str()); + return; + } + + if (m_available_list->size() >= m_available_list->max_size()) + return; + + auto sa = sa_copy(peer_info->socket_address()); + sa_set_port(sa.get(), peer_info->listen_port()); + + // Already on the list (a tracker named it before it dialled us) counts as queued: the dial still needs the + // handshake time cleared. + bool added = m_available_list->insert_unique(sa.get()); + + peer_info->set_last_redial(now); + + // A dial within ten minutes of the last handshake would be refused by connect_filter_recent, and the address lost. + peer_info->set_last_handshake(0); + + if (added) { + LT_LOG_EVENTS("disconnected: back on the list to dial : %s", sa_pretty_str(sa.get()).c_str()); + } else { + LT_LOG_EVENTS("disconnected: already on the list to dial : %s", sa_pretty_str(sa.get()).c_str()); + } +} + uint32_t PeerList::cull_peers(int flags) { uint32_t counter = 0; diff --git a/src/torrent/peer/peer_list.h b/src/torrent/peer/peer_list.h index dfa72f77..a196a38e 100644 --- a/src/torrent/peer/peer_list.h +++ b/src/torrent/peer/peer_list.h @@ -107,6 +107,12 @@ public: // idle_peer_rules::refuse_time() after the drop (30 minutes by default), so it cannot take the freed slot straight // back. + // disconnect_available puts the peer's address, on its listen port, back on the list to dial (ConnectionList::erase + // asks for it only when a connection that worked closed while the download downloads). Not for a peer that sent + // a bad piece or is banned, an unwanted or idle-dropped one, nor one without a listen port, and at most once every + // redial_interval seconds for a peer. + static constexpr uint32_t redial_interval = 300; + static constexpr int cull_old = (1 << 0); static constexpr int cull_keep_interesting = (1 << 1); @@ -149,6 +155,8 @@ private: PeerList(const PeerList&) = delete; PeerList& operator=(const PeerList&) = delete; + void queue_redial(PeerInfo* peer_info) LIBTORRENT_NO_EXPORT; + static ipv4_table m_ipv4_table; DownloadInfo* m_info;