From 7e8a07d228303aea9f51fc005126effd761eb190 Mon Sep 17 00:00:00 2001 From: James Date: Wed, 16 Oct 2024 21:08:08 +0700 Subject: [PATCH] fix: corrupt model download --- engine/services/download_service.cc | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/engine/services/download_service.cc b/engine/services/download_service.cc index 5df2aaa96..831c20f7d 100644 --- a/engine/services/download_service.cc +++ b/engine/services/download_service.cc @@ -6,6 +6,7 @@ #include #include #include +#include #include "download_service.h" #include "utils/format_utils.h" #include "utils/result.hpp" @@ -238,7 +239,7 @@ void DownloadService::WorkerThread() { void DownloadService::ProcessTask(DownloadTask& task) { CTL_INF("Processing task: " + task.id); - std::vector task_handles; + std::vector> task_handles; downloading_data_ = std::make_shared(DownloadingData{ .item_id = "", @@ -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!"); @@ -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()); } @@ -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);