fix issue where peers could be disconnected while iterating over the peer list in the torrent. Specifically while marking peers as not-interesting

This commit is contained in:
arvidn 2017-09-21 03:42:44 -07:00 committed by Arvid Norberg
parent fc4588ad3b
commit ddc8711dbe
1 changed files with 14 additions and 17 deletions

View File

@ -1066,9 +1066,11 @@ namespace libtorrent
if (share_mode()) return; if (share_mode()) return;
if (super_seeding()) return; if (super_seeding()) return;
int idx = 0; // sending "not_interested" to a peer may cause us to disconnect it. Don't
// do that while looping over the peer list.
std::vector<bt_peer_connection*> defer;
for (peer_iterator i = m_connections.begin(); for (peer_iterator i = m_connections.begin();
i != m_connections.end(); ++idx) i != m_connections.end(); ++i)
{ {
TORRENT_INCREMENT(m_iterating_connections); TORRENT_INCREMENT(m_iterating_connections);
// since the call to disconnect_if_redundant() may // since the call to disconnect_if_redundant() may
@ -1078,24 +1080,19 @@ namespace libtorrent
if (p->type() == peer_connection::bittorrent_connection) if (p->type() == peer_connection::bittorrent_connection)
{ {
bt_peer_connection* btp = static_cast<bt_peer_connection*>(*i); bt_peer_connection* btp = static_cast<bt_peer_connection*>(*i);
boost::shared_ptr<peer_connection> me(btp->self());
if (!btp->is_disconnecting()) if (!btp->is_disconnecting())
{ defer.push_back(btp);
btp->send_not_interested();
btp->write_upload_only();
}
} }
}
for (std::vector<bt_peer_connection*>::iterator i = defer.begin()
if (p->is_disconnecting()) , end(defer.end()); i != end; ++i)
{ {
i = m_connections.begin() + idx; bt_peer_connection* btp = *i;
--idx; boost::shared_ptr<peer_connection> me(btp->self());
} btp->send_not_interested();
else if (!btp->is_disconnecting())
{ btp->write_upload_only();
++i;
}
} }
#endif #endif
} }