LinuxContainer.create() leaks the VZ VM/XPC process when vm.start() throws — no do/catch around it
Maintainer antworten meist innerhalb von 2 Tagen
Bewertung
- Schwierigkeit
- 2/5
- Geschätzter Aufwand
- 1-3 Stunden
- Anfängerfreundlichkeit
- 78/100
- Issue-Typ
- Bug
- Klarheit
- Klar beschrieben
- Aktivitätsstatus
- Ruhig
- Tech-Stack
- macos, swift
- Bereich
- operating-systems
Rechercherichtung
Beginnen Sie in Sources/Containerization/LinuxContainer.swift bei LinuxContainer.create() und vergleichen Sie den vm.start()-Pfad mit der vorhandenen do/catch-Bereinigung nach dem Start. Überprüfen Sie den Fehlerpfad für ein vm.start(), das einen Fehler auslöst, einschließlich des Herunterfahrens des Relays, des Anhaltens der VM und des Übergangs in einen Fehlerzustand. Die Aufgabe ist abgeschlossen, wenn der ausgelöste Fehler weitergegeben wird, ohne den VZ-Hilfsprozess oder die vmnet-Lease zurückzulassen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
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().
- Vorherrschende Sprache
- Swift
- Sterne
- 9k
- Forks
- 412
- Ø Merge
- 3 T. 10 Std.
- Gemergte PRs (30 T.)
- 12
Entwicklungsumgebung
- Kein Dockerfile und keine Docker-Compose-Datei
- Keine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus apple/containerization
-
[Bug]: RegistryClient ignores credentials in HTTPS_PROXY, so image pulls fail with proxyAuthenticationRequiredEvtl. vergeben @vishalhabib99 hat das vor 4 Tagen übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
apple/containerization#1012 ·
Maintainer antworten meist innerhalb von 2 Tagen
-
[Bug]: archive ownership changes clear set-ID permission bitsEvtl. vergeben @maksim-romanov hat das vor 101 Tagen übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
apple/containerization#819 · 1 Kommentar · 1 Reaktion ·
Maintainer antworten meist innerhalb von 2 Tagen
-
[Bug]: archive extraction strips special permission bitsEvtl. vergeben @maksim-romanov hat das vor 101 Tagen übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
apple/containerization#818 · 2 Kommentare ·
Maintainer antworten meist innerhalb von 2 Tagen
-
[Bug]: TimeSyncer steps the guest clock backward every 30 s, and the first sync steps it by up to ±0.5 sEvtl. vergeben @castlemilk hat das vor 1 Tag übernommen. Offen
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 46/100
apple/containerization#1019 · 1 Kommentar · 1 zugewiesene Person ·
Maintainer antworten meist innerhalb von 2 Tagen
-
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 65/100
apple/containerization#1017 ·
Maintainer antworten meist innerhalb von 2 Tagen
Alle Issues in apple/containerization
Ähnliche Issues
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
Maintainer antworten meist innerhalb von 1 Tag
-
compiler diagnostics quality ownership SIL ownership
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 64/100
Maintainer antworten meist innerhalb von 1 Tag
-
T-Defect
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 65/100
element-hq/element-x-ios#6536 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
Maintainer antworten meist innerhalb von 1 Tag
-
Switch to new string APIOffenfxios-coreexperience
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
mozilla-mobile/firefox-ios#36661 ·
Maintainer antworten meist innerhalb von 1 Tag