fix(computer): reap mac helper after client loss (#11493)
* perf(computer): add mac helper owner-loss benchmark Measure the release helper's resident memory before and after its owner-session deadline. Record exact revisions, per-trial RSS, retained state, and clean-exit latency so lifecycle reclamation is reproducible. * fix(computer): reap mac helper after client loss Bind the detached macOS helper lifetime to authenticated socket ownership. Reap the helper after its final authenticated client disconnects, and add a startup deadline for sessions that never authenticate. * test(computer): harden owner benchmark cleanup * test(computer): make owner benchmark cleanup failure-safe * test(computer): close remaining owner cleanup races
This commit is contained in:
@@ -2579,9 +2579,12 @@ private enum KeyMap {
|
||||
}
|
||||
|
||||
private final class AgentRuntime: NSObject, NSApplicationDelegate {
|
||||
private static let unclaimedSessionDeadline: TimeInterval = 30
|
||||
|
||||
private let socketPath: String
|
||||
private let token: String?
|
||||
private var listener: SocketListener?
|
||||
private var unclaimedSessionTimeout: DispatchWorkItem?
|
||||
|
||||
init(socketPath: String, token: String?) {
|
||||
self.socketPath = socketPath
|
||||
@@ -2590,9 +2593,29 @@ private final class AgentRuntime: NSObject, NSApplicationDelegate {
|
||||
|
||||
func applicationDidFinishLaunching(_ notification: Notification) {
|
||||
do {
|
||||
let listener = try SocketListener(socketPath: socketPath, token: token)
|
||||
let timeout = DispatchWorkItem {
|
||||
fputs("computer-use agent received no authenticated session before its deadline\n", stderr)
|
||||
NSApp.terminate(nil)
|
||||
}
|
||||
unclaimedSessionTimeout = timeout
|
||||
let listener = try SocketListener(
|
||||
socketPath: socketPath,
|
||||
token: token,
|
||||
onSessionClaimed: {
|
||||
timeout.cancel()
|
||||
},
|
||||
onSessionClosed: {
|
||||
DispatchQueue.main.async {
|
||||
NSApp.terminate(nil)
|
||||
}
|
||||
}
|
||||
)
|
||||
self.listener = listener
|
||||
listener.start()
|
||||
DispatchQueue.main.asyncAfter(
|
||||
deadline: .now() + Self.unclaimedSessionDeadline,
|
||||
execute: timeout
|
||||
)
|
||||
} catch {
|
||||
fputs("failed to start computer-use socket: \(error)\n", stderr)
|
||||
NSApp.terminate(nil)
|
||||
@@ -2600,6 +2623,8 @@ private final class AgentRuntime: NSObject, NSApplicationDelegate {
|
||||
}
|
||||
|
||||
func applicationWillTerminate(_ notification: Notification) {
|
||||
unclaimedSessionTimeout?.cancel()
|
||||
unclaimedSessionTimeout = nil
|
||||
listener?.stop()
|
||||
}
|
||||
}
|
||||
@@ -3505,14 +3530,26 @@ private final class ButtonTarget: NSObject {
|
||||
private final class SocketListener: @unchecked Sendable {
|
||||
private let socketPath: String
|
||||
private let token: String?
|
||||
private let onSessionClaimed: () -> Void
|
||||
private let onSessionClosed: () -> Void
|
||||
private let provider = Provider()
|
||||
private let providerLock = NSLock()
|
||||
private let sessionLock = NSLock()
|
||||
private var sessionOwnership = AgentSessionOwnership()
|
||||
private var lastConnectionID: UInt64 = 0
|
||||
private var socketFd: Int32 = -1
|
||||
private var isStopped = false
|
||||
|
||||
init(socketPath: String, token: String?) throws {
|
||||
init(
|
||||
socketPath: String,
|
||||
token: String?,
|
||||
onSessionClaimed: @escaping () -> Void,
|
||||
onSessionClosed: @escaping () -> Void
|
||||
) throws {
|
||||
self.socketPath = socketPath
|
||||
self.token = token
|
||||
self.onSessionClaimed = onSessionClaimed
|
||||
self.onSessionClosed = onSessionClosed
|
||||
try bindSocket()
|
||||
}
|
||||
|
||||
@@ -3587,14 +3624,35 @@ private final class SocketListener: @unchecked Sendable {
|
||||
}
|
||||
continue
|
||||
}
|
||||
guard let connectionID = allocateConnectionID() else {
|
||||
fputs("computer-use socket exhausted connection identities\n", stderr)
|
||||
close(fd)
|
||||
continue
|
||||
}
|
||||
Thread.detachNewThread { [weak self] in
|
||||
self?.handleConnection(fd)
|
||||
self?.handleConnection(fd, connectionID: connectionID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func handleConnection(_ fd: Int32) {
|
||||
defer { close(fd) }
|
||||
private func allocateConnectionID() -> AgentSessionConnectionID? {
|
||||
sessionLock.lock()
|
||||
defer { sessionLock.unlock() }
|
||||
guard lastConnectionID < UInt64.max else { return nil }
|
||||
lastConnectionID += 1
|
||||
return AgentSessionConnectionID(rawValue: lastConnectionID)
|
||||
}
|
||||
|
||||
private func handleConnection(_ fd: Int32, connectionID: AgentSessionConnectionID) {
|
||||
var registeredSession = false
|
||||
var hangupMonitor: AuthenticatedConnectionHangupMonitor?
|
||||
defer {
|
||||
hangupMonitor?.cancel()
|
||||
if registeredSession {
|
||||
disconnectSession(connectionID)
|
||||
}
|
||||
close(fd)
|
||||
}
|
||||
let authorizedPeer = peerProcessId(fd).map(isAuthorizedAgentPeer) == true
|
||||
let decoder = JSONDecoder()
|
||||
while let line = readLine(from: fd) {
|
||||
@@ -3603,6 +3661,40 @@ private final class SocketListener: @unchecked Sendable {
|
||||
else {
|
||||
continue
|
||||
}
|
||||
if !registeredSession && isAuthenticatedAgentSession(
|
||||
expectedToken: token,
|
||||
requestToken: request.token,
|
||||
authorizedPeer: authorizedPeer
|
||||
) {
|
||||
let monitor: AuthenticatedConnectionHangupMonitor
|
||||
do {
|
||||
monitor = try AuthenticatedConnectionHangupMonitor(
|
||||
fileDescriptor: fd,
|
||||
onHangup: { [weak self] in
|
||||
self?.disconnectSession(connectionID)
|
||||
}
|
||||
)
|
||||
} catch {
|
||||
fputs("computer-use owner monitor failed: \(error)\n", stderr)
|
||||
return
|
||||
}
|
||||
sessionLock.lock()
|
||||
let registration = sessionOwnership.registerConnection(
|
||||
connectionID,
|
||||
authenticated: true
|
||||
)
|
||||
sessionLock.unlock()
|
||||
guard registration != .rejected else {
|
||||
monitor.cancel()
|
||||
return
|
||||
}
|
||||
registeredSession = true
|
||||
hangupMonitor = monitor
|
||||
monitor.start()
|
||||
if registration == .claimed {
|
||||
onSessionClaimed()
|
||||
}
|
||||
}
|
||||
let response = handleRequest(
|
||||
provider: provider,
|
||||
lock: providerLock,
|
||||
@@ -3613,6 +3705,15 @@ private final class SocketListener: @unchecked Sendable {
|
||||
writeJSON(response, to: fd)
|
||||
}
|
||||
}
|
||||
|
||||
private func disconnectSession(_ connectionID: AgentSessionConnectionID) {
|
||||
sessionLock.lock()
|
||||
let shouldTerminate = sessionOwnership.disconnect(connectionID)
|
||||
sessionLock.unlock()
|
||||
if shouldTerminate {
|
||||
onSessionClosed()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func existingPathMode(_ path: String) -> mode_t? {
|
||||
|
||||
Reference in New Issue
Block a user