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): clean up stale session files on VM list (#846)

Francesco Bonacci835194bf095c
diff --git a/libs/lume/src/LumeController.swift b/libs/lume/src/LumeController.swift
index 9567d8a3a6..69c9062ef9 100644
--- a/libs/lume/src/LumeController.swift
+++ b/libs/lume/src/LumeController.swift
@@ -115,6 +115,18 @@ final class LumeController {
}
}
+ /// Parses the VNC port from a VNC URL
+ /// - Parameter url: VNC URL like "vnc://:[email protected]:62295"
+ /// - Returns: The port number if successfully parsed, nil otherwise
+ private func parseVNCPort(from url: String) -> UInt16? {
+ // URL format: vnc://:password@host:port
+ guard let urlComponents = URLComponents(string: url.replacingOccurrences(of: "vnc://", with: "http://")),
+ let port = urlComponents.port else {
+ return nil
+ }
+ return UInt16(port)
+ }
+
/// Get VM details using lightweight path (no VM object instantiation)
/// Checks provisioning marker first, then SharedVM cache for running status
@MainActor
@@ -144,10 +156,19 @@ final class LumeController {
// If not in cache, check if session file exists (cross-process fallback)
// Session files are created when VM starts and deleted when VM stops
+ // Validate that the VNC port is actually in use to detect stale sessions
if !isRunning {
if let session = try? vmDir.loadSession() {
- isRunning = true
- vncUrl = session.url
+ // Parse VNC port from URL like "vnc://:[email protected]:62295"
+ if let port = parseVNCPort(from: session.url),
+ NetworkUtils.isLocalPortInUse(port: port) {
+ isRunning = true
+ vncUrl = session.url
+ } else {
+ // Stale session file - VNC port not in use, clean it up
+ vmDir.clearSession()
+ Logger.info("Cleaned up stale session file", metadata: ["name": vmName])
+ }
}
}
diff --git a/libs/lume/src/Utils/NetworkUtils.swift b/libs/lume/src/Utils/NetworkUtils.swift
index e8f2dd5a79..c515acd1bd 100644
--- a/libs/lume/src/Utils/NetworkUtils.swift
+++ b/libs/lume/src/Utils/NetworkUtils.swift
@@ -55,4 +55,27 @@ enum NetworkUtils {
static func isSSHAvailable(ipAddress: String) -> Bool {
return isPortOpen(ipAddress: ipAddress, port: 22, timeout: 2)
}
-}
\ No newline at end of file
+
+ /// Checks if a local port has a process listening on it
+ /// Uses lsof which is fast for local port checks
+ /// - Parameter port: The port number to check
+ /// - Returns: true if a process is listening on the port, false otherwise
+ static func isLocalPortInUse(port: UInt16) -> Bool {
+ let process = Process()
+ process.executableURL = URL(fileURLWithPath: "/usr/sbin/lsof")
+ process.arguments = ["-i", ":\(port)", "-sTCP:LISTEN"]
+
+ let pipe = Pipe()
+ process.standardOutput = pipe
+ process.standardError = pipe
+
+ do {
+ try process.run()
+ process.waitUntilExit()
+ // lsof returns 0 if it finds a matching process
+ return process.terminationStatus == 0
+ } catch {
+ return false
+ }
+ }
+}