From 7335a584e71de6be912af1e72929b8dee4b773db Mon Sep 17 00:00:00 2001 From: SomberNight Date: Wed, 14 Apr 2021 05:10:54 +0200 Subject: [PATCH] logging: handle "cannot delete old logfile" error E.g. on Windows, files open in one process cannot be deleted by another process. With file logging enabled, if an old logfile was open in a text editor, Electrum could crash during startup. ``` E | __main__ | Traceback (most recent call last): File "...\electrum\run_electrum", line 391, in main handle_cmd( File "...\electrum\run_electrum", line 403, in handle_cmd configure_logging(config) File "...\electrum\electrum\logging.py", line 278, in configure_logging _configure_file_logging(log_directory) File "...\electrum\electrum\logging.py", line 107, in _configure_file_logging _delete_old_logs(log_directory) File "...\electrum\electrum\logging.py", line 98, in _delete_old_logs os.remove(str(f)) PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: '...\\AppData\\Roaming\\Electrum\\testnet\\logs\\electrum_log_20210414T023751Z_25008.log' ``` --- electrum/logging.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/electrum/logging.py b/electrum/logging.py index f3e0a4dd8..7f2742ead 100644 --- a/electrum/logging.py +++ b/electrum/logging.py @@ -77,7 +77,10 @@ electrum_logger.setLevel(logging.DEBUG) def _delete_old_logs(path, keep=10): files = sorted(list(pathlib.Path(path).glob("electrum_log_*.log")), reverse=True) for f in files[keep:]: - os.remove(str(f)) + try: + os.remove(str(f)) + except OSError as e: + _logger.warning(f"cannot delete old logfile: {e}") _logfile_path = None