Skip to content

fix(bay): 清理终止运行时引用的孤立 Cargo - #26

Draft
RhoninSeiei wants to merge 2 commits into
AstrBotDevs:mainfrom
RhoninSeiei:fix/orphan-cargo-runtime-state
Draft

fix(bay): 清理终止运行时引用的孤立 Cargo#26
RhoninSeiei wants to merge 2 commits into
AstrBotDevs:mainfrom
RhoninSeiei:fix/orphan-cargo-runtime-state

Conversation

@RhoninSeiei

@RhoninSeiei RhoninSeiei commented Jul 15, 2026

Copy link
Copy Markdown

修改内容

  • 孤立 Cargo 清理时区分运行中实例与已经终止的实例。
  • 清除 deadexitedfailedsucceeded 状态的旧实例后,再次查询运行时引用。
  • 仍存在活动或状态未知的实例时保留 Cargo。
  • 删除前再次查询,处理扫描与删除之间新增运行时的并发情况。

问题原因

Docker 运行时查询包含已经退出的容器。旧实现只要看到任意引用就会永久跳过 Cargo 清理,导致已删除沙盒的目录和退出容器持续残留。

验证

  • 专项单元测试:24 passed
  • 生产组合分支单元测试:525 passed
  • Ruff:通过
  • 实际启动清理:移除 4 个退出运行时并清理 2 个孤立 Cargo,错误数为 0

Summary by Sourcery

Refine orphan Cargo garbage collection to ignore terminal runtime instances while still protecting cargos referenced by active or newly created runtimes.

Bug Fixes:

  • Ensure orphan cargos referenced only by exited or otherwise terminal runtimes are cleaned up instead of being preserved indefinitely.

Enhancements:

  • Clean up terminal runtime instances before deciding on orphan cargo deletion and re-check for active references to handle races between scanning and deletion.

Tests:

  • Add unit tests covering cleanup when only exited runtimes reference a cargo and when a new active runtime appears after terminal runtime cleanup.

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request updates the orphan cargo garbage collection task to automatically clean up terminal runtime instances (such as exited or dead containers) that reference a cargo, rather than allowing them to block garbage collection indefinitely. It also adds corresponding unit tests. The review feedback points out a potential robustness issue where instance.state could be None, which would cause an AttributeError when calling .lower(), and suggests adding a defensive check.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +104 to +107
if any(
instance.state.lower() not in _TERMINAL_RUNTIME_STATES
for instance in instances
):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

在进行 instance.state.lower() 调用时,如果 instance.stateNone(例如驱动实现中未正确返回状态,或者处于某种未知异常状态),将会抛出 AttributeError 异常,导致整个 GC 任务中断。\n\n为了提高代码的健壮性(防御性编程),建议在调用 .lower() 之前先对 instance.state 进行空值检查。如果状态为空,可以保守地将其视为非终端状态(即返回 True 跳过 Cargo 清理),以避免潜在的运行时崩溃。

        if any(\n            not instance.state or instance.state.lower() not in _TERMINAL_RUNTIME_STATES\n            for instance in instances\n        ):

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