mirror of
https://github.com/qbittorrent/qBittorrent
synced 2025-08-19 21:03:30 -07:00
Avoid race conditions when adding torrent
This commit is contained in:
parent
a466ff5057
commit
cdcc7a210b
1 changed files with 16 additions and 12 deletions
|
@ -2147,20 +2147,26 @@ bool Session::addTorrent_impl(CreateTorrentParams params, const MagnetUri &magne
|
||||||
if (fromMagnetUri) {
|
if (fromMagnetUri) {
|
||||||
hash = magnetUri.hash();
|
hash = magnetUri.hash();
|
||||||
|
|
||||||
if (m_loadedMetadata.contains(hash)) {
|
const auto it = m_loadedMetadata.constFind(hash);
|
||||||
/// Adding preloaded torrent...
|
if (it != m_loadedMetadata.constEnd()) {
|
||||||
m_addingTorrents.insert(hash, params);
|
// 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);
|
lt::torrent_handle handle = m_nativeSession->find_torrent(hash);
|
||||||
// We need to pause it first to create TorrentHandle within the same
|
// We need to pause it first to create TorrentHandle within the same
|
||||||
// underlying state as in other cases.
|
// underlying state as in other cases.
|
||||||
|
handle.auto_managed(false);
|
||||||
|
handle.pause();
|
||||||
|
|
||||||
try {
|
m_loadedMetadata.remove(hash);
|
||||||
handle.auto_managed(false);
|
m_addingTorrents.insert(hash, params);
|
||||||
handle.pause();
|
|
||||||
}
|
|
||||||
catch (std::exception &) {}
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4349,16 +4355,14 @@ void Session::handleTorrentPausedAlert(const libtorrent::torrent_paused_alert *p
|
||||||
{
|
{
|
||||||
const InfoHash hash {p->handle.info_hash()};
|
const InfoHash hash {p->handle.info_hash()};
|
||||||
|
|
||||||
if (m_loadedMetadata.contains(hash)) {
|
if (m_addingTorrents.contains(hash)) {
|
||||||
// Adding preloaded torrent
|
// Adding preloaded torrent
|
||||||
m_loadedMetadata.remove(hash);
|
|
||||||
lt::torrent_handle handle = p->handle;
|
lt::torrent_handle handle = p->handle;
|
||||||
--m_extraLimit;
|
--m_extraLimit;
|
||||||
|
|
||||||
// Preloaded torrent is in "Upload mode" so we need to disable it
|
// Preloaded torrent is in "Upload mode" so we need to disable it
|
||||||
// otherwise the torrent never be downloaded (until application restart)
|
// otherwise the torrent never be downloaded (until application restart)
|
||||||
handle.set_upload_mode(false);
|
handle.set_upload_mode(false);
|
||||||
handle.auto_managed(true);
|
|
||||||
|
|
||||||
adjustLimits();
|
adjustLimits();
|
||||||
// use common 2nd step of torrent addition
|
// use common 2nd step of torrent addition
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue