Skip to content

fix(core): 持有开机自启动设置任务引用并记录异常 - #416

Merged
1w1w11w1 merged 1 commit into
devfrom
perf/startup-lazy-imports
Aug 27, 2026
Merged

fix(core): 持有开机自启动设置任务引用并记录异常#416
1w1w11w1 merged 1 commit into
devfrom
perf/startup-lazy-imports

Conversation

@1w1w11w1

@1w1w11w1 1w1w11w1 commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

问题

#407System.set_SelfStart 改为 fire-and-forget 的 asyncio.create_task(...),但未持有返回的 task 引用:

  • task 可能在执行前被 GC;
  • 若注册失败(如注册表写入被拒),异常被静默吞掉,无任何日志。

改动

沿用本类既有的 temp_task + add_done_callback 模式(与 _stage_refresh_task 一致):

  • temp_task 持有 task 引用,避免被 GC;
  • done 回调中移除引用,失败时记录 logger.warning
  • 仍以 fire-and-forget 方式执行,不阻塞配置初始化。

验证

  • 全量测试:7 failed, 200 passed,那 7 项在 dev 上已失败,与本 PR 无关。
  • 本地端到端启动正常。
  • ruff format --check:触碰文件均 already formatted。

Sourcery 摘要

修复自启动设置后台任务可能被垃圾回收且异常被静默忽略的问题。

@sourcery-ai

sourcery-ai Bot commented Aug 25, 2026

Copy link
Copy Markdown

审查者指南

本 PR 通过切断启动前的同步 import 链,将 8 个任务 Manager、OCR/GUI 相关重依赖及 ArknightWin32Toolkit 延迟到首次使用或后台任务启动时加载,并补强自启动异步任务的引用持有和异常处理;实测 serve 前耗时中位数从 2285 ms 降至 1920 ms,重依赖不再提前进入 sys.modules

Manager 惰性加载时序图

sequenceDiagram
    participant TM as TaskManager
    participant Task as app.task
    participant Importer as import_module
    participant Manager as Manager submodule
    TM->>Task: access task.MaaManager
    Task->>Importer: import_module(.MAA, app.task)
    Importer->>Manager: load MaaManager and dependencies
    Manager-->>Task: MaaManager
    Task->>Task: cache MaaManager in globals
    Task-->>TM: MaaManager
Loading

已跟踪的自启动初始化时序图

sequenceDiagram
    participant Config as Config.init_config
    participant EventLoop as asyncio event loop
    participant System as System.set_SelfStart
    participant Callback as _self_start_done
    Config->>EventLoop: create_task(System.set_SelfStart(...))
    Config->>Config: append task to temp_task
    Config->>EventLoop: add_done_callback(_self_start_done)
    EventLoop->>System: run set_SelfStart
    System-->>EventLoop: complete or raise exception
    EventLoop->>Callback: _self_start_done(task)
    Callback->>Config: remove task from temp_task
    Callback-->>Config: logger.warning on exception
Loading

文件级变更

变更 详细信息 文件
将任务 Manager 的导出改为按属性访问时惰性加载,避免在启动阶段引入重量级依赖。
  • 通过 PEP 562 __getattr__ 延迟导入并缓存 8 个 Manager,同时保留 TYPE_CHECKING 类型可见性。
  • 将 TaskManager 的模块级 Manager 导入替换为 import app.task as task,在实际实例化点按需访问属性。
app/task/__init__.py
app/core/task_manager.py
移除计时器模块导入阶段对 ArknightWin32Toolkit 的加载,将其推迟到定时任务实际启动时。
  • 将 Toolkit 导入移入 second_task 循环之前,使其不再位于启动 import 路径中,并在运行期间只加载一次。
app/core/timer.py
修复后台自启动设置任务的生命周期管理和异常处理。
  • create_task 的返回值保存到 temp_task,通过完成回调移除任务并记录未处理的异常。
  • 继续以 fire-and-forget 方式执行,避免阻塞配置初始化。
app/core/config.py
更新版本元数据。
  • 调整版本 JSON 中的版本信息。
res/version.json

提示与命令

与 Sourcery 交互

  • 触发新的审查: 在拉取请求中评论 @sourcery-ai review
  • 继续讨论: 直接回复 Sourcery 的审查评论。
  • 根据审查评论生成 GitHub issue: 回复审查评论,请 Sourcery 根据该评论创建 issue。也可以回复审查评论并使用 @sourcery-ai issue,根据该评论创建 issue。
  • 生成拉取请求标题: 在拉取请求标题的任意位置写入 @sourcery-ai,即可随时生成标题。也可以在拉取请求中评论 @sourcery-ai title,随时生成或重新生成标题。
  • 生成拉取请求摘要: 在拉取请求正文中任意位置写入 @sourcery-ai summary,即可在指定位置生成 PR 摘要。也可以在拉取请求中评论 @sourcery-ai summary,随时生成或重新生成摘要。
  • 生成审查者指南: 在拉取请求中评论 @sourcery-ai guide,即可随时生成或重新生成审查者指南。
  • 解决所有 Sourcery 评论: 在拉取请求中评论 @sourcery-ai resolve,即可解决所有 Sourcery 评论。如果你已经处理了所有评论且不想再看到它们,这会很有用。
  • 忽略所有 Sourcery 审查: 在拉取请求中评论 @sourcery-ai dismiss,即可忽略所有现有的 Sourcery 审查。如果你想使用新的审查重新开始,这尤其有用——别忘了评论 @sourcery-ai review 来触发新的审查!

