Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions engine/services/download_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <mutex>
#include <optional>
#include <ostream>
#include <utility>
#include "download_service.h"
#include "utils/format_utils.h"
#include "utils/result.hpp"
Expand Down Expand Up @@ -238,7 +239,7 @@ void DownloadService::WorkerThread() {

void DownloadService::ProcessTask(DownloadTask& task) {
CTL_INF("Processing task: " + task.id);
std::vector<CURL*> task_handles;
std::vector<std::pair<CURL*, FILE*>> task_handles;

downloading_data_ = std::make_shared<DownloadingData>(DownloadingData{
.item_id = "",
Expand All @@ -247,7 +248,7 @@ void DownloadService::ProcessTask(DownloadTask& task) {
});

for (auto& item : task.items) {
auto handle = curl_easy_init();
CURL* handle = curl_easy_init();
if (handle == nullptr) {
// skip the task
CTL_ERR("Failed to init curl!");
Expand All @@ -270,7 +271,7 @@ void DownloadService::ProcessTask(DownloadTask& task) {
curl_easy_setopt(handle, CURLOPT_XFERINFODATA, downloading_data_.get());

curl_multi_add_handle(multi_handle_, handle);
task_handles.push_back(handle);
task_handles.push_back(std::make_pair(handle, file));
CTL_INF("Adding item to multi curl: " + item.ToString());
}

Expand All @@ -296,15 +297,22 @@ void DownloadService::ProcessTask(DownloadTask& task) {

if (stop_flag_) {
CTL_INF("Download service is stopping..");

// try to close file
for (auto pair : task_handles) {
fclose(pair.second);
}

return;
}

ProcessCompletedTransfers();
for (auto handle : task_handles) {
curl_multi_remove_handle(multi_handle_, handle);
curl_easy_cleanup(handle);
downloading_data_.reset();
for (auto pair : task_handles) {
curl_multi_remove_handle(multi_handle_, pair.first);
curl_easy_cleanup(pair.first);
fclose(pair.second);
}
downloading_data_.reset();

RemoveTaskFromStopList(task.id);

Expand Down
Loading