Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
Fix freezing caused by stuck file lock.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jules-WinnfieldX committed Feb 27, 2024
1 parent 1968b2e commit 2b7080a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
37 changes: 20 additions & 17 deletions cyberdrop_dl/downloader/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,6 @@ async def wrapper(self, *args, **kwargs):
while True:
try:
return await f(self, *args, **kwargs)
except InvalidContentTypeFailure as e:
media_item = args[0]
if not isinstance(media_item.download_task_id, Field):
await self.manager.progress_manager.file_progress.remove_file(media_item.download_task_id)
await log(f"Download Failed: {media_item.url} received Invalid Content", 40)
await log(e.message, 40)
await self.manager.progress_manager.download_stats_progress.add_failure("Invalid Content Type")
await self.manager.progress_manager.download_progress.add_failed()
break

except DownloadFailure as e:
media_item = args[0]
if not isinstance(media_item.download_task_id, Field):
Expand Down Expand Up @@ -75,9 +65,21 @@ async def wrapper(self, *args, **kwargs):
else:
await log(f"Download Failed: {media_item.url} with error {e}", 40)
await log(f"Download Retrying: {media_item.url} with attempt {media_item.current_attempt}", 20)


except InvalidContentTypeFailure as e:
media_item = args[0]
await self._file_lock.release_lock(media_item.file_lock_reference_name)
if not isinstance(media_item.download_task_id, Field):
await self.manager.progress_manager.file_progress.remove_file(media_item.download_task_id)
await log(f"Download Failed: {media_item.url} received Invalid Content", 40)
await log(e.message, 40)
await self.manager.progress_manager.download_stats_progress.add_failure("Invalid Content Type")
await self.manager.progress_manager.download_progress.add_failed()
break

except Exception as e:
media_item = args[0]
await self._file_lock.release_lock(media_item.file_lock_reference_name)
await log(f"Download Failed: {media_item.url} with error {e}", 40)
if not isinstance(media_item.download_task_id, Field):
await self.manager.progress_manager.file_progress.remove_file(media_item.download_task_id)
Expand Down Expand Up @@ -309,10 +311,11 @@ async def download(self, media_item: MediaItem) -> None:

partial_file = None
complete_file = None
FL_Filename = media_item.filename
if isinstance(media_item.file_lock_reference_name, Field):
media_item.file_lock_reference_name = media_item.filename

try:
await self._file_lock.check_lock(FL_Filename)
await self._file_lock.check_lock(media_item.file_lock_reference_name)

if not isinstance(media_item.current_attempt, int):
media_item.current_attempt = 1
Expand All @@ -331,7 +334,7 @@ async def download(self, media_item: MediaItem) -> None:
await log(f"Skipping {media_item.url} as it has already been downloaded", 10)
await self.manager.progress_manager.download_progress.add_previously_completed(False)
await self.mark_completed(media_item)
await self._file_lock.release_lock(FL_Filename)
await self._file_lock.release_lock(media_item.file_lock_reference_name)
return

resume_point = partial_file.stat().st_size if partial_file.exists() else 0
Expand All @@ -354,11 +357,11 @@ async def download(self, media_item: MediaItem) -> None:
await self.mark_completed(media_item)
await self.manager.progress_manager.file_progress.mark_task_completed(media_item.download_task_id)
await self.manager.progress_manager.download_progress.add_completed()
await self._file_lock.release_lock(FL_Filename)
await self._file_lock.release_lock(media_item.file_lock_reference_name)
return

except (aiohttp.ServerDisconnectedError, asyncio.TimeoutError, aiohttp.ServerTimeoutError) as e:
await self._file_lock.release_lock(FL_Filename)
await self._file_lock.release_lock(media_item.file_lock_reference_name)

if partial_file and partial_file.is_file():
size = partial_file.stat().st_size
Expand All @@ -372,7 +375,7 @@ async def download(self, media_item: MediaItem) -> None:

except (aiohttp.ClientPayloadError, aiohttp.ClientOSError, aiohttp.ClientResponseError, ConnectionResetError,
DownloadFailure, FileNotFoundError, PermissionError) as e:
await self._file_lock.release_lock(FL_Filename)
await self._file_lock.release_lock(media_item.file_lock_reference_name)

if hasattr(e, "status"):
if ((await is_4xx_client_error(e.status) and e.status != HTTPStatus.TOO_MANY_REQUESTS)
Expand Down
1 change: 1 addition & 0 deletions cyberdrop_dl/utils/dataclasses/url_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def __init__(self, url: "URL", referer: "URL", download_folder: Path, filename:
self.ext: str = ext
self.download_filename: str = field(init=False)
self.original_filename: str = original_filename
self.file_lock_reference_name: str = field(init=False)
self.datetime: str = field(init=False)
self.filesize: int = field(init=False)
self.current_attempt: int = field(init=False)
Expand Down

0 comments on commit 2b7080a

Please sign in to comment.