自定义使用体验

访问你的控制面板以:

  • 启用或禁用审查功能,例如 Sourcery 生成的拉取请求摘要、审查者指南等。
  • 更改审查语言。
  • 添加、删除或编辑自定义审查指令。
  • 调整其他审查设置。

获取帮助

Original review guide in English

Reviewer's Guide

本 PR 通过切断启动前的同步 import 链,将 8 个任务 Manager、OCR/GUI 相关重依赖及 ArknightWin32Toolkit 延迟到首次使用或后台任务启动时加载,并补强自启动异步任务的引用持有和异常处理;实测 serve 前耗时中位数从 2285 ms 降至 1920 ms,重依赖不再提前进入 sys.modules

Sequence diagram for lazy Manager loading

sequenceDiagram
    participant TM as TaskManager
    participant Task as app.task
    participant Importer as import_module
    participant Manager as Manager submodule
    TM->>Task: access task.MaaManager
    Task->>Importer: import_module(.MAA, app.task)
    Importer->>Manager: load MaaManager and dependencies
    Manager-->>Task: MaaManager
    Task->>Task: cache MaaManager in globals
    Task-->>TM: MaaManager
Loading

Sequence diagram for tracked self-start initialization

sequenceDiagram
    participant Config as Config.init_config
    participant EventLoop as asyncio event loop
    participant System as System.set_SelfStart
    participant Callback as _self_start_done
    Config->>EventLoop: create_task(System.set_SelfStart(...))
    Config->>Config: append task to temp_task
    Config->>EventLoop: add_done_callback(_self_start_done)
    EventLoop->>System: run set_SelfStart
    System-->>EventLoop: complete or raise exception
    EventLoop->>Callback: _self_start_done(task)
    Callback->>Config: remove task from temp_task
    Callback-->>Config: logger.warning on exception
Loading

File-Level Changes

Change Details Files
将任务 Manager 导出改为按属性访问惰性加载,避免启动阶段引入重型依赖。
  • 通过 PEP 562 __getattr__ 延迟导入并缓存 8 个 Manager,同时保留 TYPE_CHECKING 类型可见性。
  • 将 TaskManager 的模块级 Manager 导入替换为 import app.task as task,在实际实例化点按需访问属性。
app/task/__init__.py
app/core/task_manager.py
移除计时器模块导入阶段对 ArknightWin32Toolkit 的加载,将其推迟到定时任务实际启动。
  • 把 Toolkit 导入移入 second_task 的循环前,使其在启动 import 路径之外且运行期间只加载一次。
app/core/timer.py
修复后台自启动设置任务的生命周期与异常处理。
  • 保存 create_task 返回值到 temp_task,通过完成回调移除任务并记录未处理异常。
  • 继续以 fire-and-forget 方式执行,避免阻塞配置初始化。
app/core/config.py
更新版本元数据。
  • 调整版本 JSON 中的版本信息。
res/version.json

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

sourcery-ai[bot]
sourcery-ai Bot previously approved these changes Aug 25, 2026

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

你好——我已经审阅了你的更改,看起来很棒!

Sourcery 评估

已批准。


Sourcery 对开源项目免费——如果你喜欢我们的评审,请考虑分享它们 ✨
帮助我变得更有用!请在每条评论上点击 👍 或 👎,我会利用这些反馈来改进评审。
Original comment in English

Hey - I've reviewed your changes and they look great!

Sourcery assessment

Approved.


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@sourcery-ai
sourcery-ai Bot dismissed their stale review August 25, 2026 19:10

Sourcery withdrew this approval because the latest commits introduced blocking findings.

@1w1w11w1 1w1w11w1 changed the title perf(startup): 修正 PR #407 中被同步 import 链抵消的两处延迟加载 perf(startup): 移出 serve 前的三处阻塞导入与 MaaFW bundle 加载 Aug 25, 2026
#407 把 System.set_SelfStart 改为 fire-and-forget 的 create_task 但未持有引用:
task 可能被 GC, 且异常被静默吞掉。沿用本类既有的 temp_task +
add_done_callback 模式, 失败时记录警告日志。
@1w1w11w1
1w1w11w1 force-pushed the perf/startup-lazy-imports branch from fadc6d1 to 30705ad Compare August 27, 2026 08:31
@1w1w11w1 1w1w11w1 changed the title perf(startup): 移出 serve 前的三处阻塞导入与 MaaFW bundle 加载 fix(core): 持有开机自启动设置任务引用并记录异常 Aug 27, 2026

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Sourcery assessment

Approved.

@1w1w11w1
1w1w11w1 merged commit 5d65bcf into dev Aug 27, 2026
3 checks passed
@1w1w11w1
1w1w11w1 deleted the perf/startup-lazy-imports branch August 27, 2026 08:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant