Skip to content

fix: prevent clipboard restore from running when task is cancelled#10

Open
Ahmet-4955 wants to merge 1 commit into
cmagnussen:mainfrom
Ahmet-4955:fix/clipboard-restore-on-task-cancellation
Open

fix: prevent clipboard restore from running when task is cancelled#10
Ahmet-4955 wants to merge 1 commit into
cmagnussen:mainfrom
Ahmet-4955:fix/clipboard-restore-on-task-cancellation

Conversation

@Ahmet-4955

Copy link
Copy Markdown

Summary

Replace try? with do/catch { return } in schedulePasteboardRestore to correctly handle Swift Task cancellation.

Problem

try? on Task.sleep silently swallows CancellationError, causing the restore task to continue executing immediately after being cancelled — resulting in the old clipboard content being pasted instead of the transcribed text.

Race condition:

  1. Transcript written to clipboard with marker M
  2. 60 s fallback restore task (A) scheduled with marker M
  3. performPaste() fires Cmd+V
  4. 1.5 s post-paste restore task (B) scheduled → cancels task A
  5. Task A: try? swallows CancellationErrortask continues running immediately
  6. Task A: marker M still matches → old clipboard content restored
  7. Target app processes Cmd+V — but clipboard now has old content → wrong text pasted

Fix

// Before
try? await Task.sleep(for: .seconds(delay))

// After
do {
    try await Task.sleep(for: .seconds(delay))
} catch {
    return
}

Cancellation now causes the task to exit cleanly without triggering the clipboard restore.

Test plan

  • Record audio via Blitztext popover or hotkey
  • Confirm the transcript — not the previous clipboard content — is pasted into the target app
  • Confirm the previous clipboard content is correctly restored ~1.5 s after the paste (press Cmd+V again to verify)

🤖 Generated with Claude Code

Using `try?` on `Task.sleep` silently swallows `CancellationError`,
causing the restore task to continue executing immediately after being
cancelled. This created a race condition where the old clipboard content
was restored before the target app had a chance to process the simulated
Cmd+V paste event — resulting in the previous clipboard content being
pasted instead of the transcribed text.

Replace `try?` with a proper `do/catch { return }` so that cancellation
causes the task to exit cleanly without triggering the clipboard restore.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@Ahmet-4955 Ahmet-4955 requested a review from cmagnussen as a code owner June 14, 2026 23:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant