x11: Fix race conditions in XSetSelection

Some clients like X11 LibreOffice clear the selection prior to copying
some content there. This selection clear is correctly seen by our clipboard
manager as a hint to take ownership and preserve the last copied content,
all while LO is issuing other XSetSelection with the new clipboard content.

Our use of META_CURRENT_TIME turns this into a race condition, as there's
both LO and our clipboard manager trying to do XSetSelection(), from our
side it's all up to the order in which the requests arrive to the X server.

In order to break the tie, keep the selection timestamp from the XFixes
event (i.e. the timestamp set by the XSetSelection external call that is
unsetting the clipboard) and ensure it is used for our own XSetSelection
call replacing the selection. In this same situation, it will make the
X server deem the request too old, and let LO win.

If the compositor-side XSetSelection event does not happen in result to
a XFixes selection notify event, the current event time will be used, and
META_CURRENT_TIME as a last resort.

Closes: https://gitlab.gnome.org/GNOME/mutter/-/issues/1113

https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/1205
This commit is contained in:
Carlos Garnacho 2020-04-17 14:15:54 +02:00
parent f988a9caaa
commit 2cf9249d2b
2 changed files with 10 additions and 1 deletions

View File

@ -127,6 +127,7 @@ struct _MetaX11Display
struct { struct {
Window xwindow; Window xwindow;
uint32_t ownership_timestamp;
MetaSelectionSource *owners[META_N_SELECTION_TYPES]; MetaSelectionSource *owners[META_N_SELECTION_TYPES];
GCancellable *cancellables[META_N_SELECTION_TYPES]; GCancellable *cancellables[META_N_SELECTION_TYPES];

View File

@ -322,6 +322,7 @@ meta_x11_selection_handle_xfixes_selection_notify (MetaX11Display *x11_display,
if (!atom_to_selection_type (xdisplay, event->selection, &selection_type)) if (!atom_to_selection_type (xdisplay, event->selection, &selection_type))
return FALSE; return FALSE;
x11_display->selection.ownership_timestamp = event->selection_timestamp;
selection = meta_display_get_selection (meta_get_display ()); selection = meta_display_get_selection (meta_get_display ());
if (x11_display->selection.cancellables[selection_type]) if (x11_display->selection.cancellables[selection_type])
@ -360,6 +361,8 @@ meta_x11_selection_handle_xfixes_selection_notify (MetaX11Display *x11_display,
data); data);
} }
x11_display->selection.ownership_timestamp = 0;
return TRUE; return TRUE;
} }
@ -381,6 +384,7 @@ notify_selection_owner (MetaX11Display *x11_display,
MetaSelectionSource *new_owner) MetaSelectionSource *new_owner)
{ {
Display *xdisplay = x11_display->xdisplay; Display *xdisplay = x11_display->xdisplay;
uint32_t timestamp;
if (new_owner && !META_IS_SELECTION_SOURCE_X11 (new_owner)) if (new_owner && !META_IS_SELECTION_SOURCE_X11 (new_owner))
{ {
@ -390,13 +394,17 @@ notify_selection_owner (MetaX11Display *x11_display,
g_clear_object (&x11_display->selection.cancellables[selection_type]); g_clear_object (&x11_display->selection.cancellables[selection_type]);
} }
timestamp = x11_display->selection.ownership_timestamp ?
x11_display->selection.ownership_timestamp :
meta_display_get_current_time (x11_display->display);
/* If the owner is non-X11, claim the selection on our selection /* If the owner is non-X11, claim the selection on our selection
* window, so X11 apps can interface with it. * window, so X11 apps can interface with it.
*/ */
XSetSelectionOwner (xdisplay, XSetSelectionOwner (xdisplay,
selection_to_atom (selection_type, xdisplay), selection_to_atom (selection_type, xdisplay),
x11_display->selection.xwindow, x11_display->selection.xwindow,
META_CURRENT_TIME); timestamp);
} }
} }