Skip to content

Commit

Permalink
vm(apple): fix request stop
Browse files Browse the repository at this point in the history
Fixes #5425
  • Loading branch information
osy committed Jul 17, 2023
1 parent ba9033d commit c20a909
Showing 1 changed file with 36 additions and 29 deletions.
65 changes: 36 additions & 29 deletions Services/UTMAppleVirtualMachine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -208,42 +208,43 @@ 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
}
}
} 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 {
apple.stop { error in
if let error = error {
continuation.resume(throwing: error)
} else {
self.guestDidStop(apple)
continuation.resume()
}
}
}
}
}

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)
}
}
}
}

func stop(usingMethod method: UTMVirtualMachineStopMethod = .request) async throws {
if let installProgress = installProgress {
installProgress.cancel()
Expand All @@ -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
Expand Down

0 comments on commit c20a909

Please sign in to comment.