public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* Re: [PATCH] Cygwin: pthread: Fix a race issue introduced by the commit 2c5433e5da82
       [not found] <20240529103020.53514-1-takashi.yano@nifty.ne.jp>
@ 2024-05-29 11:19 ` Bruno Haible
  0 siblings, 0 replies; only message in thread
From: Bruno Haible @ 2024-05-29 11:19 UTC (permalink / raw)
  To: Takashi Yano, cygwin

Takashi Yano wrote in cygwin-patches:
> To avoid race issues, pthread::once() uses pthread_mutex. This caused
> the handle leak which was fixed by the commit 2c5433e5da82. However,
> this fix introduced another race issue, i.e., the mutex may be used
> after it is destroyed. With this patch, do not use pthread_mutex in
> pthread::once() to avoid both issues. Instead, InterlockedExchage()
> is used.

This patch is bogus as well, because it allows one thread to return
from a pthread_once call while the other thread is currently
executing the init_routine and not yet done with it.

> +  if (!InterlockedExchange (&once_control->state, 1))
> +    init_routine ();
> return 0;
> }

There is no code after the init_routine () call here. This means
that other threads are not notified when the init_routine () call
is complete. Therefore this implementation *cannot* be correct.

See: Assume thread1 and thread2 call pthread_once on the same
once_control.

            thread1                      thread2
            -------                      -------

         enters pthread_once       enters pthread_once

         sets state to 1

                                   sees that state == 1

                                   returns from pthread_once

                                   executes code that assumes
                                   init_routine has completed

         starts executing
         init_routine

         finished executing
         init_routine

         returns from pthread_once

Bruno




^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2024-05-29 11:20 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <20240529103020.53514-1-takashi.yano@nifty.ne.jp>
2024-05-29 11:19 ` [PATCH] Cygwin: pthread: Fix a race issue introduced by the commit 2c5433e5da82 Bruno Haible

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).