66#include < mutex>
77#include < optional>
88#include < ostream>
9+ #include < utility>
910#include " download_service.h"
1011#include " utils/format_utils.h"
1112#include " utils/result.hpp"
@@ -238,7 +239,7 @@ void DownloadService::WorkerThread() {
238239
239240void DownloadService::ProcessTask (DownloadTask& task) {
240241 CTL_INF (" Processing task: " + task.id );
241- std::vector<CURL*> task_handles;
242+ std::vector<std::pair< CURL*, FILE*> > task_handles;
242243
243244 downloading_data_ = std::make_shared<DownloadingData>(DownloadingData{
244245 .item_id = " " ,
@@ -247,7 +248,7 @@ void DownloadService::ProcessTask(DownloadTask& task) {
247248 });
248249
249250 for (auto & item : task.items ) {
250- auto handle = curl_easy_init ();
251+ CURL* handle = curl_easy_init ();
251252 if (handle == nullptr ) {
252253 // skip the task
253254 CTL_ERR (" Failed to init curl!" );
@@ -270,7 +271,7 @@ void DownloadService::ProcessTask(DownloadTask& task) {
270271 curl_easy_setopt (handle, CURLOPT_XFERINFODATA, downloading_data_.get ());
271272
272273 curl_multi_add_handle (multi_handle_, handle);
273- task_handles.push_back (handle);
274+ task_handles.push_back (std::make_pair ( handle, file) );
274275 CTL_INF (" Adding item to multi curl: " + item.ToString ());
275276 }
276277
@@ -296,15 +297,22 @@ void DownloadService::ProcessTask(DownloadTask& task) {
296297
297298 if (stop_flag_) {
298299 CTL_INF (" Download service is stopping.." );
300+
301+ // try to close file
302+ for (auto pair : task_handles) {
303+ fclose (pair.second );
304+ }
305+
299306 return ;
300307 }
301308
302309 ProcessCompletedTransfers ();
303- for (auto handle : task_handles) {
304- curl_multi_remove_handle (multi_handle_, handle );
305- curl_easy_cleanup (handle );
306- downloading_data_. reset ( );
310+ for (auto pair : task_handles) {
311+ curl_multi_remove_handle (multi_handle_, pair. first );
312+ curl_easy_cleanup (pair. first );
313+ fclose (pair. second );
307314 }
315+ downloading_data_.reset ();
308316
309317 RemoveTaskFromStopList (task.id );
310318
0 commit comments