Skip to content

Bulk asynchronous downloader for multiple file hosts

fix: properly close log files (#1022)

NTFSvolume79d7aff95709
diff --git a/cyberdrop_dl/director.py b/cyberdrop_dl/director.py
index 3dbff452c..93def7016 100644
--- a/cyberdrop_dl/director.py
+++ b/cyberdrop_dl/director.py
@@ -3,6 +3,7 @@ from __future__ import annotations
import asyncio
import contextlib
import logging
+import os
import signal
import sys
from datetime import datetime
@@ -37,7 +38,15 @@ P = ParamSpec("P")
R = TypeVar("R")
startup_logger = logging.getLogger("cyberdrop_dl_startup")
-STARTUP_LOGGER_FILE = Path.cwd().joinpath("startup.log")
+
+
+def new_startup_log_file() -> Path:
+ file = Path.cwd().joinpath("startup.log")
+ file.unlink(missing_ok=True)
+ return file
+
+
+STARTUP_LOGGER_FILE = new_startup_log_file()
class ExitCode(IntEnum):
@@ -241,8 +250,10 @@ def _setup_main_logger(manager: Manager, config_name: str) -> None:
def _setup_startup_logger(*, first_time_setup: bool = False) -> None:
+ global STARTUP_LOGGER_FILE
if first_time_setup:
- STARTUP_LOGGER_FILE.unlink(missing_ok=True) # Only delete file once. Subsequent calls will append to file
+ # Get startup logger again in case the CWD changed after the module was imported (for pytest)
+ STARTUP_LOGGER_FILE = new_startup_log_file()
_destroy_startup_logger()
startup_logger.setLevel(10)
console_handler = LogHandler(level=10)
@@ -335,7 +346,9 @@ def _setup_manager() -> Manager:
class Director:
"""Creates a manager and runs it"""
- def __init__(self):
+ def __init__(self) -> None:
+ if os.name == "nt":
+ asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(self.loop)
self.manager = _setup_manager()
diff --git a/cyberdrop_dl/utils/logger.py b/cyberdrop_dl/utils/logger.py
index cc317c85e..de34d54dc 100644
--- a/cyberdrop_dl/utils/logger.py
+++ b/cyberdrop_dl/utils/logger.py
@@ -102,6 +102,7 @@ class QueuedLogger:
assert name not in manager.loggers, f"A logger with the name '{name}' already exists"
log_queue = queue.Queue()
self.handler = BareQueueHandler(log_queue)
+ self.log_handler = split_handler
self.listener = QueueListener(log_queue, split_handler, respect_handler_level=True)
self.listener.start()
manager.loggers[name] = self
@@ -110,6 +111,8 @@ class QueuedLogger:
"""This asks the thread to terminate, and waits until all pending messages are processed."""
self.listener.stop()
self.handler.close()
+ self.log_handler.console.file.close()
+ self.log_handler.close()
class NoPaddingLogRender(LogRender):