Fix clipboard race condition: previous transcription pasted instead of current#129
Open
YounesMadrid69 wants to merge 1 commit intoStarmel:masterfrom
Open
Conversation
Owner
|
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>
d9c5307 to
79897fa
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:CGEvent.postreturns immediately)The issue:
CGEvent.post(tap: .cghidEventTap)posts the keyboard event asynchronously. The target app needs to: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.