Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions astrbot/core/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,15 @@ def _setup_loguru(cls) -> None:
return

_loguru.remove()
# NOTE(n100): enqueue=True keeps console logging OFF the event loop
# thread. Without it a blocking console (e.g. Windows QuickEdit with
# text selected) stalls the whole asyncio loop for many seconds, which
# kills the heartbeat of long-lived websocket clients.
cls._console_sink_id = _loguru.add(
sys.stdout,
level="DEBUG",
colorize=True,
enqueue=True,
filter=lambda record: not record["extra"].get("is_trace", False),
format=(
"<green>[{time:HH:mm:ss.SSS}]</green> {extra[plugin_tag]} "
Expand Down Expand Up @@ -509,6 +514,7 @@ def configure_logger(
sys.stdout,
level=configured_level,
colorize=True,
enqueue=True, # NOTE(n100): non-blocking, see above

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): When configure_logger() replaces the console sink while its background worker is blocked in sys.stdout.write(), _loguru.remove(cls._console_sink_id) waits for that queued sink thread to finish. Because configure_logger() runs on the event-loop thread, logger reconfiguration still stalls the event loop until the console becomes writable.

Triggers: When the log level is reconfigured while the console output is blocked.

Suggested fix: Perform sink replacement outside the event-loop thread, or update the sink without synchronously joining a blocked enqueue worker.

Suggested change
enqueue=True, # NOTE(n100): non-blocking, see above
enqueue=False, # Avoid joining a blocked worker during reconfiguration

filter=lambda record: (
not record["extra"].get("is_trace", False)
),
Expand Down