Skip to content

Commit 0b30b7b

Browse files
committed
fix(macos): back off approval socket retries
1 parent 342826b commit 0b30b7b

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ final class ExecApprovalsPromptServer {
351351
static let shared = ExecApprovalsPromptServer()
352352

353353
private let retryDelay: Duration
354+
private let maximumRetryDelay: Duration
354355
private let resolveSocketCredentials: @Sendable () -> (socketPath: String, token: String)
355356
private let onPrompt: @Sendable (ExecApprovalPromptRequest) async -> ExecApprovalDecision?
356357
private var server: ExecApprovalsSocketServer?
@@ -360,6 +361,7 @@ final class ExecApprovalsPromptServer {
360361

361362
init(
362363
retryDelay: Duration = .seconds(1),
364+
maximumRetryDelay: Duration = .seconds(30),
363365
resolveSocketCredentials: @escaping @Sendable () -> (socketPath: String, token: String) = {
364366
let approvals = ExecApprovalsStore.resolve(agentId: nil)
365367
return (approvals.socketPath, approvals.token)
@@ -371,6 +373,7 @@ final class ExecApprovalsPromptServer {
371373
})
372374
{
373375
self.retryDelay = retryDelay
376+
self.maximumRetryDelay = maximumRetryDelay
374377
self.resolveSocketCredentials = resolveSocketCredentials
375378
self.onPrompt = onPrompt
376379
}
@@ -380,6 +383,7 @@ final class ExecApprovalsPromptServer {
380383
self.startupGeneration &+= 1
381384
let generation = self.startupGeneration
382385
let retryDelay = self.retryDelay
386+
let maximumRetryDelay = self.maximumRetryDelay
383387
let resolveSocketCredentials = self.resolveSocketCredentials
384388
let onPrompt = self.onPrompt
385389
let previousStartupTask = self.previousStartupTask
@@ -394,12 +398,15 @@ final class ExecApprovalsPromptServer {
394398
guard !Task.isCancelled, self?.startupGeneration == generation else { return }
395399

396400
var isFirstAttempt = true
401+
var retryBackoff = ExecApprovalsPromptRetryBackoff(
402+
initialDelay: retryDelay,
403+
maximumDelay: maximumRetryDelay)
397404
while !Task.isCancelled {
398405
if isFirstAttempt {
399406
isFirstAttempt = false
400407
} else {
401408
do {
402-
try await Task.sleep(for: retryDelay)
409+
try await Task.sleep(for: retryBackoff.nextDelay())
403410
} catch {
404411
return
405412
}
@@ -1181,6 +1188,24 @@ private final class ExecApprovalsSocketLifecycleLease: @unchecked Sendable {
11811188
}
11821189
}
11831190

1191+
struct ExecApprovalsPromptRetryBackoff {
1192+
private var currentDelay: Duration
1193+
private let maximumDelay: Duration
1194+
1195+
init(initialDelay: Duration, maximumDelay: Duration) {
1196+
self.currentDelay = initialDelay
1197+
self.maximumDelay = max(initialDelay, maximumDelay)
1198+
}
1199+
1200+
mutating func nextDelay() -> Duration {
1201+
let delay = self.currentDelay
1202+
// A second app can hold the lifecycle lease indefinitely. Back off the
1203+
// SQLite credential read and bind attempt instead of polling every second.
1204+
self.currentDelay = min(self.currentDelay * 2, self.maximumDelay)
1205+
return delay
1206+
}
1207+
}
1208+
11841209
private final class ExecApprovalsSocketServer: @unchecked Sendable {
11851210
private struct OpenedSocket {
11861211
let fd: Int32

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,18 @@ struct ExecApprovalsPromptServerTests {
7979
}
8080
}
8181

82+
@Test
83+
func `prompt server retry backoff grows exponentially and caps`() {
84+
var backoff = ExecApprovalsPromptRetryBackoff(
85+
initialDelay: .milliseconds(10),
86+
maximumDelay: .milliseconds(40))
87+
88+
#expect(backoff.nextDelay() == .milliseconds(10))
89+
#expect(backoff.nextDelay() == .milliseconds(20))
90+
#expect(backoff.nextDelay() == .milliseconds(40))
91+
#expect(backoff.nextDelay() == .milliseconds(40))
92+
}
93+
8294
@Test
8395
func `prompt server reloads credentials after an unavailable approvals read`() async throws {
8496
let root = try self.makeShortSocketRoot()

0 commit comments

Comments
 (0)