Skip to content

Commit 859280e

Browse files
authored
fix(macos): coalesce location permission requests (#116183)
1 parent 5128df0 commit 859280e

3 files changed

Lines changed: 90 additions & 21 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Docs: https://docs.openclaw.ai
5757

5858
- **Linux gateway service ownership:** refuse user-scope systemd publication and activation when the same gateway unit name is already owned or cannot be verified in the system scope, including `--force`, with actionable recovery guidance instead of creating restart-looping dual managers. Fixes #116129.
5959
- **macOS remote tunnel lifecycle:** prevent cancelled or superseded restart backoffs from recreating SSH tunnels, and join a tunnel create that another caller started while the actor was suspended.
60+
- **macOS location permission requests:** coalesce concurrent prompts so every caller resumes, and stop cancelled timeouts from opening Settings or completing a newer request.
6061
- **Control UI update reconciliation:** preserve an unresolved managed-update request across disconnects, accept the replacement Gateway version when it proves success, and otherwise show explicit recovery guidance instead of trusting an unrelated cached update result or failing silently. Fixes #116075. Thanks @shakkernerd.
6162
- **Control UI model readiness:** put AI setup first when no model is selectable, distinguish signed-in credentials from ready providers, and route accounts with no exposed models directly to provider recovery instead of leading with disabled default controls.
6263
- **Control UI Talk session isolation:** stop active realtime Talk media and retire its callbacks before chat session changes, Gateway disconnects, or pane disposal so previous-session audio, transcript, camera, and status updates cannot leak into the next view. Thanks @shakkernerd.

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

Lines changed: 63 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -295,12 +295,42 @@ enum LocationPermissionHelper {
295295
}
296296
}
297297

298+
@MainActor
299+
final class LocationPermissionRequestCoordinator {
300+
private var continuations: [CheckedContinuation<CLAuthorizationStatus, Never>] = []
301+
302+
var hasPendingRequests: Bool {
303+
!self.continuations.isEmpty
304+
}
305+
306+
var pendingRequestCount: Int {
307+
self.continuations.count
308+
}
309+
310+
func wait(onEnqueue: (_ isFirstRequest: Bool) -> Void) async -> CLAuthorizationStatus {
311+
await withCheckedContinuation { continuation in
312+
let isFirstRequest = self.continuations.isEmpty
313+
self.continuations.append(continuation)
314+
onEnqueue(isFirstRequest)
315+
}
316+
}
317+
318+
func finish(status: CLAuthorizationStatus) {
319+
let continuations = self.continuations
320+
self.continuations.removeAll()
321+
for continuation in continuations {
322+
continuation.resume(returning: status)
323+
}
324+
}
325+
}
326+
298327
@MainActor
299328
final class LocationPermissionRequester: NSObject, CLLocationManagerDelegate {
300329
static let shared = LocationPermissionRequester()
301330
private let manager = CLLocationManager()
302-
private var continuation: CheckedContinuation<CLAuthorizationStatus, Never>?
331+
private let requests = LocationPermissionRequestCoordinator()
303332
private var timeoutTask: Task<Void, Never>?
333+
private var requestedAlways = false
304334

305335
override init() {
306336
super.init()
@@ -318,35 +348,47 @@ final class LocationPermissionRequester: NSObject, CLLocationManagerDelegate {
318348
return current
319349
}
320350

321-
return await withCheckedContinuation { cont in
322-
self.continuation = cont
323-
self.timeoutTask?.cancel()
324-
self.timeoutTask = Task { [weak self] in
325-
try? await Task.sleep(nanoseconds: 3_000_000_000)
326-
await MainActor.run { [weak self] in
327-
guard let self else { return }
328-
guard self.continuation != nil else { return }
329-
LocationPermissionHelper.openSettings()
330-
self.finish(status: self.manager.authorizationStatus)
331-
}
332-
}
333-
if always {
351+
return await self.requests.wait { isFirstRequest in
352+
if isFirstRequest {
353+
self.requestedAlways = always
354+
self.scheduleTimeout()
355+
self.requestAuthorization(always: always)
356+
// On macOS, requesting an actual fix makes the prompt more reliable.
357+
self.manager.requestLocation()
358+
} else if always, !self.requestedAlways {
359+
self.requestedAlways = true
334360
self.manager.requestAlwaysAuthorization()
335-
} else {
336-
self.manager.requestWhenInUseAuthorization()
337361
}
362+
}
363+
}
338364

339-
// On macOS, requesting an actual fix makes the prompt more reliable.
340-
self.manager.requestLocation()
365+
private func requestAuthorization(always: Bool) {
366+
if always {
367+
self.manager.requestAlwaysAuthorization()
368+
} else {
369+
self.manager.requestWhenInUseAuthorization()
370+
}
371+
}
372+
373+
private func scheduleTimeout() {
374+
self.timeoutTask?.cancel()
375+
self.timeoutTask = Task { [weak self] in
376+
do {
377+
try await Task.sleep(nanoseconds: 3_000_000_000)
378+
} catch {
379+
return
380+
}
381+
guard !Task.isCancelled, let self, self.requests.hasPendingRequests else { return }
382+
LocationPermissionHelper.openSettings()
383+
self.finish(status: self.manager.authorizationStatus)
341384
}
342385
}
343386

344387
private func finish(status: CLAuthorizationStatus) {
345388
self.timeoutTask?.cancel()
346389
self.timeoutTask = nil
347-
guard let cont = self.continuation else { return }
348-
self.continuation = nil
349-
cont.resume(returning: status)
390+
self.requestedAlways = false
391+
self.requests.finish(status: status)
350392
}
351393

352394
/// nonisolated for Swift 6 strict concurrency compatibility

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,33 @@ import CoreLocation
22
import Testing
33
@testable import OpenClaw
44

5+
@Suite(.serialized)
6+
@MainActor
57
struct PermissionManagerLocationTests {
8+
@Test
9+
func `concurrent request waiters all resume`() async {
10+
let coordinator = LocationPermissionRequestCoordinator()
11+
let first = Task { @MainActor in
12+
await coordinator.wait { _ in }
13+
}
14+
while coordinator.pendingRequestCount < 1 {
15+
await Task.yield()
16+
}
17+
18+
let second = Task { @MainActor in
19+
await coordinator.wait { _ in }
20+
}
21+
while coordinator.pendingRequestCount < 2 {
22+
await Task.yield()
23+
}
24+
25+
coordinator.finish(status: .denied)
26+
27+
#expect(await first.value == .denied)
28+
#expect(await second.value == .denied)
29+
#expect(coordinator.pendingRequestCount == 0)
30+
}
31+
632
@Test
733
func `authorizedAlways counts for both modes`() {
834
#expect(PermissionManager.isLocationAuthorized(status: .authorizedAlways, requireAlways: false))

0 commit comments

Comments
 (0)