Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit 24bebed

Browse files
authored
Merge pull request #1689 from janhq/j/prevent-update-event-after-stopped
fix: prevent download event update after stopped
2 parents 3bf5f87 + 65876fb commit 24bebed

File tree

2 files changed

+27
-19
lines changed

2 files changed

+27
-19
lines changed

engine/services/download_service.cc

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,6 @@ cpp::result<std::string, std::string> DownloadService::StopTask(
222222
CTL_INF("Stopping task: " << task_id);
223223
auto cancelled = task_queue_.cancelTask(task_id);
224224
if (cancelled) {
225-
EmitTaskStopped(task_id);
226225
return task_id;
227226
}
228227
CTL_INF("Not found in pending task, try to find task " + task_id +
@@ -236,7 +235,6 @@ cpp::result<std::string, std::string> DownloadService::StopTask(
236235
std::lock_guard<std::mutex> lock(stop_mutex_);
237236
tasks_to_stop_.insert(task_id);
238237
}
239-
EmitTaskStopped(task_id);
240238
return task_id;
241239
}
242240

@@ -356,7 +354,15 @@ void DownloadService::ProcessTask(DownloadTask& task, int worker_id) {
356354
fclose(file);
357355
}
358356

359-
if (!result.has_error()) {
357+
if (result.has_error()) {
358+
if (result.error().type == DownloadEventType::DownloadStopped) {
359+
RemoveTaskFromStopList(task.id);
360+
EmitTaskStopped(task.id);
361+
} else {
362+
EmitTaskError(task.id);
363+
}
364+
} else {
365+
// success
360366
// if the download has error, we are not run the callback
361367
ExecuteCallback(task);
362368
EmitTaskCompleted(task.id);
@@ -369,7 +375,7 @@ void DownloadService::ProcessTask(DownloadTask& task, int worker_id) {
369375
worker_data->downloading_data_map.clear();
370376
}
371377

372-
cpp::result<void, std::string> DownloadService::ProcessMultiDownload(
378+
cpp::result<void, ProcessDownloadFailed> DownloadService::ProcessMultiDownload(
373379
DownloadTask& task, CURLM* multi_handle,
374380
const std::vector<std::pair<CURL*, FILE*>>& handles) {
375381
auto still_running = 0;
@@ -379,25 +385,21 @@ cpp::result<void, std::string> DownloadService::ProcessMultiDownload(
379385

380386
auto result = ProcessCompletedTransfers(multi_handle);
381387
if (result.has_error()) {
382-
EmitTaskError(task.id);
383-
{
384-
std::lock_guard<std::mutex> lock(event_emit_map_mutex);
385-
event_emit_map_.erase(task.id);
386-
}
387-
return cpp::fail(result.error());
388+
return cpp::fail(ProcessDownloadFailed{
389+
.message = result.error(),
390+
.task_id = task.id,
391+
.type = DownloadEventType::DownloadError,
392+
});
388393
}
389394

390395
if (IsTaskTerminated(task.id) || stop_flag_) {
391396
CTL_INF("IsTaskTerminated " + std::to_string(IsTaskTerminated(task.id)));
392397
CTL_INF("stop_flag_ " + std::to_string(stop_flag_));
393-
{
394-
std::lock_guard<std::mutex> lock(event_emit_map_mutex);
395-
event_emit_map_.erase(task.id);
396-
}
397-
CTL_INF("Emit task stopped: " << task.id);
398-
EmitTaskStopped(task.id);
399-
RemoveTaskFromStopList(task.id);
400-
return cpp::fail("Task " + task.id + " cancelled");
398+
return cpp::fail(ProcessDownloadFailed{
399+
.message = result.error(),
400+
.task_id = task.id,
401+
.type = DownloadEventType::DownloadStopped,
402+
});
401403
}
402404
} while (still_running);
403405
return {};

engine/services/download_service.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
#include "common/event.h"
1111
#include "utils/result.hpp"
1212

13+
struct ProcessDownloadFailed {
14+
std::string message;
15+
std::string task_id;
16+
cortex::event::DownloadEventType type;
17+
};
18+
1319
class DownloadService {
1420
private:
1521
static constexpr int MAX_CONCURRENT_TASKS = 4;
@@ -48,7 +54,7 @@ class DownloadService {
4854

4955
void ProcessTask(DownloadTask& task, int worker_id);
5056

51-
cpp::result<void, std::string> ProcessMultiDownload(
57+
cpp::result<void, ProcessDownloadFailed> ProcessMultiDownload(
5258
DownloadTask& task, CURLM* multi_handle,
5359
const std::vector<std::pair<CURL*, FILE*>>& handles);
5460

0 commit comments

Comments
 (0)