Cygwin: clipboard: Add workaround for ERROR_CLIPBOARD_NOT_OPEN

SetClipboardData() and GetClipboardData() occasionally fail with
ERROR_CLIPBOARD_NOT_OPEN, even though OpenClipboard() succeeded if
NULL HWND is used. Retry until GetClipboardData() does not return
ERROR_CLIPBOARD_NOT_OPEN.

Addresses: https://cygwin.com/pipermail/cygwin/2026-February/259438.html
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
Reviewed-by: Mark Geisert <mark@maxrnd.com>
This commit is contained in:
Takashi Yano committed 2026-06-13 16:26:13 +09:00
1 parent 950abe2600
commit 7fd670a36b
2 files changed
+22 -2

No files matched your search

+19 -2
View File
@@ -25,11 +25,28 @@ details. */
static inline bool
open_clipboard ()
{
const int max_retry = 10;
const int max_retry = 20;
for (int i = 0; i < max_retry; i++)
{
/* No appropriate HWND exists here. */
if (OpenClipboard (NULL))
return true;
{
/* SetClipboardData() and GetClipboardData() occasionally
fail with ERROR_CLIPBOARD_NOT_OPEN, even though
OpenClipboard() succeeded if NULL HWND is used.
Retry until GetClipboardData() does not return
ERROR_CLIPBOARD_NOT_OPEN. */
if (GetClipboardData (CF_UNICODETEXT))
return true;
DWORD err = GetLastError ();
/* Here, ERROR_NOT_FOUND means the clipboard does not contains
valid CF_UNICODETEXT. OpenClipboard() must have succeeded. */
if (err == ERROR_NOT_FOUND)
return true;
CloseClipboard ();
if (err != ERROR_CLIPBOARD_NOT_OPEN)
return false;
}
Sleep (1);
}
return false;
+3
View File
@@ -8,3 +8,6 @@ Fixes:
- Fix behaviour with OPEN_MAX files open
Addresses: https://cygwin.com/pipermail/cygwin/2026-May/259664.html
- Add workaround for clipboard access error.
Addresses: https://cygwin.com/pipermail/cygwin/2026-February/259438.html