LinuxContainer.create() leaks the VZ VM/XPC process when vm.start() throws — no do/catch around it
维护者通常 2 天内回复
评估
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 新手友好度
- 78/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 冷清
- 技术栈
- macos, swift
调研方向
从 Sources/Containerization/LinuxContainer.swift 中的 LinuxContainer.create() 开始,将 vm.start() 路径与现有的启动后 do/catch 清理进行比较。验证会抛出错误的 vm.start() 的失败路径,包括 relay 关闭、VM 停止以及转换到错误状态。完成的标准是:抛出的错误能够传播出去,同时不会遗留 VZ helper process 或 vmnet lease。
由索引模型根据 Issue 内容生成。
描述
Summary
LinuxContainer.create() (Sources/Containerization/LinuxContainer.swift, pinned 0.35.0) calls the raw vm.start() outside the do/catch block that otherwise cleans up on failure. If vm.start() itself throws — e.g. a vmnet allocation failure surfaced through the network device attachment — the underlying VZVirtualMachine (and its com.apple.Virtualization.VirtualMachine.xpc helper process) is never stopped. Since vm is a local variable inside create(), nothing outside the function ever holds a reference to it, so there is no way for a caller to stop or clean it up after the fact either.
public func create() async throws {
try await self.state.withLock { state in
...
let vm = try await self.vmm.create(config: creationConfig)
let relayManager = UnixSocketRelayManager(vm: vm, log: self.logger)
try await vm.start() // <-- NOT inside the do/catch below; a throw here leaks `vm`
do {
try await vm.withAgent { agent in ... }
state = .created(.init(vm: vm, relayManager: relayManager, ...))
} catch {
try? await relayManager.stopAll()
try? await vm.stop()
state.setErrored(error: error)
throw error
}
}
}
Because state is never transitioned away from .initialized when vm.start() throws here, a caller's subsequent container.stop() also can't help — stop() requires .created/.started state (via state.createdState("stop") / state.startedState("stop")), both of which throw against .initialized, so stop() is a no-op in this exact failure mode.
Impact
In our case (Anglesite, a macOS app embedding this package), a transient vmnet NAT-subnet contention (a known single-consumer limitation — see apple/container's vmnet plugin) causes exactly this: vm.start() throws vmnet_return_t(rawValue: 1002), and the resulting leaked VZ process permanently holds the vmnet lease. Every subsequent boot attempt — including retries and full app relaunches — fails identically, because the orphaned com.apple.Virtualization.VirtualMachine.xpc process survives even a full quit/relaunch of the embedding app (it isn't reaped as a child of our process). The only recovery is manually finding and killing that PID.
Suggested fix
Wrap vm.start() itself in a do/catch that calls try? await vm.stop() (and relayManager.stopAll()) on failure, mirroring the existing catch that already covers the post-start setup steps — e.g.:
let vm = try await self.vmm.create(config: creationConfig)
let relayManager = UnixSocketRelayManager(vm: vm, log: self.logger)
do {
try await vm.start()
try await vm.withAgent { agent in ... }
state = .created(.init(vm: vm, relayManager: relayManager, ...))
} catch {
try? await relayManager.stopAll()
try? await vm.stop()
state.setErrored(error: error)
throw error
}
Environment
- Package version:
0.35.0(pinned via.upToNextMinor(from: "0.35.0")) - macOS: Apple Silicon, Virtualization.framework via
VZVirtualMachineManager
Happy to send a PR with the fix above if useful — wanted to check the intended failure-handling shape first, since I'm not certain whether vm.stop() is safe to call on a VM that never finished start().
- 主要语言
- Swift
- 星标
- 9k
- 派生
- 412
- 平均合并
- 3 天 10 小时
- 30 天内合并 PR
- 12
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
apple/containerization 的其他 Issue
-
[Bug]: RegistryClient ignores credentials in HTTPS_PROXY, so image pulls fail with proxyAuthenticationRequired可能已有人在做 @vishalhabib99 于 4 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 68/100
apple/containerization#1012 ·
维护者通常 2 天内回复
-
[Bug]: archive ownership changes clear set-ID permission bits可能已有人在做 @maksim-romanov 于 100 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
apple/containerization#819 · 1 条评论 · 1 个 reaction ·
维护者通常 2 天内回复
-
[Bug]: archive extraction strips special permission bits可能已有人在做 @maksim-romanov 于 100 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 78/100
apple/containerization#818 · 2 条评论 ·
维护者通常 2 天内回复
-
[Bug]: TimeSyncer steps the guest clock backward every 30 s, and the first sync steps it by up to ±0.5 s可能已有人在做 @castlemilk 于 1 天前认领。 未关闭
难度 4/5 3-5 天 新手友好度 46/100
apple/containerization#1019 · 1 条评论 · 已指派 1 人 ·
维护者通常 2 天内回复
-
难度 3/5 1-2 天 新手友好度 65/100
apple/containerization#1017 ·
维护者通常 2 天内回复
查看 apple/containerization 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 76/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 1 天内回复
-
fxios-coreexperience
难度 2/5 1-3 小时 新手友好度 68/100
mozilla-mobile/firefox-ios#36661 ·
维护者通常 1 天内回复
-
needs-triage
难度 2/5 1-3 小时 新手友好度 68/100
manaflow-ai/cmux#45713 · 1 条评论 ·
维护者通常 1 天内回复
-
enhancement
难度 2/5 1-3 小时 新手友好度 67/100
alejandrobujan/tendedero#151 ·
维护者通常 2 天内回复