fix(clients): wait for qBittorrent-compatible clients to finish before post-processing (#1465)

## Summary

`QBittorrentClient.get_status()` treats a torrent as complete as soon as
`progress >= 1.0`, excluding only the `moving` state. Some
qBittorrent-compatible clients report full progress before they are
finished. rdt-client (v2.0.142) reports `progress = 1.0` once the debrid
and local downloads are done, but keeps `state = "downloading"` for a
few seconds while it finalises the torrent, and until then reports a
guessed `content_path`. It only switches to `pausedUP` with the real
path afterwards.

Shelfmark picks the torrent up inside that window, so post-processing
runs against a path that isn't there yet and fails within milliseconds
("No book files found" / `post-processing failed`). Clicking Retry then
imports it fine. Every rdt-client download hits this.

## Change

- Add `_INCOMPLETE_STATES` (all download-side states, plus `moving`) and
only mark a torrent complete when `progress >= 1.0` **and** its state is
not one of those.
- Real qBittorrent leaves these states when a torrent reaches 100%, so
its behaviour is unchanged.

## Example log (before)

```
12:04:43,215 base_handler - Remapped download path for qbittorrent (<hash>): <rdt-client download dir>\<torrent name>\ -> /downloads/<torrent name>
12:04:43,216 orchestrator - download finished; starting post-processing
12:04:43,232 orchestrator - WARNING - post-processing failed
```

## Testing

- New test
`test_get_status_full_progress_still_downloading_not_complete`: fails
before the change, passes after.
- `tests/prowlarr/`: all passing. `ruff check`, `ruff format --check`
and `basedpyright` clean on the changed files.
- Running the patched client against rdt-client: downloads now import
without Retry.

Related to #1463 / #1464, which handle the same symptom when the client
really is finished but files are still syncing to local disk. This PR
addresses a different cause: the client reports 100% progress while
still in a downloading state, so Shelfmark shouldn't treat it as
complete yet. The two changes are independent and complementary. Not
covered by #741, which fixed add-time failures. Possibly related: #607.
This commit is contained in:
ultraviolet150 authored and GitHub committed 2026-10-20 01:11:33 -04:00
1 parent ad62f41edc
commit 5f3647a496
2 files changed
+66 -3

No files matched your search

+22 -3
View File
@@ -50,6 +50,24 @@ _METADATA_DOWNLOAD_STATES = {"forcedMetaDL", "metaDL"}
_METADATA_WAIT_POLLS = 20
_METADATA_WAIT_INTERVAL_SECONDS = 0.5
_ONE_WEEK_IN_SECONDS = 604800
# States that mean a torrent is still downloading or being finalised. Some
# qBittorrent-compatible clients (e.g. rdt-client) report progress 1.0 while
# still in one of these, before the files and content path are final, resulting in
# a file not found error.
_INCOMPLETE_STATES = frozenset(
{
"allocating",
"checkingDL",
"checkingResumeData",
"downloading",
"forcedDL",
"forcedMetaDL",
"metaDL",
"moving",
"queuedDL",
"stalledDL",
}
)
class _UnsafeQBittorrentPath:
@@ -622,9 +640,10 @@ class QBittorrentClient(DownloadClient):
state, message = state_info.get(torrent_state, ("unknown", str(torrent_state)))
torrent_progress = getattr(torrent, "progress", 0.0)
# Don't mark complete while files are being moved to final location
# (qBittorrent moves files from incomplete → complete folder)
complete = torrent_progress >= 1.0 and torrent_state != "moving"
# Don't mark complete while files are still downloading, being finalised,
# or being moved to their final location (qBittorrent moves files from
# incomplete to complete folder).
complete = torrent_progress >= 1.0 and torrent_state not in _INCOMPLETE_STATES
# For active downloads without a special message, leave message as None
# so the handler can build the progress message
+44
View File
@@ -471,6 +471,50 @@ class TestQBittorrentClientGetStatus:
assert status.state_value == "complete"
assert status.file_path == "/downloads/completed.epub"
def test_get_status_full_progress_still_downloading_not_complete(self, monkeypatch):
"""rdt-client reports progress 1.0 while still finalising (state stays "downloading").
The torrent must not be treated as complete until it leaves the download-side
states, or post-processing runs against a content path that does not exist yet.
"""
config_values = {
"QBITTORRENT_URL": "http://localhost:8080",
"QBITTORRENT_USERNAME": "admin",
"QBITTORRENT_PASSWORD": "password",
"QBITTORRENT_CATEGORY": "test",
}
monkeypatch.setattr(
"shelfmark.download.clients.qbittorrent.config.get",
lambda key, default="": config_values.get(key, default),
)
mock_torrent = MockTorrent(
hash_val="abc123",
progress=1.0,
state="downloading",
content_path="/downloads/not-there-yet/",
)
mock_client_instance = MagicMock()
info_payload = mock_torrent.to_dict() | {"save_path": "/downloads"}
mock_client_instance._session.get.return_value = create_mock_session_response(
[info_payload], status_code=200
)
mock_client_class = MagicMock(return_value=mock_client_instance)
with patch.dict("sys.modules", {"qbittorrentapi": MagicMock(Client=mock_client_class)}):
import importlib
import shelfmark.download.clients.qbittorrent as qb_module
importlib.reload(qb_module)
client = qb_module.QBittorrentClient()
status = client.get_status("abc123")
assert status.complete is False
assert status.state_value == "downloading"
assert status.file_path is None
def test_get_status_complete_derives_when_content_path_equals_save_path(self, monkeypatch):
"""Keep get_status() and get_download_path() consistent."""
config_values = {