Skip to content

Commit 22aff31

Browse files
dpagesandeep-edb
authored andcommittedNov 12, 2024
Switch to using a joinable thread for downloads.
With newer versions of the macOS SDK, there's a race condition in which the download thread classes destructor is called before the status has been checked. This causes a successful download to look like it failed. This is a quick hack to fix the problem. We really need to review the code properly and clean it up.
1 parent 4194b8b commit 22aff31

File tree

2 files changed

+6
-18
lines changed

2 files changed

+6
-18
lines changed
 

‎App.cpp

+5-18
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ bool App::Download(const wxString& downloadPath, const Mirror *mirror)
374374
DownloadThread *dt = new DownloadThread();
375375
if (dt == NULL)
376376
{
377-
wxLogError(wxString::Format(_("Failed to create new DownloadThread")));
377+
wxLogError(wxString::Format(_("Failed to create new Download thread")));
378378
return false;
379379
}
380380

@@ -397,12 +397,12 @@ bool App::Download(const wxString& downloadPath, const Mirror *mirror)
397397

398398
if (dt->Create() != wxTHREAD_NO_ERROR)
399399
{
400-
wxLogError(wxString::Format(_("Failed to create Download Thread ")));
400+
wxLogError(wxString::Format(_("Failed to create the Download thread")));
401401
return false;
402402
}
403403
if (dt->Run() != wxTHREAD_NO_ERROR)
404404
{
405-
wxLogError(wxString::Format(_("Couldn't initalise the downloading process")));
405+
wxLogError(wxString::Format(_("Failed to run the Download thread")));
406406
return false;
407407
}
408408

@@ -413,7 +413,7 @@ bool App::Download(const wxString& downloadPath, const Mirror *mirror)
413413
int kbDownloaded;
414414
int kbFileSize;
415415
wxString msg;
416-
bool isDownloadInProgress = false;
416+
417417
/* setting this value will ensure download is in progress. */
418418
dt->SetDownloadInProgress(true);
419419

@@ -442,28 +442,15 @@ bool App::Download(const wxString& downloadPath, const Mirror *mirror)
442442
kbFileSize = (int)dt->GetTotalFileSize() / KBSIZE;
443443
kbDownloaded = (int)dt->GetTotalDownloadSize() / KBSIZE;
444444

445-
if (isDownloadInProgress)
446-
{
447-
if ((kbFileSize == 0) && (kbDownloaded == 0))
448-
{
449-
// Sometime kbDownloaded does not add the last few bytes which causes
450-
// an infinite loop event though the file has completely downloaded
451-
isDownloadInProgress=false;
452-
break;
453-
}
454-
}
455-
456445
// To ignore initial few 0 values which is not updated
457446
if ((kbFileSize == 0) || (kbDownloaded == 0))
458447
continue;
459448

460-
isDownloadInProgress = true;
461-
462449
if (kbFileSize > kbDownloaded)
463450
msg = wxString::Format(_("Downloaded %d KB of %d KB (%ld KB/Sec)"), kbDownloaded, kbFileSize, speed);
464451
else
465452
{
466-
msg = wxString::Format(_("Downloaded %6.0lf KB (%ld KB/Sec)"), kbDownloaded, speed);
453+
dt->Kill();
467454
break;
468455
}
469456

‎CurlHandler.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ static size_t ReadFunctionFromFile(void *ptr, size_t size, size_t nmemb, FILE *s
385385

386386

387387
DownloadThread::DownloadThread():
388+
wxThread(wxTHREAD_JOINABLE),
388389
m_downloadUrl(wxEmptyString), m_downloadFileName(wxEmptyString),
389390
m_totalFileSize(0), m_totalDownloadSize(0), m_isDownloadInProgress(0)
390391
{

0 commit comments

Comments
 (0)
Please sign in to comment.