Skip to content

Commit 7fd670a

Browse files
committed
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>
1 parent 950abe2 commit 7fd670a

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

winsup/cygwin/fhandler/clipboard.cc

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,28 @@ details. */
2525
static inline bool
2626
open_clipboard ()
2727
{
28-
const int max_retry = 10;
28+
const int max_retry = 20;
2929
for (int i = 0; i < max_retry; i++)
3030
{
31+
/* No appropriate HWND exists here. */
3132
if (OpenClipboard (NULL))
32-
return true;
33+
{
34+
/* SetClipboardData() and GetClipboardData() occasionally
35+
fail with ERROR_CLIPBOARD_NOT_OPEN, even though
36+
OpenClipboard() succeeded if NULL HWND is used.
37+
Retry until GetClipboardData() does not return
38+
ERROR_CLIPBOARD_NOT_OPEN. */
39+
if (GetClipboardData (CF_UNICODETEXT))
40+
return true;
41+
DWORD err = GetLastError ();
42+
/* Here, ERROR_NOT_FOUND means the clipboard does not contains
43+
valid CF_UNICODETEXT. OpenClipboard() must have succeeded. */
44+
if (err == ERROR_NOT_FOUND)
45+
return true;
46+
CloseClipboard ();
47+
if (err != ERROR_CLIPBOARD_NOT_OPEN)
48+
return false;
49+
}
3350
Sleep (1);
3451
}
3552
return false;

winsup/cygwin/release/3.6.10

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ Fixes:
88

99
- Fix behaviour with OPEN_MAX files open
1010
Addresses: https://cygwin.com/pipermail/cygwin/2026-May/259664.html
11+
12+
- Add workaround for clipboard access error.
13+
Addresses: https://cygwin.com/pipermail/cygwin/2026-February/259438.html

0 commit comments

Comments
 (0)