wayland: clipboard set before the first input serial is lost (3.4.x) or applied late over another client's copy
SDL version: 3.4.2 (Ubuntu 26.04) and 3.4.16; the second problem below is also present on main.
Platform: GNOME 50 / mutter, Wayland backend.
Found with: the FreeRDP SDL3 client (sdl-freerdp), which sets the clipboard whenever the remote side announces one — including once at connect, before the window has seen any input. FreeRDP-side fixes are in FreeRDP/FreeRDP#13560.
1. A clipboard set before the first input serial is never sent, but SDL still treats it as owned (3.4.x)
When SDL_SetClipboardData runs while data_device->selection_serial is still 0, Wayland_data_device_set_selection skips wl_data_device_set_selection (the "set it later" path) but still stores the source as selection_source. The later path in Wayland_data_device_set_serial assigns the new serial before testing selection_serial == 0, so the test can never be true and the pending selection is never sent.
Result: the compositor never gets the selection, but SDL keeps a stale selection_source. Wayland_HasClipboardData / Wayland_GetClipboardData then answer from SDL's own internal data instead of the real selection offer, so the application reads empty data for what another client copied (for FreeRDP: pasting local text into the remote session comes back blank until the next remote copy).
On main this is already fixed: Wayland_DataDeviceSetSerial now tests for the pending selection before assigning the serial (it arrived as part of 33a1b566e "wayland: Cleanup clipboard code"). Could that be backported to release-3.4.x? It is the ordering change in the data-device (and primary-selection) set-serial functions only.
2. The pending selection is applied later even after another client took the clipboard (3.4.x with the backport, and main)
With (1) fixed, the pending source is set with whatever serial arrives first — possibly long after, and after other clients have changed the selection. Repro with two FreeRDP sessions A and B on one desktop:
- Connect A, then B. Each server announces its clipboard at connect, so each client calls
SDL_SetClipboardDatabefore its window has had any input: both selections are pending. - Copy something in A (A is focused, the selection is set normally).
- Click into B. B's first input serial arrives and B's stale pending selection from connect time is sent now, replacing what was copied in A.
The pending selection is obsolete as soon as the compositor reports a selection on the data device, because the clipboard has moved on without it. Suggested behaviour: in the data device's selection handler, if a source is still pending (serial 0), destroy it the same way a cancelled source is destroyed, so it cancels the application's clipboard data. We tested that approach locally on 3.4.16 and it resolves the scenario above without affecting normal copies.
The analysis was done with AI assistance (Claude); I've described the fix rather than attaching code because of the contribution policy. Happy to test a fix.
Source: libsdl-org/SDL