fixed potential race condition when removing a torrent that was just added

This commit is contained in:
Marcos Pinto 2007-10-03 01:58:50 +00:00
parent 8fa2f73434
commit 3211dc9019
2 changed files with 11 additions and 3 deletions

View File

@ -971,8 +971,7 @@ namespace libtorrent
assert(c.remote() == c.get_socket()->remote_endpoint()); assert(c.remote() == c.get_socket()->remote_endpoint());
peer p(c.remote(), peer::not_connectable, 0); peer p(c.remote(), peer::not_connectable, 0);
m_peers.insert(std::make_pair(c.remote().address(), p)); i = m_peers.insert(std::make_pair(c.remote().address(), p));
i = boost::prior(m_peers.end());
} }
assert(m_torrent->connection_for(c.remote()) == &c); assert(m_torrent->connection_for(c.remote()) == &c);

View File

@ -205,6 +205,15 @@ namespace detail
// lock the session to add the new torrent // lock the session to add the new torrent
session_impl::mutex_t::scoped_lock l(m_ses.m_mutex); session_impl::mutex_t::scoped_lock l(m_ses.m_mutex);
mutex::scoped_lock l2(m_mutex); mutex::scoped_lock l2(m_mutex);
if (m_torrents.empty() || m_torrents.front() != t)
{
// this means the torrent was removed right after it was
// added. Abort the checking.
t.reset();
continue;
}
// clear the resume data now that it has been used // clear the resume data now that it has been used
// (the fast resume data is now parsed and stored in t) // (the fast resume data is now parsed and stored in t)
t->resume_data = entry(); t->resume_data = entry();
@ -214,6 +223,7 @@ namespace detail
{ {
INVARIANT_CHECK; INVARIANT_CHECK;
assert(!m_torrents.empty());
assert(m_torrents.front() == t); assert(m_torrents.front() == t);
t->torrent_ptr->files_checked(t->unfinished_pieces); t->torrent_ptr->files_checked(t->unfinished_pieces);
@ -1760,7 +1770,6 @@ namespace detail
assert(m_torrents.find(i_hash) == m_torrents.end()); assert(m_torrents.find(i_hash) == m_torrents.end());
return; return;
} }
l.unlock();
if (h.m_chk) if (h.m_chk)
{ {