Skip to content

Fix clipboard deadlock by removing aggressive Wayland image watcher - #11715

Open
D-Pagey wants to merge 1 commit into
omacom:quattrofrom
D-Pagey:fix/clipboard-image-deadlock
Open

D-Pagey wants to merge 1 commit into
omacom:quattrofrom
D-Pagey:fix/clipboard-image-deadlock

Conversation

@D-Pagey

@D-Pagey D-Pagey commented Sep 13, 2026 •

Copy link
Copy Markdown

Problem:
The Omarchy clipboard manager ran a continuous wl-paste --type image/png --watch process to capture image history. In Wayland, when an image is copied (e.g., from Chromium, Brave, or a screenshot tool), the source application often uses a single-use pipe to serve the clipboard data. Because this background watcher immediately requests the data, it consumes the single-use pipe before the user can paste it into another application. This leaves the user's paste target with nothing and results in stuck wl-copy processes that deadlock the clipboard entirely.

Solution:
This PR completely removes the imageWatchProc from the clipboard manager.

While this means Omarchy no longer maintains a background history of copied images, it guarantees that image pasting works reliably 100% of the time. Text history remains perfectly intact because text payloads are tiny and aren't subject to the same single-use pipe restrictions. An explicit test has been added to ensure the image watcher is not accidentally reintroduced.


Retaining Image History

Removing the background watcher drops the image history feature. Building a background image clipboard manager on Wayland using purely bash and wl-paste is fundamentally flawed due to the single-use pipe issue. If the project wants to retain image history, here are the architectural alternatives:

1. "Capture on Demand" (Best UX without new dependencies)
Instead of a background watcher, modify Clipboard.qml to read the clipboard only when the user opens the history menu (Super+Ctrl+V).

  • Pros: Zero background SSD/CPU thrashing, 100% reliable pasting.
  • Cons: It won't keep a historical backlog of every image copied, only the one currently on the clipboard when the menu is opened.

2. Opt-In Toggle
Restore the watcher but put it behind a toggle (e.g. omarchy toggle image-clipboard-history).

  • Pros: Preserves the feature for users who rely on it.
  • Cons: Leaves a known, confusing bug in the system for anyone who enables it and then tries to paste an image into an affected app.

3. Native Wayland Daemon
Integrate a native Wayland data-control daemon (like cliphist or wl-clip-persist) that is explicitly designed to safely proxy and store clipboard file descriptors without destroying the offer for the user.

  • Pros: Perfect UX (background history + reliable pasting).
  • Cons: Requires adding and maintaining a new compiled dependency in the Omarchy package set.

@D-Pagey
D-Pagey force-pushed the fix/clipboard-image-deadlock branch from 8ac2f51 to 03a253c Compare September 13, 2026 16:55
@D-Pagey D-Pagey changed the title Fix clipboard deadlock by replacing aggressive image watcher with a polling timer Fix clipboard deadlock by removing aggressive Wayland image watcher Sep 13, 2026
@omarchybot omarchybot added the bug Something isn't working label Sep 27, 2026
@llstrk

llstrk commented Oct 1, 2026

Copy link
Copy Markdown

Automated AI review

Community review: Independent automated community review, unaffiliated with the Omarchy team, intended to help prepare PRs for their review.

Outcome: A clipboard read does not use up a Wayland selection, so this drops image history without a demonstrated cause.

The single-use pipe premise does not reproduce

Hyprland forwards each read to the owner; wl-copy 2.3.0 and Chromium answer each on its own fd. With the removed watcher, 15 of 15 pastes over 5 copies were identical and wl-copy never stuck. Impact: new image copies go unrecorded; the hang stays unexplained.

