fix(debridlink): reuse the file list from the status check (#1431)

Kept small on purpose so it's easy to review.

When a torrent is ready, `get_status` has already fetched the full
record with `/seedbox/list?ids=` and `_handle_torrent_info` has checked
that every file is at 100%. The retrieval thread was then started
without that list, so `_process_and_download` called `_fetch_file_list`,
which asks the same endpoint again. That second call runs inside the
thread's generic `except`, without the flood and transient error
handling the status path has, so a rate limit or a hiccup there sets the
phase to error for a torrent that finished fine.

This passes the files already in hand into the thread.
`_process_and_download` already took an optional `files` argument. The
new test starts the thread from a ready status and checks it uses the
given list and never calls `_fetch_file_list`.

Separately, `download_url` returns a `BytesIO`, so a whole file sits in
memory. AllDebrid, TorBox and Real-Debrid do the same, so I left that
for a streaming variant of the shared helper instead of fixing it for
one client here.

Ruff and basedpyright are clean and `tests/prowlarr` passes.

The one red check is the existing clock-dependent test
`test_a_queued_torrent_asks_for_a_grace_once`, which fails on any runner
that booted recently. It is fixed in #1426 and is not caused by this
change.
This commit is contained in:
splitsec2 authored and GitHub committed 2026-10-20 00:59:00 -04:00
1 parent e8f6505bea
commit be2653bdb7
2 files changed
+35 -6

No files matched your search

+11 -4
View File
@@ -592,7 +592,7 @@ class DebridLinkClient(DownloadClient):
and all(_as_float(f.get("downloadPercent", 100)) >= _COMPLETE_PERCENT for f in files)
)
if ready:
self._maybe_start_download_thread(state)
self._maybe_start_download_thread(state, files)
return DownloadStatus(
progress=50.0,
state=DownloadState.DOWNLOADING,
@@ -657,8 +657,15 @@ class DebridLinkClient(DownloadClient):
with state.lock:
state.start_requested = False
def _maybe_start_download_thread(self, state: _DownloadState) -> None:
"""Spawn a background thread to download the seedbox's files."""
def _maybe_start_download_thread(
self, state: _DownloadState, files: list[dict[str, Any]] | None = None
) -> None:
"""Spawn a background thread to download the seedbox's files.
``files`` is the list the status check already fetched by ``ids``; passing it on
saves the thread a second request that has none of the status path's flood and
transient-error handling.
"""
with state.lock:
already_running = state.phase in ("downloading_http", "complete")
thread_alive = state.download_thread is not None and state.download_thread.is_alive()
@@ -670,7 +677,7 @@ class DebridLinkClient(DownloadClient):
state.progress = 50.0
t = threading.Thread(
target=self._process_and_download,
args=(state,),
args=(state, files),
daemon=True,
)
state.download_thread = t
+24 -2
View File
@@ -256,7 +256,7 @@ class TestStatus:
status = client._handle_torrent_info(partial, state)
assert status.progress == 50.0
started.assert_called_once_with(state)
started.assert_called_once_with(state, partial["files"])
def test_an_empty_file_list_is_not_treated_as_ready(self, tmp_path, monkeypatch):
client = self._bare_client()
@@ -326,7 +326,7 @@ class TestStatus:
state = _state(tmp_path)
DebridLinkClient._downloads["DL1"] = state
release = threading.Event()
monkeypatch.setattr(client, "_process_and_download", lambda _state: release.wait(5))
monkeypatch.setattr(client, "_process_and_download", lambda _state, _files: release.wait(5))
client._maybe_start_download_thread(state)
try:
@@ -339,6 +339,28 @@ class TestStatus:
assert status.message == "Downloading files via HTTP..."
assert status.progress == 50.0
def test_retrieval_reuses_the_files_the_status_check_already_has(self, tmp_path, monkeypatch):
client = self._bare_client()
state = _state(tmp_path)
DebridLinkClient._downloads["DL1"] = state
refetched = MagicMock(side_effect=AssertionError("file list fetched a second time"))
monkeypatch.setattr(client, "_fetch_file_list", refetched)
seen: list[list[dict]] = []
monkeypatch.setattr(
client, "_process_and_download", lambda _state, files=None: seen.append(files)
)
ready = {
"downloadPercent": 100,
"files": [{"name": "Dune.epub", "downloadPercent": 100, "downloadUrl": "https://dl/x"}],
}
client._handle_torrent_info(ready, state)
assert state.download_thread is not None
state.download_thread.join(5)
assert seen == [ready["files"]]
refetched.assert_not_called()
def test_a_vanished_torrent_becomes_an_error_rather_than_a_stall(self, monkeypatch, tmp_path):
monkeypatch.setattr("shelfmark.download.clients.debridlink.TMP_DIR", tmp_path)
_mock_request(monkeypatch, {"success": True, "value": []})