public inbox for cygwin-cvs@sourceware.org
help / color / mirror / Atom feed
From: Ken Brown <kbrown@sourceware.org>
To: cygwin-cvs@sourceware.org
Subject: [newlib-cygwin] Cygwin: FIFO: fix timing issue with owner change
Date: Tue,  4 Aug 2020 15:29:21 +0000 (GMT)	[thread overview]
Message-ID: <20200804152921.A52A1385041D@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=6acce025d07aa9328968351e1b718c0974e8780d

commit 6acce025d07aa9328968351e1b718c0974e8780d
Author: Ken Brown <kbrown@cornell.edu>
Date:   Sun Aug 2 16:38:24 2020 -0400

    Cygwin: FIFO: fix timing issue with owner change
    
    fhandler_fifo::take_ownership() tacitly assumes that the current
    owner's fifo_reader_thread will be woken up from WFMO when
    update_needed_evt is signaled.  But it's possible that the the current
    owner's fifo_reader_thread is at the beginning of its main loop rather
    than in its WFMO call when that event is signaled.
    
    In this case the owner will never see that the event has been
    signaled, and it will never update the shared fifo_client_handlers.
    The reader that wants to take ownership will then spin its wheels
    forever.
    
    Fix this by having the current owner call update_shared_handlers at
    the beginning of its loop, if necessary.

Diff:
---
 winsup/cygwin/fhandler_fifo.cc | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)

diff --git a/winsup/cygwin/fhandler_fifo.cc b/winsup/cygwin/fhandler_fifo.cc
index ee7f47c0c..7b87aab00 100644
--- a/winsup/cygwin/fhandler_fifo.cc
+++ b/winsup/cygwin/fhandler_fifo.cc
@@ -472,17 +472,34 @@ fhandler_fifo::fifo_reader_thread_func ()
 
       if (pending_owner)
 	{
-	  if (pending_owner != me)
+	  if (pending_owner == me)
+	    take_ownership = true;
+	  else if (cur_owner != me)
 	    idle = true;
 	  else
-	    take_ownership = true;
+	    {
+	      /* I'm the owner but someone else wants to be.  Have I
+		 already seen and reacted to update_needed_evt? */
+	      if (WaitForSingleObject (update_needed_evt, 0) == WAIT_OBJECT_0)
+		{
+		  /* No, I haven't. */
+		  fifo_client_lock ();
+		  if (update_shared_handlers () < 0)
+		    api_fatal ("Can't update shared handlers, %E");
+		  fifo_client_unlock ();
+		}
+	      owner_unlock ();
+	      /* Yield to pending owner. */
+	      Sleep (1);
+	      continue;
+	    }
 	}
       else if (!cur_owner)
 	take_ownership = true;
       else if (cur_owner != me)
 	idle = true;
       else
-	/* I'm the owner. */
+	/* I'm the owner and there's no pending owner. */
 	goto owner_listen;
       if (idle)
 	{
@@ -1212,7 +1229,7 @@ fhandler_fifo::take_ownership ()
   /* Wake up my fifo_reader_thread. */
   owner_needed ();
   if (get_owner ())
-    /* Wake up owner's fifo_reader_thread. */
+    /* Wake up the owner and request an update of the shared fc_handlers. */
     SetEvent (update_needed_evt);
   owner_unlock ();
   /* The reader threads should now do the transfer. */


                 reply	other threads:[~2020-08-04 15:29 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200804152921.A52A1385041D@sourceware.org \
    --to=kbrown@sourceware.org \
    --cc=cygwin-cvs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).