Skip to content

[Bug] Loguru file logging leaves resource_tracker semaphore warnings on shutdown #8595

@Akokk0

Description

@Akokk0

What happened / 发生了什么

在 macOS 本地运行 AstrBot 时,如果开启文件日志,进程退出时会稳定出现 Python multiprocessing.resource_tracker 的 semaphore 泄漏提示。

开启一个文件日志 sink 时:

resource_tracker: There appear to be 8 leaked semaphore objects to clean up at shutdown

如果同时开启普通日志和 trace 日志,则数量会变成 16:

resource_tracker: There appear to be 16 leaked semaphore objects to clean up at shutdown

该问题不影响 AstrBot 主功能运行,但会污染退出时的终端输出,也容易让用户误以为插件或平台适配器存在资源泄漏。

Reproduce / 如何复现?

  1. 在 macOS 本地启动 AstrBot
  2. 开启文件日志:
{
  "log_file_enable": true,
  "log_file_path": "logs/astrbot.log"
}
  1. 启动 AstrBot
  2. 退出进程
  3. 观察终端输出

如果同时开启 trace 日志:

{
  "log_file_enable": true,
  "trace_log_enable": true,
  "log_file_path": "logs/astrbot.log",
  "trace_log_path": "logs/astrbot.trace.log"
}

退出时 warning 数量会从 8 变成 16。

AstrBot version, deployment method (e.g., Windows Docker Desktop deployment), provider used, and messaging platform used. / AstrBot 版本、部署方式(如 Windows Docker Desktop 部署)、使用的提供商、使用的消息平台适配器

  • AstrBot version: v4.25.2
  • Deployment: macOS local source / CLI run
  • Provider: not related
  • Messaging platform: not related

OS

macOS

Logs / 报错日志

开启一个文件日志 sink 时:

resource_tracker: There appear to be 8 leaked semaphore objects to clean up at shutdown

同时开启普通日志和 trace 日志时:

resource_tracker: There appear to be 16 leaked semaphore objects to clean up at shutdown

预期行为:

AstrBot 正常关闭时应 flush 并移除 Loguru file / trace sinks,不应在退出阶段输出 resource_tracker semaphore warning。

初步判断:

astrbot/core/log.py 中的文件日志 sink 使用了 Loguru 的 enqueue=True

_loguru.add(..., enqueue=True, ...)

Loguru 在 enqueue=True 时会创建 multiprocessing 队列相关资源。当前这些 sink 在日志重配置时会被移除,但在 AstrBot 进程关闭路径中没有统一清理。

Are you willing to submit a PR? / 你愿意提交 PR 吗?

  • Yes!

Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:coreThe bug / feature is about astrbot's core, backend

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions