Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

fix(lume): make failed create cleanup consistent across sync and async paths (#1167)

Aditya Singh51b6c5d709b6
diff --git a/libs/lume/src/LumeController.swift b/libs/lume/src/LumeController.swift
index 8892383477..4c04c3d5a7 100644
--- a/libs/lume/src/LumeController.swift
+++ b/libs/lume/src/LumeController.swift
@@ -478,8 +478,9 @@ final class LumeController {
Logger.info("Provisioning marker cleared", metadata: ["name": name])
} catch {
- // Clear provisioning marker on failure (vmDir may have been deleted by createInternal)
- vmDir.clearProvisioningMarker()
+ // Clean up the pre-created directory on failure. If deletion fails, keep the
+ // provisioning marker intact so the leftover VM remains discoverable.
+ cleanupFailedCreateVMDirectory(vmDir, context: "creation")
Logger.error("Failed to create VM", metadata: ["error": error.localizedDescription])
throw error
}
@@ -585,20 +586,26 @@ final class LumeController {
Logger.info("Async VM creation completed successfully", metadata: ["name": name])
} catch {
- // Clear marker and cleanup on failure
- vmDir.clearProvisioningMarker()
- do {
- try vmDir.delete()
- } catch let cleanupError {
- Logger.error("Failed to clean up VM directory after async creation failure",
- metadata: ["error": cleanupError.localizedDescription])
- }
+ // Clean up the pre-created directory on failure. If deletion fails, keep the
+ // provisioning marker intact so the leftover VM remains discoverable.
+ controller.cleanupFailedCreateVMDirectory(vmDir, context: "async creation")
Logger.error("Async VM creation failed",
metadata: ["name": name, "error": error.localizedDescription])
}
}
}
+ private func cleanupFailedCreateVMDirectory(_ vmDir: VMDirectory, context: String) {
+ do {
+ if vmDir.exists() {
+ try vmDir.delete()
+ }
+ } catch let cleanupError {
+ Logger.error("Failed to clean up VM directory after \(context) failure",
+ metadata: ["error": cleanupError.localizedDescription])
+ }
+ }
+
/// Internal create method that skips validation (used by createAsync)
@MainActor
private func createInternal(
diff --git a/libs/lume/tests/LumeControllerTests.swift b/libs/lume/tests/LumeControllerTests.swift
new file mode 100644
index 0000000000..a21548dace
--- /dev/null
+++ b/libs/lume/tests/LumeControllerTests.swift
@@ -0,0 +1,94 @@
+import Foundation
+import Testing
+
+@testable import lume
+
+private enum TestCreateError: Error {
+ case setupFailed
+}
+
+@MainActor
+private final class FailingVM: MockVM {
+ override func setup(
+ ipswPath: String,
+ cpuCount: Int,
+ memorySize: UInt64,
+ diskSize: UInt64,
+ display: String
+ ) async throws {
+ throw TestCreateError.setupFailed
+ }
+}
+
+@MainActor
+private final class FailingVMFactory: VMFactory {
+ func createVM(
+ vmDirContext: VMDirContext,
+ imageLoader: ImageLoader?
+ ) throws -> VM {
+ FailingVM(
+ vmDirContext: vmDirContext,
+ virtualizationServiceFactory: { _ in MockVMVirtualizationService() },
+ vncServiceFactory: { MockVNCService(vmDirectory: $0) }
+ )
+ }
+}
+
+@MainActor
+@Test("create cleans up pre-created VM directory when setup fails")
+func testCreateCleansUpPrecreatedDirectoryOnFailure() async throws {
+ let tempConfigDir = try createTempDirectory()
+ let tempHomeDir = try createTempDirectory()
+ let tempStorageDir = try createTempDirectory()
+
+ defer {
+ try? FileManager.default.removeItem(at: tempConfigDir)
+ try? FileManager.default.removeItem(at: tempHomeDir)
+ try? FileManager.default.removeItem(at: tempStorageDir)
+ }
+
+ let previousXDGConfigHome = ProcessInfo.processInfo.environment["XDG_CONFIG_HOME"]
+ setenv("XDG_CONFIG_HOME", tempConfigDir.path, 1)
+ defer {
+ if let previousXDGConfigHome {
+ setenv("XDG_CONFIG_HOME", previousXDGConfigHome, 1)
+ } else {
+ unsetenv("XDG_CONFIG_HOME")
+ }
+ }
+
+ let settingsManager = SettingsManager(fileManager: .default)
+ try settingsManager.setHomeDirectory(path: tempHomeDir.path)
+
+ let controller = LumeController(
+ home: Home(settingsManager: settingsManager, fileManager: .default),
+ vmFactory: FailingVMFactory()
+ )
+ let orphanVMDir = VMDirectory(Path(tempStorageDir.path).directory("orphan-vm"))
+
+ do {
+ try await controller.create(
+ name: "orphan-vm",
+ os: "linux",
+ diskSize: 64 * 1024 * 1024,
+ cpuCount: 1,
+ memorySize: 1024 * 1024 * 1024,
+ display: "1024x768",
+ ipsw: nil,
+ storage: tempStorageDir.path
+ )
+ #expect(Bool(false), "Expected create to fail with the injected VM factory")
+ } catch TestCreateError.setupFailed {
+ // Expected failure from the injected VM factory.
+ } catch {
+ #expect(Bool(false), "Expected setupFailed error but got \(error)")
+ }
+
+ #expect(!orphanVMDir.exists())
+}
+
+private func createTempDirectory() throws -> URL {
+ let tempDir = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
+ try FileManager.default.createDirectory(at: tempDir, withIntermediateDirectories: true)
+ return tempDir
+}