Suggested change: Share ps -eo pid,ppid,etime,args output from a hang; if capture.sh is stalled, bounding its reads (open #9488) keeps image history.

Reproducer (premise, at 03a253c)

Checks the PR's premise with the image watcher the PR removes (the two watcher commands are copied from Clipboard.qml at the merge base, lines 285 and 294). capture.sh is unchanged by the PR. The script uses the real wl-copy/wl-paste (wl-clipboard 2.3.0, the version Arch ships) on a private Wayland display (relay.c). That display only implements wl_seat and ext-data-control, and it forwards every read to the clipboard owner, as Hyprland does (src/protocols/ExtDataDevice.cpp, lines 14-29 at v0.56.2). Your session clipboard is not touched.

The watcher reads the 3 MiB image first, then three pastes each still get identical bytes, and wl-copy keeps serving. The last line shows that the shell-start snapshot (currentProc, which the PR keeps) still records the current image.

Run: put relay.c and repro.sh in one directory, then OMARCHY=/path/to/omarchy-checkout bash repro.sh (needs wl-clipboard, gcc, pkg-config, wayland-scanner, wayland-protocols, the libwayland development files, jq, perl, setpriv).

relay.c

// Minimal Wayland display with only wl_seat and ext-data-control, relaying
// clipboard reads the way wlroots/Hyprland do: each offer.receive(mime, fd)
// becomes source.send(mime, fd). Logs one line per relayed read to stderr.
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wayland-server.h>
#include "ext-data-control-v1-server-protocol.h"

struct source { struct wl_resource *res; struct wl_array mimes; };
struct offer { struct source *src; struct wl_list link; };
struct device { struct wl_resource *res; struct wl_list link; };
static struct wl_list devices, offers;
static struct source *selection;

static void offer_receive(struct wl_client *c, struct wl_resource *r, const char *mime, int32_t fd) {
  struct offer *o = wl_resource_get_user_data(r);
  if (o->src && o->src == selection) {
    fprintf(stderr, "relay: read of %s forwarded to the source\n", mime);
    ext_data_control_source_v1_send_send(o->src->res, mime, fd);
  }
  close(fd);
}
static void destroy_req(struct wl_client *c, struct wl_resource *r) { wl_resource_destroy(r); }
static const struct ext_data_control_offer_v1_interface offer_impl = {offer_receive, destroy_req};
static void offer_gone(struct wl_resource *r) {
  struct offer *o = wl_resource_get_user_data(r);
  wl_list_remove(&o->link);
  free(o);
}

static void send_selection(struct device *d) {
  if (!selection) { ext_data_control_device_v1_send_selection(d->res, NULL); return; }
  struct wl_resource *or = wl_resource_create(wl_resource_get_client(d->res), &ext_data_control_offer_v1_interface, 1, 0);
  struct offer *o = calloc(1, sizeof *o);
  o->src = selection;
  wl_list_insert(&offers, &o->link);
  wl_resource_set_implementation(or, &offer_impl, o, offer_gone);
  ext_data_control_device_v1_send_data_offer(d->res, or);
  char **m;
  wl_array_for_each(m, &selection->mimes) ext_data_control_offer_v1_send_offer(or, *m);
  ext_data_control_device_v1_send_selection(d->res, or);
}
static void broadcast(void) { struct device *d; wl_list_for_each(d, &devices, link) send_selection(d); }

static void set_selection(struct wl_client *c, struct wl_resource *r, struct wl_resource *sr) {
  struct source *s = sr ? wl_resource_get_user_data(sr) : NULL, *old = selection;
  if (s == old) return;
  selection = s;
  if (old) ext_data_control_source_v1_send_cancelled(old->res);
  broadcast();
}
static void set_primary(struct wl_client *c, struct wl_resource *r, struct wl_resource *sr) {}
static const struct ext_data_control_device_v1_interface device_impl = {set_selection, destroy_req, set_primary};
static void device_gone(struct wl_resource *r) {
  struct device *d = wl_resource_get_user_data(r);
  wl_list_remove(&d->link);
  free(d);
}

static void source_offer(struct wl_client *c, struct wl_resource *r, const char *mime) {
  struct source *s = wl_resource_get_user_data(r);
  *(char **)wl_array_add(&s->mimes, sizeof(char *)) = strdup(mime);
}
static const struct ext_data_control_source_v1_interface source_impl = {source_offer, destroy_req};
static void source_gone(struct wl_resource *r) {
  struct source *s = wl_resource_get_user_data(r);
  struct offer *o;
  wl_list_for_each(o, &offers, link) if (o->src == s) o->src = NULL;
  if (selection == s) { selection = NULL; broadcast(); }
  char **m;
  wl_array_for_each(m, &s->mimes) free(*m);
  wl_array_release(&s->mimes);
  free(s);
}

static void create_source(struct wl_client *c, struct wl_resource *r, uint32_t id) {
  struct source *s = calloc(1, sizeof *s);
  wl_array_init(&s->mimes);
  s->res = wl_resource_create(c, &ext_data_control_source_v1_interface, 1, id);
  wl_resource_set_implementation(s->res, &source_impl, s, source_gone);
}
static void get_device(struct wl_client *c, struct wl_resource *r, uint32_t id, struct wl_resource *seat) {
  struct device *d = calloc(1, sizeof *d);
  d->res = wl_resource_create(c, &ext_data_control_device_v1_interface, 1, id);
  wl_resource_set_implementation(d->res, &device_impl, d, device_gone);
  wl_list_insert(&devices, &d->link);
  send_selection(d);
}
static const struct ext_data_control_manager_v1_interface manager_impl = {create_source, get_device, destroy_req};
static void bind_manager(struct wl_client *c, void *data, uint32_t v, uint32_t id) {
  wl_resource_set_implementation(wl_resource_create(c, &ext_data_control_manager_v1_interface, 1, id), &manager_impl, NULL, NULL);
}
static void bind_seat(struct wl_client *c, void *data, uint32_t v, uint32_t id) {
  struct wl_resource *r = wl_resource_create(c, &wl_seat_interface, v, id);
  wl_resource_set_implementation(r, NULL, NULL, NULL);
  wl_seat_send_capabilities(r, 0);
  wl_seat_send_name(r, "seat0");
}
static int quit(int sig, void *dpy) { wl_display_terminate(dpy); return 0; }

int main(void) {
  signal(SIGPIPE, SIG_IGN);
  wl_list_init(&devices);
  wl_list_init(&offers);
  struct wl_display *dpy = wl_display_create();
  const char *name = wl_display_add_socket_auto(dpy);
  wl_global_create(dpy, &wl_seat_interface, 2, NULL, bind_seat);
  wl_global_create(dpy, &ext_data_control_manager_v1_interface, 1, NULL, bind_manager);
  wl_event_loop_add_signal(wl_display_get_event_loop(dpy), SIGTERM, quit, dpy);
  printf("%s\n", name);
  fflush(stdout);
  wl_display_run(dpy);
  wl_display_destroy_clients(dpy);
  wl_display_destroy(dpy);
  return 0;
}

repro.sh

#!/bin/bash
# Does the image watcher that PR #11715 removes stop a paste from getting an image?
# Usage: OMARCHY=/path/to/omarchy/checkout bash repro.sh   (needs relay.c next to it)
# Needs wl-clipboard, gcc, wayland-scanner, wayland-protocols, jq, perl, setpriv.
# Everything runs on a private Wayland display; the session clipboard is not touched.
set -u
here=$(cd "$(dirname "$0")" && pwd)
work=$(mktemp -d)
pids=()
trap 'kill "${pids[@]}" 2>/dev/null; wait 2>/dev/null; rm -rf "$work"' EXIT
export XDG_RUNTIME_DIR=$work HOME=$work/home XDG_STATE_HOME=$work/state
mkdir -p "$HOME" "$XDG_STATE_HOME"

xml=$(pkg-config --variable=pkgdatadir wayland-protocols)/staging/ext-data-control/ext-data-control-v1.xml
wayland-scanner server-header "$xml" "$work/ext-data-control-v1-server-protocol.h"
wayland-scanner private-code "$xml" "$work/ext-data-control-v1-protocol.c"
gcc -I"$work" -o "$work/relay" "$here/relay.c" "$work/ext-data-control-v1-protocol.c" -lwayland-server || exit 1

"$work/relay" >"$work/display" 2>"$work/relay.log" &
pids+=($!)
until [[ -s $work/display ]]; do sleep 0.05; done
export WAYLAND_DISPLAY=$(<"$work/display")

# The two watchers as Clipboard.qml declares them before the PR.
capture=$OMARCHY/shell/plugins/clipboard/capture.sh
setpriv --pdeathsig TERM wl-paste --type text --watch "$capture" text >"$work/text-watcher.out" &
pids+=($!)
setpriv --pdeathsig TERM wl-paste --type image/png --watch "$capture" image/png >"$work/image-watcher.out" &
pids+=($!)
sleep 1

{ printf '\x89PNG\r\n\x1a\n'; head -c 3145728 /dev/urandom; } >"$work/image.png"
wl-copy --foreground --type image/png <"$work/image.png" &
copy_pid=$!
pids+=($copy_pid)

for _ in $(seq 1 100); do [[ -s $work/image-watcher.out ]] && break; sleep 0.05; done
echo "image watcher recorded: $(jq -c '{type, mime}' "$work/image-watcher.out")"
cmp -s "$(jq -r .path "$work/image-watcher.out")" "$work/image.png" && echo "image watcher's stored copy is identical"

for i in 1 2 3; do
  if timeout 5 wl-paste --type image/png | cmp -s - "$work/image.png"; then
    echo "paste $i after the watcher's read: identical $(stat -c %s "$work/image.png") bytes"
  else
    echo "paste $i after the watcher's read: DIFFERENT or empty"
  fi
done
kill -0 "$copy_pid" 2>/dev/null && echo "wl-copy is still serving the image"
echo "reads the source served:"
sed 's/^/  /' "$work/relay.log"

# Shell start runs capture.sh with no arguments (currentProc), which the PR keeps.
echo "startup snapshot at the PR head records: $("$capture" | jq -c '{type, mime}')"

Actual output (PR head 03a253c, wl-clipboard 2.3.0)

image watcher recorded: {"type":"image","mime":"image/png"}
image watcher's stored copy is identical
paste 1 after the watcher's read: identical 3145736 bytes
paste 2 after the watcher's read: identical 3145736 bytes
paste 3 after the watcher's read: identical 3145736 bytes
wl-copy is still serving the image
reads the source served:
  relay: read of image/png forwarded to the source
  relay: read of image/png forwarded to the source
  relay: read of image/png forwarded to the source
  relay: read of image/png forwarded to the source
startup snapshot at the PR head records: {"type":"image","mime":"image/png"}
More findings: the kept text watcher still reads browser image copies

For images with a URL, Chromium's "Copy image" also offers text/html, which wl-paste --type text reads. Impact: by the PR's own premise, those browser image copies would still break, and their only new history entry is the <img> markup.

Suggested change: Drop the description's claim that text copies are exempt.

Reproducer (text watcher, at 03a253c)

Chromium's "Copy image" writes image/png and, when the image has a URL, also text/html <img> markup (SystemClipboard::WriteImageWithTag, third_party/blink/renderer/core/clipboard/system_clipboard.cc, lines 301-347). wl-paste --type text falls back to any text/* type when no text/plain is offered (src/wl-paste.c, lines 145-148, wl-clipboard 2.3.0). Its bookmark write also adds text/x-moz-url (clipboard_ozone.cc, lines 746-754; Chromium main 9d90aa5c). source.c is a clipboard owner with these three types that serves every read separately, as Chromium's Wayland data source does. Only the text watcher from Clipboard.qml at the PR head is running.

The text watcher reads text/html from the image owner on the copy and stores the markup as a text entry. A later image paste is still identical.

Run: put relay.c (from the first reproducer), source.c and repro-text-watcher.sh in one directory, then OMARCHY=/path/to/omarchy-checkout bash repro-text-watcher.sh.

source.c

// A clipboard owner shaped like Chromium's "Copy image": it offers image/png,
// text/html (<img> markup) and text/x-moz-url, serves every read separately
// from memory on a forked writer, and logs each read to stderr.
// Usage: source IMAGEFILE   (prints "ready" once it owns the selection)
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <wayland-client.h>
#include "ext-data-control-v1-client-protocol.h"

static struct wl_seat *seat;
static struct ext_data_control_manager_v1 *manager;
static char image[8 << 20];
static size_t image_len;
static const char html[] = "<img src=\"https://example.com/cat.png\" alt=\"cat\">";
static const char mozurl[] = "h\0t\0t\0p\0s\0:\0/\0/\0e\0x\0a\0m\0p\0l\0e\0.\0c\0o\0m\0";

static void global(void *d, struct wl_registry *r, uint32_t name, const char *iface, uint32_t v) {
  if (!strcmp(iface, "wl_seat") && !seat) seat = wl_registry_bind(r, name, &wl_seat_interface, 2);
  if (!strcmp(iface, "ext_data_control_manager_v1")) manager = wl_registry_bind(r, name, &ext_data_control_manager_v1_interface, 1);
}
static void global_remove(void *d, struct wl_registry *r, uint32_t name) {}
static const struct wl_registry_listener registry_listener = {global, global_remove};

static void on_send(void *d, struct ext_data_control_source_v1 *s, const char *mime, int32_t fd) {
  const char *buf = !strcmp(mime, "image/png") ? image : !strcmp(mime, "text/html") ? html : mozurl;
  size_t len = buf == image ? image_len : buf == html ? strlen(html) : sizeof mozurl - 1;
  fprintf(stderr, "source: served a read of %s\n", mime);
  if (fork() == 0) {
    fcntl(fd, F_SETFL, 0);
    for (size_t off = 0; off < len;) {
      ssize_t w = write(fd, buf + off, len - off);
      if (w <= 0) break;
      off += w;
    }
    _exit(0);
  }
  close(fd);
}
static void on_cancelled(void *d, struct ext_data_control_source_v1 *s) { exit(0); }
static const struct ext_data_control_source_v1_listener source_listener = {on_send, on_cancelled};

static void data_offer(void *d, struct ext_data_control_device_v1 *dev, struct ext_data_control_offer_v1 *o) {}
static void selection(void *d, struct ext_data_control_device_v1 *dev, struct ext_data_control_offer_v1 *o) {
  if (o) ext_data_control_offer_v1_destroy(o);
}
static void finished(void *d, struct ext_data_control_device_v1 *dev) { exit(1); }
static const struct ext_data_control_device_v1_listener device_listener = {data_offer, selection, finished, selection};

int main(int argc, char **argv) {
  signal(SIGCHLD, SIG_IGN);
  int fd = open(argv[1], O_RDONLY);
  image_len = read(fd, image, sizeof image);
  struct wl_display *dpy = wl_display_connect(NULL);
  wl_registry_add_listener(wl_display_get_registry(dpy), &registry_listener, NULL);
  wl_display_roundtrip(dpy);
  struct ext_data_control_source_v1 *src = ext_data_control_manager_v1_create_data_source(manager);
  ext_data_control_source_v1_add_listener(src, &source_listener, NULL);
  ext_data_control_source_v1_offer(src, "image/png");
  ext_data_control_source_v1_offer(src, "text/html");
  ext_data_control_source_v1_offer(src, "text/x-moz-url");
  struct ext_data_control_device_v1 *dev = ext_data_control_manager_v1_get_data_device(manager, seat);
  ext_data_control_device_v1_add_listener(dev, &device_listener, NULL);
  ext_data_control_device_v1_set_selection(dev, src);
  wl_display_roundtrip(dpy);
  printf("ready\n");
  fflush(stdout);
  while (wl_display_dispatch(dpy) >= 0) {}
  return 1;
}

repro-text-watcher.sh

#!/bin/bash
# Does the text watcher that PR #11715 keeps still read from a browser-style image copy?
# Usage: OMARCHY=/path/to/omarchy/checkout bash repro-text-watcher.sh   (relay.c and source.c next to it)
# Needs wl-clipboard, gcc, wayland-scanner, wayland-protocols, jq, perl, setpriv.
set -u
here=$(cd "$(dirname "$0")" && pwd)
work=$(mktemp -d)
pids=()
trap 'kill "${pids[@]}" 2>/dev/null; wait 2>/dev/null; rm -rf "$work"' EXIT
export XDG_RUNTIME_DIR=$work HOME=$work/home XDG_STATE_HOME=$work/state
mkdir -p "$HOME" "$XDG_STATE_HOME"

xml=$(pkg-config --variable=pkgdatadir wayland-protocols)/staging/ext-data-control/ext-data-control-v1.xml
wayland-scanner server-header "$xml" "$work/ext-data-control-v1-server-protocol.h"
wayland-scanner client-header "$xml" "$work/ext-data-control-v1-client-protocol.h"
wayland-scanner private-code "$xml" "$work/ext-data-control-v1-protocol.c"
gcc -I"$work" -o "$work/relay" "$here/relay.c" "$work/ext-data-control-v1-protocol.c" -lwayland-server || exit 1
gcc -I"$work" -o "$work/source" "$here/source.c" "$work/ext-data-control-v1-protocol.c" -lwayland-client || exit 1

"$work/relay" >"$work/display" 2>/dev/null &
pids+=($!)
until [[ -s $work/display ]]; do sleep 0.05; done
export WAYLAND_DISPLAY=$(<"$work/display")

# The only watcher Clipboard.qml declares at the PR head.
capture=$OMARCHY/shell/plugins/clipboard/capture.sh
setpriv --pdeathsig TERM wl-paste --type text --watch "$capture" text >"$work/text-watcher.out" &
pids+=($!)
sleep 1

{ printf '\x89PNG\r\n\x1a\n'; head -c 3145728 /dev/urandom; } >"$work/image.png"
"$work/source" "$work/image.png" >"$work/source.ready" 2>"$work/source.log" &
pids+=($!)
for _ in $(seq 1 100); do [[ -s $work/text-watcher.out ]] && break; sleep 0.05; done

echo "reads the source served after the copy:"
sed 's/^/  /' "$work/source.log"
echo "text watcher recorded: $(jq -c . "$work/text-watcher.out")"
timeout 5 wl-paste --type image/png | cmp -s - "$work/image.png" && echo "a later image paste is still identical"

Actual output (PR head 03a253c, wl-clipboard 2.3.0)

reads the source served after the copy:
  source: served a read of text/html
text watcher recorded: {"type":"text","text":"<img src=\"https://example.com/cat.png\" alt=\"cat\">"}
a later image paste is still identical
More findings: manual and startup capture

Manual still promises image history

manual/08-unified-clipboard-history.md line 18 ("works for both text and images") and manual/03-coming-from-mac-or-windows.md line 27 ("it holds images as well as text") are unchanged. Impact: users are told new image copies appear in history when they no longer do.

Suggested change: Update both lines if image history is removed.

Image capture is only partly removed

The shell-start snapshot (currentProc, which runs capture.sh with no arguments) still tries image types first (capture.sh lines 97-102). When the clipboard holds an image, each shell start therefore still reads and stores it, as the last line of the first reproducer shows. Impact: image capture continues in one place, and the image/* branch of capture.sh and its tests now cover a path no watcher invokes.

Suggested change: Either keep image capture or remove it from the startup snapshot too, so the behaviour is consistent.

Details

Overlap with open PRs: #11920 conflicts in watchRestartTimer and adds more imageWatchProc handling (a merge leaves references to the deleted process). #7452 (OCR and an images-only view) depends on image capture. #9488 bounds the stalled-callback case and merges cleanly with this PR.

Hypothesis (simulated, not the reporter's confirmed cause): wl-paste --watch waits for each callback, and capture.sh runs an unbounded wl-paste --list-types (line 13) before reading its stdin. wl-copy serves one read at a time. If a callback stalls before draining a payload above the 64 KiB pipe buffer, other pastes wait until it drains (in the run below, nothing arrived within 3 s) and a replaced wl-copy lingers. This affects large text as well as images (issue #9443 reports a stuck capture.sh text).

This is not a reproduction of the reporter's hang. It shows a mechanism that produces the symptoms the PR describes ("leaves the user's paste target with nothing and results in stuck wl-copy processes").

wl-paste --watch hands the payload to capture.sh through a pipe. capture.sh runs wl-paste --list-types (line 13) before it reads that pipe. wl-copy serves one read at a time and waits until each reader has drained it (do_send, src/types/copy-action.c, lines 89-123, wl-clipboard 2.3.0). In this script a wrapper makes that --list-types call sleep 6 s, as a stand-in for a callback that stops before draining its pipe (issue #9443 reports a capture.sh text callback stuck for two days).

With a 3 MiB payload, a paste during the stall gets nothing within its 3 s timeout, and the old wl-copy keeps running after a new copy replaces it until the stall ends. This happens with the text watcher the PR keeps, not only with the image watcher. A 16 KiB payload fits in the 64 KiB pipe buffer and is not affected.

Run: put relay.c (from the first reproducer) and repro-stall.sh in one directory, then OMARCHY=/path/to/omarchy-checkout bash repro-stall.sh WATCHER-TYPE MIME BYTES.

repro-stall.sh

#!/bin/bash
# What happens to wl-copy when a watcher callback stops before reading its pipe?
# Simulation: a wrapper makes `wl-paste --list-types` (capture.sh runs it before
# reading stdin) sleep 6 s. Usage: OMARCHY=... bash repro-stall.sh WATCHER-TYPE MIME BYTES
#   e.g. image/png image/png 3145728   (watcher removed by the PR)
#        text text/plain 3145728        (watcher kept by the PR)
#        text text/plain 16384
set -u
watch_type=$1 mime=$2 bytes=$3
here=$(cd "$(dirname "$0")" && pwd)
work=$(mktemp -d)
pids=()
trap 'kill "${pids[@]}" 2>/dev/null; wait 2>/dev/null; rm -rf "$work"' EXIT
export XDG_RUNTIME_DIR=$work HOME=$work/home XDG_STATE_HOME=$work/state
mkdir -p "$HOME" "$XDG_STATE_HOME" "$work/bin"

xml=$(pkg-config --variable=pkgdatadir wayland-protocols)/staging/ext-data-control/ext-data-control-v1.xml
wayland-scanner server-header "$xml" "$work/ext-data-control-v1-server-protocol.h"
wayland-scanner private-code "$xml" "$work/ext-data-control-v1-protocol.c"
gcc -I"$work" -o "$work/relay" "$here/relay.c" "$work/ext-data-control-v1-protocol.c" -lwayland-server || exit 1
"$work/relay" >"$work/display" 2>/dev/null &
pids+=($!)
until [[ -s $work/display ]]; do sleep 0.05; done
export WAYLAND_DISPLAY=$(<"$work/display")

real=$(command -v wl-paste)
cat >"$work/bin/wl-paste" <<SH
#!/bin/bash
[[ \${1:-} == --list-types && -e $work/stall ]] && sleep 6
exec $real "\$@"
SH
chmod +x "$work/bin/wl-paste"

capture=$OMARCHY/shell/plugins/clipboard/capture.sh
PATH="$work/bin:$PATH" setpriv --pdeathsig TERM wl-paste --type "$watch_type" --watch "$capture" "$watch_type" >"$work/watcher.out" &
pids+=($!)
sleep 1

if [[ $mime == image/* ]]; then head -c "$bytes" /dev/urandom >"$work/payload"; else head -c "$bytes" /dev/zero | tr '\0' a >"$work/payload"; fi
touch "$work/stall"
wl-copy --foreground --type "$mime" <"$work/payload" &
old=$!
pids+=($old)
sleep 0.7

start=$(date +%s%3N)
timeout 3 "$real" --no-newline --type "$mime" | cmp -s - "$work/payload" &&
  echo "paste while the callback is stalled: identical after $(($(date +%s%3N) - start)) ms" ||
  echo "paste while the callback is stalled: nothing after $(($(date +%s%3N) - start)) ms (timed out)"

printf 'next copy' | wl-copy --foreground --type text/plain &
pids+=($!)
start=$(date +%s%3N)
while kill -0 "$old" 2>/dev/null; do sleep 0.05; done
echo "previous wl-copy exited $(($(date +%s%3N) - start)) ms after a new copy replaced it"

Actual output (PR head 03a253c, wl-clipboard 2.3.0)

bash repro-stall.sh image/png image/png 3145728 (the image watcher the PR removes):

paste while the callback is stalled: nothing after 3002 ms (timed out)
previous wl-copy exited 2358 ms after a new copy replaced it

bash repro-stall.sh text text/plain 3145728 (the text watcher the PR keeps):

paste while the callback is stalled: nothing after 3003 ms (timed out)
previous wl-copy exited 2349 ms after a new copy replaced it

bash repro-stall.sh text text/plain 16384 (payload below the pipe buffer):

paste while the callback is stalled: identical after 3 ms
previous wl-copy exited 53 ms after a new copy replaced it

Verified:

  • imageWatchProc and both of its start sites are removed, and no reference remains. The existing pkill pattern still stops an image watcher left by an older shell.
  • test/shell.d/clipboard-test.sh passes (73 ok) at the base, at the head, and at the head merged onto current quattro. The new assertion fails against base code.
  • Existing stored image entries still display, paste and open.

Optional test improvement: The new substring check misses other spellings such as -t image/png. The restart-count check catches such a watcher only if it uses watchRestartTimer; one without a restart handler passes all 73 checks. Matching any --watch command whose type is not text would guard the removal.


Review information

Test scope: Source and sandbox runs of wl-clipboard 2.3.0 on a minimal Wayland relay at 03a253c2; not run in Hyprland, Quickshell or a browser.

AI process: Opus 5.5 Medium coordination and synthesis, Opus 5.5 Xhigh technical review and final fact check, GPT 6 Sol Xhigh search for related issues, Opus 5.5 Medium editorial check.

Opt out: To stop receiving these reviews, reply to this comment saying so.

@omarchybot

Copy link
Copy Markdown
Collaborator

Reviewed at 03a253c. I could not reproduce the problem this fixes, and the change removes image clipboard history, so it is not ready.

What was run. On a disposable Omarchy worker (Hyprland 0.56.2, wl-clipboard 2.3.0), in its live session:

  • Base, with the shell's text and image/png watchers both running: three rounds of copying two different ~3 MB PNGs with wl-copy --type image/png, five wl-paste --type image/png reads after each copy. All 30 pastes were byte-identical. Only one wl-copy was left running, the current owner, and the watcher recorded both images.
  • Base, with a GTK4 app owning an image/png selection: 5 of 5 pastes returned the image, and the watcher recorded it.
  • This head, with the shell linked to the branch so only the text watcher ran: the same pastes gave the same results. The only difference was that no new image reached history.
  • At the head, ./test/cli passed (112 ok) and test/shell.d/clipboard-test.sh passed (73).

Why the premise does not hold. A Wayland selection is not a pipe one reader can drain. Each paste sends its own receive with its own fd, the compositor forwards it to the source as a separate send, and wl-copy buffers its input and serves every request from the start. A watcher reading the image does not take it away from a later paste. The exceptions are sources that choose to serve once, such as wl-copy --paste-once, which nothing in Omarchy uses. Chromium and Brave were not tested here.

What the change costs. New image copies stop reaching history, while manual/08-unified-clipboard-history.md:18 still says history covers images. Image capture is also only partly removed: the startup snapshot (currentProc, capture.sh:97-100) still reads and stores an image when the shell starts. Three blank lines are left where the process was deleted (Clipboard.qml:290-292).

A real hang has a narrower fix. If you have seen a paste hang and wl-copy processes pile up, the likely cause is a stalled capture.sh callback. It runs wl-paste --list-types with no timeout before draining its input, and while it holds its pipe undrained, wl-copy cannot serve anyone else. That would hit large text copies through the watcher this PR keeps, too (#9443). #9488 bounds those reads and keeps image history. If you can catch a hang, ps -eo pid,ppid,etime,args | grep -E 'wl-(copy|paste)|capture.sh' taken during it would show which process is stuck.

Second opinion. Codex Medium reviewed the diff and the reasoning separately and reached the same conclusion. It added the --paste-once exception, the startup snapshot still reading images, and that the watcher restart timer only catches a watcher that has exited, not one stuck waiting on its callback. Its independence is not guaranteed: it can read this review's working notes.

Waiting on the author: a reproduction or process listing from an actual hang. Whether to drop image history at all is the maintainer's call.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants