Skip to content

Fix Wayland FD leak and clipboard sync feedback loop - #51

Open
halfcrazy wants to merge 2 commits into
dnut:masterfrom
halfcrazy:fix/wayland-fd-leak-and-sync-loop
Open

Fix Wayland FD leak and clipboard sync feedback loop#51
halfcrazy wants to merge 2 commits into
dnut:masterfrom
halfcrazy:fix/wayland-fd-leak-and-sync-loop

Conversation

@halfcrazy

Copy link
Copy Markdown

Summary

  • Add WlrBackend with a persistent Wayland data-control connection, mirroring the existing X11Backend pattern, so polling no longer opens a new wl-clipboard-rs session on every get()/set() and exhausts file descriptors (EMFILE after ~5 minutes under GNOME Wayland).
  • Upgrade wl-clipboard-rs to 0.9.3 and wayland-client to 0.31.14 (adds ext-data-control support on GNOME).
  • Rewrite keep_synced to track per-clipboard last-known content and add a short post-sync settle period, preventing the X11/Wayland write-lag feedback loop that spammed clipboard updated from display :1 logs.

Fixes #50

Root cause

WlrClipboard called wl_clipboard_rs::paste::get_contents() / copy::Options::copy() on every poll (~5 Hz per clipboard). Each call runs initialize() and opens a new Wayland socket, leaking FDs until ulimit -n is hit.

Separately, after syncing a change, comparing all clipboards against clipboards[0] caused repeated false positives when Wayland and X11 reads diverged briefly after a write.

Test plan

  • Run under GNOME Wayland with WlrClipboard { wayland-1 } + X11Clipboard { :1 }
  • Confirm FD count stays stable (~28) during polling instead of growing ~3–4/sec
  • Copy text on Wayland side; verify it appears on X11 side
  • Confirm only one log line per user copy (no :1 spam)
  • Process survives beyond 10 minutes without EMFILE panic

Made with Cursor

@halfcrazy
halfcrazy force-pushed the fix/wayland-fd-leak-and-sync-loop branch from 0d60c93 to d8670c4 Compare July 8, 2026 05:17
Reuse a persistent Wayland data-control connection in WlrBackend instead
of opening a new wl-clipboard-rs session on every get/set, which caused
EMFILE crashes under polling. Upgrade wl-clipboard-rs and wayland-client,
and rewrite keep_synced to track per-clipboard state with a post-sync
settle period so X11/Wayland write lag no longer spams sync logs.

Fixes dnut#50

Co-authored-by: Cursor <cursoragent@cursor.com>
@halfcrazy
halfcrazy force-pushed the fix/wayland-fd-leak-and-sync-loop branch from d8670c4 to ba5ebda Compare July 28, 2026 02:38
Large clipboard transfers exposed three failures that wiped or froze
clipboard synchronization:

- wlr_backend served data-source Send requests inline during event
  dispatch with a blocking write. Once the main loop owned the selection,
  its own get_text() roundtrip triggered Send for content larger than the
  pipe capacity: the write blocked waiting for the read end, which only
  runs after the roundtrip returns — a deadlock that froze all syncing
  and left Wayland pastes unserved until the watchdog restart. Send is
  now served from a side thread, and a failed write no longer destroys
  the source (a reader going away must not clear the clipboard).

- X11 reads ran synchronously in the poll loop. Slow INCR transfers
  (selection owners proxying data from another machine) starved Wayland
  event dispatch, so Wayland apps timed out pasting. Reads now run in a
  dedicated watcher thread with a bounded timeout and populate a cache;
  the poll loop never blocks on X11. This also replaces the
  terminal-clipboard wrapper with direct x11-clipboard usage, lifting
  its hardcoded 2s read timeout that large transfers could never meet.

- Transient backend read failures (Wayland NoSeats/NoMimeType when the
  selection holds non-text data, X11 errors collapsed by
  unwrap_or_default) surfaced as empty strings and were propagated as
  clipboard clears, wiping every synced clipboard. Empty reads are now
  never treated as changes.
@halfcrazy

Copy link
Copy Markdown
Author

ping

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.

Crashes with EMFILE (too many open files) due to Wayland connection fd leak in WlrClipboard

1 participant