Re: [PATCH] Cygwin: clipboard: Add workaround for ERROR_CLIPBOARD_NOT_OPEN

Mark Geisert <[email protected]> Fri, 12 Jun 2026 16:08:24 -0700
Newsgroups gmane.os.cygwin.patches
Message-ID <[email protected]>
Hi Takashi,

On 6/8/2026 5:20 PM, Takashi Yano wrote:
> SetClipboard/Data() 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 <[email protected]>
> Reviewed-by: Mark Geisert <[email protected]>

Sorry, I didn't read ^^^ as a request to review, and then forgot to ask 
about it...

> ---
>   winsup/cygwin/fhandler/clipboard.cc | 14 ++++++++++++--
>   1 file changed, 12 insertions(+), 2 deletions(-)
> 
> diff --git a/winsup/cygwin/fhandler/clipboard.cc b/winsup/cygwin/fhandler/clipboard.cc
> index 12691c7c1..db33d839f 100644
> --- a/winsup/cygwin/fhandler/clipboard.cc
> +++ b/winsup/cygwin/fhandler/clipboard.cc
> @@ -25,11 +25,21 @@ 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;
> +	{
> +	  /* SetClipboard/Data() 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)
> +	      || GetLastError () != ERROR_CLIPBOARD_NOT_OPEN)
> +	    return true;

I don't think this 'if' is quite right.  If GetClipboardData(...) 
succeeds, return true.  Otherwise, if GetLastError() returns 
ERROR_CLIPBOARD_NOT_OPEN, continue the loop.  Otherwise either break the 
loop or return false right there.

Do you agree with my reasoning?  I'm open to corrections.
Regards,

..mark