vm(apple): fix request stop

Fixes #5425
This commit is contained in:
osy 2023-07-16 22:22:19 -07:00
parent ba9033dbef
commit c20a909411
1 changed files with 36 additions and 29 deletions

View File

@ -208,37 +208,38 @@ final class UTMAppleVirtualMachine: UTMVirtualMachine {
}
}
private func _stop(usingMethod method: UTMVirtualMachineStopMethod) async throws {
if method != .request, #available(macOS 12, *) {
try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, Error>) in
vmQueue.async {
guard let apple = self.apple else {
continuation.resume() // already stopped
return
}
apple.stop { error in
if let error = error {
continuation.resume(throwing: error)
} else {
self.guestDidStop(apple)
continuation.resume()
}
@available(macOS 12, *)
private func _forceStop() async throws {
try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, Error>) in
vmQueue.async {
guard let apple = self.apple else {
continuation.resume() // already stopped
return
}
apple.stop { error in
if let error = error {
continuation.resume(throwing: error)
} else {
self.guestDidStop(apple)
continuation.resume()
}
}
}
} else {
try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, Error>) in
vmQueue.async {
do {
guard let apple = self.apple else {
continuation.resume() // already stopped
return
}
try apple.requestStop()
continuation.resume()
} catch {
continuation.resume(throwing: error)
}
}
}
private func _requestStop() async throws {
try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, Error>) in
vmQueue.async {
guard let apple = self.apple else {
continuation.resume() // already stopped
return
}
do {
try apple.requestStop()
continuation.resume()
} catch {
continuation.resume(throwing: error)
}
}
}
@ -252,9 +253,15 @@ final class UTMAppleVirtualMachine: UTMVirtualMachine {
guard state == .started || state == .paused else {
return
}
guard method != .request else {
return try await _requestStop()
}
guard #available(macOS 12, *) else {
throw UTMAppleVirtualMachineError.operationNotAvailable
}
state = .stopping
do {
try await _stop(usingMethod: method)
try await _forceStop()
state = .stopped
} catch {
state = .stopped