[spice-gtk,2/3] clipboard: release on both sides if race occurs

Submitted by Jakub Janku on Feb. 28, 2019, 7:12 p.m.

Details

Message ID 20190228191244.19464-3-jjanku@redhat.com
State New
Headers show
Series "clipboard grab race" ( rev: 2 1 ) in Spice

Not browsing as part of any series.

Commit Message

Jakub Janku Feb. 28, 2019, 7:12 p.m.
If spice-vdagent requests clipboard data while
it has previously advertised that it itself can provide it,
acknowledge that it's likely a race condition and not a
programming error.

In this case, release clipboard in spice-gtk as well as vdagent
to prevent other apps from requesting data from us when
we cannot provide it.

Current implementation can cause hangs, as has been reported:
https://gitlab.freedesktop.org/spice/win32/vd_agent/issues/6

Related: https://gitlab.freedesktop.org/spice/spice-gtk/issues/82
Related: https://bugzilla.redhat.com/show_bug.cgi?id=1594876

Signed-off-by: Jakub Janků <jjanku@redhat.com>
---
 src/spice-gtk-session.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

Patch hide | download patch | download mbox

diff --git a/src/spice-gtk-session.c b/src/spice-gtk-session.c
index 7b7370c..018cb4b 100644
--- a/src/spice-gtk-session.c
+++ b/src/spice-gtk-session.c
@@ -1037,7 +1037,18 @@  static gboolean clipboard_request(SpiceMainChannel *main, guint selection,
 
     cb = get_clipboard_from_selection(s, selection);
     g_return_val_if_fail(cb != NULL, FALSE);
-    g_return_val_if_fail(s->clipboard_by_guest[selection] == FALSE, FALSE);
+    if (s->clipboard_by_guest[selection]) {
+        g_warning("%s: 's->clipboard_by_guest[%u] == FALSE' failed, "
+                  "likely a race, releasing clipboard on both sides",
+                  __FUNCTION__, selection);
+
+        spice_main_channel_clipboard_selection_notify(s->main, selection, type, NULL, 0);
+        spice_main_channel_clipboard_selection_release(s->main, selection);
+        gtk_clipboard_clear(cb);
+        s->nclip_targets[selection] = 0;
+        s->clipboard_by_guest[selection] = FALSE;
+        return FALSE;
+    }
     g_return_val_if_fail(s->clip_grabbed[selection], FALSE);
 
     if (read_only(self))