Skip to content

Generalize XPCClient timeout pattern as withTimeout(seconds:operation:) #24

@hakaru

Description

@hakaru

From Gemini refactoring audit (2026-05-04) — LOW.

`MacSlowCooker/XPCClient.swift:30-72` (`fetchHelperVersion`) implements timeout by spawning a parallel `Task` that invalidates the connection on expiry, leveraging the proxy's error handler to resume the continuation. Clever, correct, but specific to NSXPCConnection.

If we ever add another XPC method that needs a bounded timeout (e.g., a future `fetchHelperDiagnostics`), we'll re-implement the same pattern.

Refactor

Extract a generic helper:

```swift
@mainactor
static func withTimeout(
seconds: TimeInterval,
operation: @escaping (NSXPCConnection) async throws -> T,
on connection: NSXPCConnection
) async throws -> T? {
// ... timeout task that invalidates conn on expiry
}
```

The challenge: NSXPCConnection's error-handler-based callback contract doesn't compose cleanly with generic async/await. May need a small actor-based wrapper.

Acceptance

  • `fetchHelperVersion` reduces to a thin wrapper around the helper
  • Future XPC calls with timeout reuse the same primitive

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:concurrencyThreading / actor isolation / data racespriority:lowPolish, only-if-measured optimizationrefactorCode organization, naming, abstraction

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions