Index: trunk/src/VBox/GuestHost/SharedClipboard/clipboard-x11.cpp
===================================================================
--- trunk/src/VBox/GuestHost/SharedClipboard/clipboard-x11.cpp	(revision 99249)
+++ trunk/src/VBox/GuestHost/SharedClipboard/clipboard-x11.cpp	(revision 99250)
@@ -1689,11 +1689,9 @@
                  pCtx->vboxFormats, idxFmtX11, g_aFormats[idxFmtX11].pcszAtom, fmtX11));
 
-#ifdef LOG_ENABLED
     char *pszFmts = ShClFormatsToStrA(pCtx->vboxFormats);
     AssertPtrReturn(pszFmts, VERR_NO_MEMORY);
     LogRel2(("Shared Clipboard: Converting VBox formats '%s' to '%s' for X11\n",
-             pszFmts, g_aFormats[idxFmtX11].pcszAtom));
+             pszFmts, fmtX11 == SHCLX11FMT_INVALID ? "<invalid>" : g_aFormats[idxFmtX11].pcszAtom));
     RTStrFree(pszFmts);
-#endif
 
     void    *pv = NULL;
@@ -2007,5 +2005,5 @@
     AssertPtrReturnVoid(pReq);
 
-    LogFlowFunc(("pReq->uFmtVBox=%#x, pReq->idxFmtX11=%u, pReq->pCtx=%p\n", pReq->uFmtVBox, pReq->idxFmtX11, pReq->pCtx));
+    LogFlowFunc(("uFmtVBox=%#x, idxFmtX11=%u, pvSrc=%p, cbSrc=%u\n", pReq->uFmtVBox, pReq->idxFmtX11, pvSrc, cbSrc));
 
     LogRel2(("Shared Clipboard: Converting X11 format '%s' to VBox format %#x\n",
@@ -2253,14 +2251,30 @@
 {
     RT_NOREF(widget);
+
+    int rc = VINF_SUCCESS;
+
+    CLIPREADX11CBREQ *pReq = (CLIPREADX11CBREQ *)pClient;
+    if (pReq) /* Give some more clues, if available. */
+    {
+        char *pszFmts = ShClFormatsToStrA(pReq->uFmtVBox);
+        AssertPtrReturnVoid(pszFmts);
+        AssertReturnVoid(pReq->idxFmtX11 <= SHCL_MAX_X11_FORMATS); /* Paranoia, should be checked already by the caller. */
+        LogRel2(("Shared Clipboard: Converting X11 format '%s' -> VBox format(s) '%s'\n", g_aFormats[pReq->idxFmtX11].pcszAtom, pszFmts));
+        RTStrFree(pszFmts);
+    }
+
     if (*atomType == XT_CONVERT_FAIL) /* Xt timeout */
-        clipConvertDataFromX11Worker(pClient, NULL, 0);
+    {
+        LogRel(("Shared Clipboard: Reading clipboard data from X11 timed out\n"));
+        rc = VERR_TIMEOUT;
+    }
     else
     {
-        CLIPREADX11CBREQ *pReq = (CLIPREADX11CBREQ *)pClient;
-        if (pReq->pCtx->Callbacks.pfnOnClipboardRead)
+        if (   pReq
+            && pReq->pCtx->Callbacks.pfnOnClipboardRead)
         {
             void  *pvData = NULL;
             size_t cbData = 0;
-            int rc = pReq->pCtx->Callbacks.pfnOnClipboardRead(pReq->pCtx->pFrontend, pReq->uFmtVBox, &pvData, &cbData, NULL);
+            rc = pReq->pCtx->Callbacks.pfnOnClipboardRead(pReq->pCtx->pFrontend, pReq->uFmtVBox, &pvData, &cbData, NULL);
             if (RT_SUCCESS(rc))
             {
@@ -2269,6 +2283,4 @@
                 RTMemFree(pvData);
             }
-            else
-                clipConvertDataFromX11Worker(pClient, NULL, 0);
         }
         else /* Call with current data provided by X (default). */
@@ -2276,12 +2288,35 @@
     }
 
+    if (RT_FAILURE(rc))
+    {
+        LogRel(("Shared Clipboard: Reading clipboard data from X11 failed with %Rrc\n", rc));
+        /* Make sure to complete the request in any case. */
+        clipConvertDataFromX11Worker(pClient, NULL, 0);
+    }
+
     XtFree((char *)pvSrc);
 }
 
-static int clipGetSelectionValue(PSHCLX11CTX pCtx, SHCLX11FMTIDX idxFmt,
-                                 CLIPREADX11CBREQ *pReq)
-{
+/**
+ * Requests the current clipboard data from a specific selection.
+ *
+ * @returns VBox status code.
+ * @param   pCtx                The X11 clipboard context to use.
+ * @param   pszWhere            Clipboard selection to request the data from.
+ * @param   idxFmt              The X11 format to request the data in.
+ * @param   pReq                Where to store the requested data on success.
+ */
+static int clipGetSelectionValueEx(PSHCLX11CTX pCtx, const char *pszWhere, SHCLX11FMTIDX idxFmt,
+                                   CLIPREADX11CBREQ *pReq)
+{
+    AssertPtrReturn(pszWhere, VERR_INVALID_POINTER);
+    AssertReturn(idxFmt <= SHCL_MAX_X11_FORMATS, VERR_INVALID_PARAMETER);
+    AssertReturn(clipIsSupportedSelectionType(pCtx, clipGetAtom(pCtx, pszWhere)), VERR_INVALID_PARAMETER);
+    AssertPtrReturn(pReq, VERR_INVALID_POINTER);
+
+    LogRel2(("Shared Clipboard: Requesting X11 selection value in %s for format '%s'\n", pszWhere, g_aFormats[idxFmt].pcszAtom));
+
 #ifndef TESTCASE
-    XtGetSelectionValue(pCtx->pWidget, clipGetAtom(pCtx, "CLIPBOARD"),
+    XtGetSelectionValue(pCtx->pWidget, clipGetAtom(pCtx, pszWhere),
                         clipAtomForX11Format(pCtx, idxFmt),
                         clipConvertDataFromX11,
@@ -2293,4 +2328,19 @@
 
     return VINF_SUCCESS; /** @todo Return real rc. */
+}
+
+/**
+ * Requests the current clipboard data from the CLIPBOARD selection.
+ *
+ * @returns VBox status code.
+ * @param   pCtx                The X11 clipboard context to use.
+ * @param   idxFmt              The X11 format to request the data in.
+ * @param   pReq                Where to store the requested data on success.
+ *
+ * @sa clipGetSelectionValueEx() for requesting data for a specific selection.
+ */
+static int clipGetSelectionValue(PSHCLX11CTX pCtx, SHCLX11FMTIDX idxFmt, CLIPREADX11CBREQ *pReq)
+{
+    return clipGetSelectionValueEx(pCtx, "CLIPBOARD", idxFmt, pReq);
 }
 
