Skip to content

[desktop/macos] Timing-based coordination: 134 asyncAfter + 169 Task.sleep call sites #8846

Open

Description

@Git-on-my-level

Area: Concurrency — race-prone delays

Problem

The Swift app coordinates state transitions with wall-clock delays instead of events: 134 DispatchQueue.main.asyncAfter and 169 Task.sleep call sites. FloatingControlBar/FloatingControlBarWindow.swift alone has 10 asyncAfters, including "settle delay" patterns (askOmiSettleDelay at :961/:976/:1148) and a bare + 0.05 (:1321). These work on a fast idle machine and misfire under load, on slower Macs, or when two delayed closures interleave — the classic source of "flaky UI" bug reports that get fixed by tuning the constant (the fix/revert rate for this area supports that: ~35% of desktop commit subjects contain fix/revert).

Evidence

grep -rc asyncAfter Desktop/Sources --include="*.swift"   # 134
grep -rc "Task.sleep" Desktop/Sources --include="*.swift" # 169

Hotspots: FloatingControlBarWindow.swift (10), OmiApp.swift (6), plus PTT/agent-pill flows (which already needed PTTSilentMicRecoveryPolicyTests, StallDetectorTests, ReentrancyGateTests — scars of timing bugs).

Suggested fix

  1. Not a bulk rewrite — triage the hotspots (floating bar show/hide/focus sequencing, PTT, onboarding) and replace delays with the actual signal being waited on (window didBecomeKey, animation completion handlers, state-machine transitions). The repo already has good state machines (PushToTalkStateMachine) — extend that pattern.
  2. Add a ratchet in CI: record the current counts; fail if a PR increases them. This costs nothing and stops the pattern spreading while hotspots burn down.
  3. Where a delay is genuinely correct (debounce, animation), route it through a named helper (Debouncer, withSettleDelay(reason:)) so intent is greppable and remaining raw asyncAfter sites are, by definition, suspicious.

Open questions

  • Which delays are load-bearing workarounds for AppKit/SwiftUI ordering quirks (may need to stay, but should be documented at the call site)?

Activity

  1. added
    ux-polish
    p3
    on Jul 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    p3refactorux-polish

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions