Skip to content

Commit

Permalink
Different handling of Download lifetimes. Might help an issue from #1…
Browse files Browse the repository at this point in the history
…3057 (1.10 crash mysteries)
  • Loading branch information
hrydgard committed Jun 28, 2020
1 parent 8ac4efd commit 1a8084c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
24 changes: 14 additions & 10 deletions ext/native/net/http_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -436,12 +436,18 @@ Download::Download(const std::string &url, const std::string &outfile)
}

Download::~Download() {
if (thread_.joinable())
thread_.join();
if (!joined_) {
FLOG("Download destructed without join");
}
}

void Download::Start() {
thread_ = std::thread(std::bind(&Download::Do, this));
}

void Download::Start(std::shared_ptr<Download> self) {
thread_ = std::thread(std::bind(&Download::Do, this, self));
void Download::Join() {
thread_.join();
joined_ = true;
}

void Download::SetFailed(int code) {
Expand Down Expand Up @@ -489,11 +495,8 @@ std::string Download::RedirectLocation(const std::string &baseUrl) {
return redirectUrl;
}

void Download::Do(std::shared_ptr<Download> self) {
void Download::Do() {
setCurrentThreadName("Downloader::Do");
// as long as this is in scope, we won't get destructed.
// yeah this is ugly, I need to think about how life time should be managed for these...
std::shared_ptr<Download> self_ = self;
resultCode_ = 0;

std::string downloadURL = url_;
Expand Down Expand Up @@ -542,7 +545,7 @@ void Download::Do(std::shared_ptr<Download> self) {
std::shared_ptr<Download> Downloader::StartDownload(const std::string &url, const std::string &outfile) {
std::shared_ptr<Download> dl(new Download(url, outfile));
downloads_.push_back(dl);
dl->Start(dl);
dl->Start();
return dl;
}

Expand All @@ -553,7 +556,7 @@ std::shared_ptr<Download> Downloader::StartDownloadWithCallback(
std::shared_ptr<Download> dl(new Download(url, outfile));
dl->SetCallback(callback);
downloads_.push_back(dl);
dl->Start(dl);
dl->Start();
return dl;
}

Expand All @@ -562,6 +565,7 @@ void Downloader::Update() {
for (size_t i = 0; i < downloads_.size(); i++) {
if (downloads_[i]->Progress() == 1.0f || downloads_[i]->Failed()) {
downloads_[i]->RunCallback();
downloads_[i]->Join();
downloads_.erase(downloads_.begin() + i);
goto restart;
}
Expand Down
9 changes: 5 additions & 4 deletions ext/native/net/http_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ class Download {
Download(const std::string &url, const std::string &outfile);
~Download();

// Keeps around an instance of the shared_ptr, so that it doesn't get destructed
// until done.
void Start(std::shared_ptr<Download> self);
void Start();

void Join();

// Returns 1.0 when done. That one value can be compared exactly - or just use Done().
float Progress() const { return progress_; }
Expand Down Expand Up @@ -138,7 +138,7 @@ class Download {
void SetHidden(bool hidden) { hidden_ = hidden; }

private:
void Do(std::shared_ptr<Download> self); // Actually does the download. Runs on thread.
void Do(); // Actually does the download. Runs on thread.
int PerformGET(const std::string &url);
std::string RedirectLocation(const std::string &baseUrl);
void SetFailed(int code);
Expand All @@ -153,6 +153,7 @@ class Download {
bool failed_ = false;
bool cancelled_ = false;
bool hidden_ = false;
bool joined_ = false;
std::function<void(Download &)> callback_;
};

Expand Down

0 comments on commit 1a8084c

Please sign in to comment.