Skip to content

Fix clipboard race condition: previous transcription pasted instead of current#129

Open
YounesMadrid69 wants to merge 1 commit intoStarmel:masterfrom
YounesMadrid69:fix/clipboard-paste-race-condition
Open

Fix clipboard race condition: previous transcription pasted instead of current#129
YounesMadrid69 wants to merge 1 commit intoStarmel:masterfrom
YounesMadrid69:fix/clipboard-paste-race-condition

Conversation

@YounesMadrid69
Copy link
Copy Markdown

Problem

When using OpenSuperWhisper, the previous transcription is sometimes pasted instead of the current one. This happens frequently, especially when the Mac is under load (right after Whisper finishes transcribing).

Root Cause

In ClipboardUtil.insertText(), the flow is:

  1. Save current clipboard contents
  2. Set new transcription text in clipboard
  3. Simulate Cmd+V paste (async — CGEvent.post returns immediately)
  4. Wait 100ms
  5. Restore original clipboard contents

The issue: CGEvent.post(tap: .cghidEventTap) posts the keyboard event asynchronously. The target app needs to:

  • Receive the Cmd+V event
  • Process it as a paste command
  • Read the clipboard

If this takes longer than 100ms (common when CPU was just busy with Whisper transcription, or the target app is an IDE/browser), the clipboard is already restored to the old contents before the app reads it.

Fix

Increase the delay from 100ms to 500ms, giving the target app sufficient time to process the paste event and read the clipboard.

Testing

Tested with rapid successive dictations in terminal (Claude Code) and browser. The previous transcription no longer gets pasted.

@Starmel
Copy link
Copy Markdown
Owner

Starmel commented Mar 14, 2026

Thank you for contributing. Could you please make the delay dynamic based on CPU usage? The app should use a fast delay (0.1) when the CPU is not under heavy load, and increase the delay when CPU utilization is high and a race condition may occur.

Replace the fixed 0.1s delay with a dynamic delay that adapts to
system CPU load using getloadavg(). The 1-minute load average is
normalized by processor count to determine system utilization:

- CPU < 50%: 0.1s (system idle, fast restore)
- CPU 50-80%: 0.3s (moderate load)
- CPU > 80%: 0.5s (heavy load, e.g. right after Whisper transcription)

This prevents the race condition where clipboard contents are restored
before the paste operation completes, which caused the previous
clipboard content to be pasted instead of the transcription.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@YounesMadrid69 YounesMadrid69 force-pushed the fix/clipboard-paste-race-condition branch from d9c5307 to 79897fa Compare March 23, 2026 22:58
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.

2 participants