Skip to content

Commit 1787fd6

Browse files
authored
fix(macos): fully persist embedded CUA pid (#124572)
1 parent 0d9a424 commit 1787fd6

3 files changed

Lines changed: 47 additions & 7 deletions

File tree

‎apps/macos/Sources/OpenClaw/CuaDriverHostCoordinator.swift‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -371,15 +371,20 @@ final class CuaDriverHostCoordinator {
371371
self?.processExited(generation: generation, status: status)
372372
}
373373
}
374-
// Record the pid we spawned so startup/teardown reaping can attribute and
375-
// terminate exactly this daemon; without it a reaper can only delete the
376-
// directory and would leave an orphaned privileged process running.
377-
Self.writeProcessIdentifier(process.processIdentifier, to: socketDirectory)
378374
self.runningChild = RunningChild(
379375
generation: generation,
380376
process: process,
381377
socketDirectory: socketDirectory,
382378
executableURL: executableURL)
379+
// Record the pid we spawned so startup/teardown reaping can attribute and
380+
// terminate exactly this daemon; without it a reaper can only delete the
381+
// directory and would leave an orphaned privileged process running.
382+
guard Self.writeProcessIdentifier(process.processIdentifier, to: socketDirectory) else {
383+
self.logger.error("embedded CUA could not record the spawned daemon pid")
384+
await self.ensureStopped()
385+
self.scheduleRestartIfNeeded()
386+
return
387+
}
383388
} catch {
384389
Self.cleanupSocketDirectory(socketDirectory)
385390
self.logger.error("embedded CUA launch failed: \(error.localizedDescription, privacy: .public)")

‎apps/macos/Sources/OpenClaw/CuaDriverHostLifecycle.swift‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,12 @@ extension CuaDriverHostCoordinator {
112112
0o600)
113113
guard descriptor >= 0 else { return false }
114114
defer { close(descriptor) }
115-
let contents = Array("\(processIdentifier)".utf8)
116-
return contents.withUnsafeBytes { bytes in
117-
Darwin.write(descriptor, bytes.baseAddress, bytes.count) == bytes.count
115+
do {
116+
let handle = FileHandle(fileDescriptor: descriptor, closeOnDealloc: false)
117+
try handle.write(contentsOf: Data("\(processIdentifier)".utf8))
118+
return true
119+
} catch {
120+
return false
118121
}
119122
}
120123

‎apps/macos/Tests/OpenClawIPCTests/CuaDriverHostCoordinatorTests.swift‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,38 @@ struct CuaDriverHostCoordinatorTests {
217217
await coordinator.setEnabled(false)
218218
}
219219

220+
@Test func `launch without an authoritative pid record never becomes ready`() async throws {
221+
let root = self.shortTemporaryDirectory("launch-pidfile-failure")
222+
let executable = try self.expectedExecutable(in: root, target: "/bin/sleep")
223+
defer { try? FileManager.default.removeItem(at: root) }
224+
let launcher = CuaProcessLauncherProbe()
225+
let coordinator = CuaDriverHostCoordinator(
226+
artifactURL: { executable },
227+
applicationSupportURL: { root },
228+
bundleIdentifier: { "ai.openclaw.test" },
229+
processLauncher: { launch, onTermination in
230+
let socketIndex = try #require(launch.arguments.firstIndex(of: "--socket"))
231+
let socketPath = try #require(launch.arguments.indices.contains(socketIndex + 1)
232+
? launch.arguments[socketIndex + 1]
233+
: nil)
234+
let pidFile = URL(fileURLWithPath: socketPath)
235+
.deletingLastPathComponent()
236+
.appendingPathComponent("cua.pid")
237+
try Data("incomplete".utf8).write(to: pidFile)
238+
return launcher.launch(launch, onTermination: onTermination)
239+
},
240+
readinessProbe: { _ in true })
241+
242+
await coordinator.setEnabled(true)
243+
244+
#expect(coordinator.workerEndpoint == nil)
245+
#expect(launcher.processes.count == 1)
246+
#expect(launcher.processes.allSatisfy { !$0.isRunning })
247+
await coordinator.setEnabled(false)
248+
let cuaRoot = root.appendingPathComponent("OpenClaw/cua", isDirectory: true)
249+
#expect(try FileManager.default.contentsOfDirectory(atPath: cuaRoot.path).isEmpty)
250+
}
251+
220252
@Test func `startup refuses to signal a pid owned by another executable`() async throws {
221253
let root = self.shortTemporaryDirectory("startup-pid-reuse")
222254
let expectedExecutable = try self.expectedExecutable(in: root, target: "/bin/cat")

0 commit comments

Comments
 (0)