Skip to content

Commit 8e7e893

Browse files
EstrellaXDclaude
andcommitted
fix(downloader): add retry logic for transient network errors in add_torrents
Handles httpx.ReadError and other network exceptions when adding torrents to qBittorrent by retrying up to 3 times with a 2-second delay. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 2031836 commit 8e7e893

1 file changed

Lines changed: 18 additions & 6 deletions

File tree

backend/src/module/downloader/client/qb_downloader.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,24 @@ async def add_torrents(self, torrent_urls, torrent_files, save_path, category):
135135
else:
136136
files["torrents"] = ("torrent.torrent", torrent_files, "application/x-bittorrent")
137137

138-
resp = await self._client.post(
139-
self._url("torrents/add"),
140-
data=data,
141-
files=files if files else None,
142-
)
143-
return resp.text == "Ok."
138+
max_retries = 3
139+
for attempt in range(max_retries):
140+
try:
141+
resp = await self._client.post(
142+
self._url("torrents/add"),
143+
data=data,
144+
files=files if files else None,
145+
)
146+
return resp.text == "Ok."
147+
except (httpx.ReadError, httpx.ConnectError, httpx.RequestError) as e:
148+
if attempt < max_retries - 1:
149+
logger.warning(
150+
f"[Downloader] Network error adding torrent (attempt {attempt + 1}/{max_retries}): {e}"
151+
)
152+
await asyncio.sleep(2)
153+
else:
154+
logger.error(f"[Downloader] Failed to add torrent after {max_retries} attempts: {e}")
155+
raise
144156

145157
async def torrents_delete(self, hash, delete_files: bool = True):
146158
await self._client.post(

0 commit comments

Comments
 (0)