From 42d7465cbabb4d0be58b166bd374d1d1d8976162 Mon Sep 17 00:00:00 2001 From: Vladimir Golovnev Date: Sat, 28 Oct 2023 10:41:53 +0300 Subject: [PATCH] Prevent crash due to race condition when adding magnet link PR #19792. Closes #19780. Closes #19790. --- src/base/bittorrent/sessionimpl.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/base/bittorrent/sessionimpl.cpp b/src/base/bittorrent/sessionimpl.cpp index aeaa54698..e1011777c 100644 --- a/src/base/bittorrent/sessionimpl.cpp +++ b/src/base/bittorrent/sessionimpl.cpp @@ -2435,6 +2435,11 @@ bool SessionImpl::cancelDownloadMetadata(const TorrentID &id) return false; const lt::torrent_handle nativeHandle = downloadedMetadataIter.value(); + m_downloadedMetadata.erase(downloadedMetadataIter); + + if (!nativeHandle.is_valid()) + return true; + #ifdef QBT_USES_LIBTORRENT2 const InfoHash infoHash {nativeHandle.info_hashes()}; if (infoHash.isHybrid()) @@ -2445,7 +2450,7 @@ bool SessionImpl::cancelDownloadMetadata(const TorrentID &id) m_downloadedMetadata.remove((altID == downloadedMetadataIter.key()) ? id : altID); } #endif - m_downloadedMetadata.erase(downloadedMetadataIter); + m_nativeSession->remove_torrent(nativeHandle, lt::session::delete_files); return true; }