@@ -232,6 +232,10 @@ cpp::result<std::string, std::string> DownloadService::StopTask(
232232 if (auto it = active_tasks_.find (task_id); it != active_tasks_.end ()) {
233233 CTL_INF (" Found task " + task_id + " in active tasks" );
234234 it->second ->status = DownloadTask::Status::Cancelled;
235+ {
236+ std::lock_guard<std::mutex> lock (stop_mutex_);
237+ tasks_to_stop_.insert (task_id);
238+ }
235239 EmitTaskStopped (task_id);
236240 return task_id;
237241 }
@@ -368,7 +372,7 @@ void DownloadService::ProcessTask(DownloadTask& task, int worker_id) {
368372cpp::result<void , std::string> DownloadService::ProcessMultiDownload (
369373 DownloadTask& task, CURLM* multi_handle,
370374 const std::vector<std::pair<CURL*, FILE*>>& handles) {
371- int still_running = 0 ;
375+ auto still_running = 0 ;
372376 do {
373377 curl_multi_perform (multi_handle, &still_running);
374378 curl_multi_wait (multi_handle, nullptr , 0 , MAX_WAIT_MSECS, nullptr );
@@ -383,12 +387,16 @@ cpp::result<void, std::string> DownloadService::ProcessMultiDownload(
383387 return cpp::fail (result.error ());
384388 }
385389
386- if (task.status == DownloadTask::Status::Cancelled || stop_flag_) {
387- EmitTaskStopped (task.id );
390+ if (IsTaskTerminated (task.id ) || stop_flag_) {
391+ CTL_INF (" IsTaskTerminated " + std::to_string (IsTaskTerminated (task.id )));
392+ CTL_INF (" stop_flag_ " + std::to_string (stop_flag_));
388393 {
389394 std::lock_guard<std::mutex> lock (event_emit_map_mutex);
390395 event_emit_map_.erase (task.id );
391396 }
397+ CTL_INF (" Emit task stopped: " << task.id );
398+ EmitTaskStopped (task.id );
399+ RemoveTaskFromStopList (task.id );
392400 return cpp::fail (" Task " + task.id + " cancelled" );
393401 }
394402 } while (still_running);
0 commit comments