Avoid race conditions when adding torrent

This commit is contained in:
Vladimir Golovnev (Glassez) 2019-07-10 19:54:10 +03:00
commit cdcc7a210b
No known key found for this signature in database
GPG key ID: 52A2C7DEE2DFA6F7

View file

@ -2147,20 +2147,26 @@ bool Session::addTorrent_impl(CreateTorrentParams params, const MagnetUri &magne
if (fromMagnetUri) {
hash = magnetUri.hash();
if (m_loadedMetadata.contains(hash)) {
/// Adding preloaded torrent...
m_addingTorrents.insert(hash, params);
const auto it = m_loadedMetadata.constFind(hash);
if (it != m_loadedMetadata.constEnd()) {
// Adding preloaded torrent...
const TorrentInfo metadata = it.value();
if (metadata.isValid()) {
// Metadata is received and torrent_handle is being deleted
// so we can't reuse it. Just add torrent using its metadata.
return addTorrent_impl(params
, MagnetUri {}, metadata, fastresumeData);
}
// Reuse existing torrent_handle
lt::torrent_handle handle = m_nativeSession->find_torrent(hash);
// We need to pause it first to create TorrentHandle within the same
// underlying state as in other cases.
handle.auto_managed(false);
handle.pause();
try {
handle.auto_managed(false);
handle.pause();
}
catch (std::exception &) {}
m_loadedMetadata.remove(hash);
m_addingTorrents.insert(hash, params);
return true;
}
@ -4349,16 +4355,14 @@ void Session::handleTorrentPausedAlert(const libtorrent::torrent_paused_alert *p
{
const InfoHash hash {p->handle.info_hash()};
if (m_loadedMetadata.contains(hash)) {
if (m_addingTorrents.contains(hash)) {
// Adding preloaded torrent
m_loadedMetadata.remove(hash);
lt::torrent_handle handle = p->handle;
--m_extraLimit;
// Preloaded torrent is in "Upload mode" so we need to disable it
// otherwise the torrent never be downloaded (until application restart)
handle.set_upload_mode(false);
handle.auto_managed(true);
adjustLimits();
// use common 2nd step of torrent addition