fix: prevent browser paste freeze from xclip hammering clipboard on Linux#18
Merged
Merged
Conversation
…y 0.5 s On Linux, _out_tick called _read_clipboard_image() on every 0.5 s poll tick. That spawned xclip which sent an X11 SelectionRequest to the clipboard owner (typically a browser). Browsers service these on their main thread, so 2 requests/second caused the browser to freeze the moment the user pressed Ctrl+V. Two-layer fix: 1. TARGETS pre-check (_linux_clipboard_has_image): before requesting image bytes, run xclip/wl-paste with TARGETS/--list-types. The clipboard owner responds immediately with a list of formats; if image/png is absent we return None without ever sending an image/png SelectionRequest. This eliminates the expensive request for the common case (text on clipboard). 2. Rate-limit image checks in _out_tick to _LINUX_IMAGE_CHECK_INTERVAL (2 s) on Linux. Even the cheap TARGETS request spawns a subprocess and touches the X server, so throttling to 2 s reduces X11 traffic 4x vs the previous 0.5 s rate. Windows and macOS are unaffected (ImageGrab.grabclipboard() is in-process). Image sync latency on Linux increases to ~2 s in the worst case, which is acceptable. Text sync latency is unchanged. 16 regression tests added in tests/test_linux_paste_freeze.py covering the TARGETS gate, rate-limiter logic, and end-to-end image/text sync correctness.
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.
Summary
_out_tickcalled_read_clipboard_image()on every 0.5 s poll tick. On Linux this spawnsxclip -t image/png -o, which sends an X11SelectionRequestto the clipboard owner (typically a browser). Browsers service these on their main thread — hitting them at 2 req/s caused the browser to freeze when the user pressed Ctrl+V._read_image_from_system_clipboard()now runsxclip -t TARGETS -o(orwl-paste --list-types) first. The clipboard owner responds to TARGETS immediately with a list of format atoms; ifimage/pngis absent we returnNonewithout ever sending the expensive image/pngSelectionRequest. This eliminates the problematic request for the common case (text on clipboard)._out_tickthrottles image checks to_LINUX_IMAGE_CHECK_INTERVAL(2 s) on Linux. Even the cheap TARGETS request spawns a subprocess and touches the X server, so throttling 4× reduces X11 traffic to a level that doesn't interfere with paste.Windows and macOS are unaffected (
ImageGrab.grabclipboard()is in-process, no SelectionRequests). Image sync latency on Linux increases to ~2 s worst case, which is acceptable. Text sync latency is unchanged.Test plan
python -m pytest tests/ --ignore=tests/integration— all 59 tests passtests/test_linux_paste_freeze.pycover:_linux_clipboard_has_image()unit tests (no tools, text-only TARGETS, image TARGETS, fallback to wl-paste, timeout handling)_out_tickrate-limiter skips image check when interval hasn't elapsed_out_tickruns image check after interval elapses_LINUX_IMAGE_CHECK_INTERVALguard (≥ 2 s)https://claude.ai/code/session_01YGXA5W7KpwGtsufDugcxDS
Generated by Claude Code