From b0b5be913c25693f38bc4d3bf8d84cb4ac62b035 Mon Sep 17 00:00:00 2001 From: Will Miao Date: Wed, 15 Jul 2026 19:12:22 +0800 Subject: [PATCH] fix(downloads): reject re-insertion of download_ids already in history MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In add_to_queue, check download_history before INSERT OR IGNORE. Without this check, a fire-and-forget /queue/complete failure on the extension side would allow the same download_id to be re-inserted after complete_download() deleted it from the queue — creating phantom queued entries for already- finished downloads. --- py/services/download_queue_service.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/py/services/download_queue_service.py b/py/services/download_queue_service.py index 7996078f..4eb22956 100644 --- a/py/services/download_queue_service.py +++ b/py/services/download_queue_service.py @@ -154,13 +154,23 @@ class DownloadQueueService: """Insert a new download into the queue. Returns the inserted row as a dict (or an empty dict if the - download_id already exists). + download_id already exists in the queue or has a terminal + record in history). """ now = time.time() file_params_json = json.dumps(file_params) if file_params is not None else None async with self._lock: conn = self._get_conn() + + # Reject download_ids that already have a terminal record in history. + history_row = conn.execute( + "SELECT 1 FROM download_history WHERE download_id = ? LIMIT 1", + (download_id,), + ).fetchone() + if history_row is not None: + return {} + conn.execute( """ INSERT OR IGNORE INTO download_queue (