#1495·loguru

在 Windows 上删除保留策略的日志文件时发生的 PermissionError 在退出时未被处理

作者: not-sekiun创建于 2026年7月30日更新于 2026年8月30日
标签enhancement

import time from loguru import logger from loguru import logger

Instance 1 starts up and keeps running, holding its log file open.

instance_1 = logger.add("app_{time}.log", retention=1) logger.info("Instance 1 is running") time.sleep(1) # Give the two instances distinct timestamps.

Instance 2 starts up later, then exits almost immediately.

instance_2 = logger.add("app_{time}.log", retention=1)

Instance 2 exits. Retention keeps the newest file (instance 2's own, now closed) and deletes the older one, but instance 1 still has that one open.

logger.remove(instance_2) # PermissionError: [WinError 32] This is the output on my machine: $ uv run Python -c "import loguru; print(loguru.version)" 0.7.3 $ cat .\test.py import time from loguru import logger from loguru import logger

Instance 1 starts up and keeps running, holding its log file open.

instance_1 = logger.add("app_{time}.log", retention=1) logger.info("Instance 1 is running") time.sleep(1) # Give the two instances distinct timestamps.

Instance 2 starts up later, then exits almost immediately.

instance_2 = logger.add("app_{time}.log", retention=1)

Instance 2 exits. Retention keeps the newest file (instance 2's own, now closed) and deletes the older one, but instance 1 still has that one open.

logger.remove(instance_2) # PermissionError: [WinError 32] $ uv run Python .\test.py 2026-07-31 00:09:57.573 | INFO | main::10 - Instance 1 is running Traceback (most recent call last): File "C:\Users\sekiun\loguru_test\test.py", line 19, in logger.remove(instance_2) # PermissionError: [WinError 32]

File "C:\Users\sekiun\loguru_test\.venv\Lib\site-packages\loguru\_logger.py", line 1066, in remove
handler.stop()
~~~~~~~~~~~~~^^
File "C:\Users\sekiun\loguru_test\.venv\Lib\site-packages\loguru\_handler.py", line 223, in stop
self._sink.stop()
~~~~~~~~~~~~~~~~^^
File "C:\Users\sekiun\loguru_test\.venv\Lib\site-packages\loguru\_file_sink.py", line 212, in stop