fix(bay): 清理终止运行时引用的孤立 Cargo - #26
Conversation
There was a problem hiding this comment.
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.
| if any( | ||
| instance.state.lower() not in _TERMINAL_RUNTIME_STATES | ||
| for instance in instances | ||
| ): |
There was a problem hiding this comment.
在进行 instance.state.lower() 调用时,如果 instance.state 为 None(例如驱动实现中未正确返回状态,或者处于某种未知异常状态),将会抛出 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 ):
修改内容
dead、exited、failed和succeeded状态的旧实例后,再次查询运行时引用。问题原因
Docker 运行时查询包含已经退出的容器。旧实现只要看到任意引用就会永久跳过 Cargo 清理,导致已删除沙盒的目录和退出容器持续残留。
验证
24 passed525 passedSummary 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:
Enhancements:
Tests: