Skip to content

Commit 8d2458a

Browse files
committed
ProjectDownloader: Fix asset downloading getting stuck when cancelled
1 parent be9846a commit 8d2458a

File tree

2 files changed

+4
-20
lines changed

2 files changed

+4
-20
lines changed

src/internal/projectdownloader.cpp

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,11 @@ static const std::string ASSET_PREFIX = "https://assets.scratch.mit.edu/internal
1717
static const std::string ASSET_SUFFIX = "/get";
1818

1919
#define CHECK_CANCEL() \
20-
m_cancelMutex.lock(); \
2120
if (m_cancel) { \
2221
m_downloadedAssetCount = 0; \
23-
m_cancelMutex.unlock(); \
2422
std::cout << "Download aborted!" << std::endl; \
2523
return false; \
26-
} \
27-
m_cancelMutex.unlock()
24+
}
2825

2926
ProjectDownloader::ProjectDownloader(IDownloaderFactory *downloaderFactory) :
3027
m_downloaderFactory(downloaderFactory)
@@ -38,9 +35,7 @@ ProjectDownloader::ProjectDownloader(IDownloaderFactory *downloaderFactory) :
3835

3936
bool ProjectDownloader::downloadJson(const std::string &projectId)
4037
{
41-
m_cancelMutex.lock();
4238
m_cancel = false;
43-
m_cancelMutex.unlock();
4439

4540
// Get project token
4641
std::cout << "Fetching project info of " << projectId << std::endl;
@@ -89,9 +84,7 @@ bool ProjectDownloader::downloadJson(const std::string &projectId)
8984

9085
bool ProjectDownloader::downloadAssets(const std::vector<std::string> &assetIds)
9186
{
92-
m_cancelMutex.lock();
9387
m_cancel = false;
94-
m_cancelMutex.unlock();
9588

9689
auto count = assetIds.size();
9790
// unsigned int threadCount = std::thread::hardware_concurrency();
@@ -119,20 +112,14 @@ bool ProjectDownloader::downloadAssets(const std::vector<std::string> &assetIds)
119112

120113
// Download assets
121114
auto f = [this, count](std::shared_ptr<IDownloader> downloader, int index, const std::string &id) {
122-
m_cancelMutex.lock();
123-
124115
if (m_cancel)
125116
return;
126117

127-
m_cancelMutex.unlock();
128-
129118
bool ret = downloader->download(ASSET_PREFIX + id + ASSET_SUFFIX);
130119

131120
if (!ret) {
132121
std::cerr << "Failed to download asset: " << id << std::endl;
133-
m_cancelMutex.lock();
134122
m_cancel = true;
135-
m_cancelMutex.unlock();
136123
return;
137124
}
138125

@@ -178,7 +165,7 @@ bool ProjectDownloader::downloadAssets(const std::vector<std::string> &assetIds)
178165

179166
m_assetsMutex.lock();
180167

181-
if (m_downloadedAssetCount != lastCount) {
168+
if (m_downloadedAssetCount != lastCount && !m_cancel) {
182169
std::cout << "Downloaded assets: " << m_downloadedAssetCount << " of " << count << std::endl;
183170
lastCount = m_downloadedAssetCount;
184171
}
@@ -197,7 +184,7 @@ bool ProjectDownloader::downloadAssets(const std::vector<std::string> &assetIds)
197184
threads.erase(index);
198185
}
199186

200-
if (done) {
187+
if (done || m_cancel) {
201188
for (auto &[index, info] : threads)
202189
info.first.join();
203190

@@ -212,9 +199,7 @@ bool ProjectDownloader::downloadAssets(const std::vector<std::string> &assetIds)
212199

213200
void ProjectDownloader::cancel()
214201
{
215-
m_cancelMutex.lock();
216202
m_cancel = true;
217-
m_cancelMutex.unlock();
218203
m_downloadedAssetCount = 0;
219204
}
220205

src/internal/projectdownloader.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ class ProjectDownloader : public IProjectDownloader
3636
std::vector<std::string> m_assets;
3737
std::mutex m_assetsMutex;
3838
std::atomic<unsigned int> m_downloadedAssetCount = 0;
39-
bool m_cancel = false;
40-
std::mutex m_cancelMutex;
39+
std::atomic<bool> m_cancel = false;
4140
sigslot::signal<unsigned int, unsigned int> m_downloadProgressChanged;
4241
};
4342

0 commit comments

Comments
 (0)