public inbox for cygwin-patches@cygwin.com
 help / color / mirror / Atom feed
From: Takashi Yano <takashi.yano@nifty.ne.jp>
To: cygwin-patches@cygwin.com
Subject: [PATCH 1/2] Cygwin: console: Fix race issue regarding cons_master_thread().
Date: Mon, 19 Apr 2021 19:30:45 +0900	[thread overview]
Message-ID: <20210419103046.21838-2-takashi.yano@nifty.ne.jp> (raw)
In-Reply-To: <20210419103046.21838-1-takashi.yano@nifty.ne.jp>

- With this patch, the race issue regarding starting/stopping
  cons_master_thread() introduced by commit ff4440fc is fixed.

  Addresses:
    https://cygwin.com/pipermail/cygwin/2021-April/248292.html
---
 winsup/cygwin/fhandler_console.cc | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/winsup/cygwin/fhandler_console.cc b/winsup/cygwin/fhandler_console.cc
index 0b33a1370..e418aac97 100644
--- a/winsup/cygwin/fhandler_console.cc
+++ b/winsup/cygwin/fhandler_console.cc
@@ -48,6 +48,7 @@ details. */
 #define con_is_legacy (shared_console_info && con.is_legacy)
 
 #define CONS_THREAD_SYNC "cygcons.thread_sync"
+static bool NO_COPY master_thread_started = false;
 
 const unsigned fhandler_console::MAX_WRITE_CHARS = 16384;
 
@@ -184,6 +185,7 @@ cons_master_thread (VOID *arg)
 		   GetCurrentProcess (), &thread_sync_event,
 		   0, FALSE, DUPLICATE_SAME_ACCESS);
   SetEvent (thread_sync_event);
+  master_thread_started = true;
   /* Do not touch class members after here because the class instance
      may have been destroyed. */
   fhandler_console::cons_master_thread (&handle_set, ttyp);
@@ -370,6 +372,8 @@ fhandler_console::set_unit ()
     }
   if (!created && shared_console_info)
     {
+      while (con.owner > MAX_PID)
+	Sleep (1);
       pinfo p (con.owner);
       if (!p)
 	con.owner = myself->pid;
@@ -1393,14 +1397,16 @@ fhandler_console::close ()
 
   release_output_mutex ();
 
-  if (shared_console_info && con.owner == myself->pid)
+  if (shared_console_info && con.owner == myself->pid
+      && master_thread_started)
     {
       char name[MAX_PATH];
       shared_name (name, CONS_THREAD_SYNC, get_minor ());
       thread_sync_event = OpenEvent (MAXIMUM_ALLOWED, FALSE, name);
-      con.owner = 0;
+      con.owner = MAX_PID + 1;
       WaitForSingleObject (thread_sync_event, INFINITE);
       CloseHandle (thread_sync_event);
+      con.owner = 0;
     }
 
   CloseHandle (input_mutex);
-- 
2.31.1


  reply	other threads:[~2021-04-19 10:31 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-19 10:30 [PATCH 0/2] Fix race issues Takashi Yano
2021-04-19 10:30 ` Takashi Yano [this message]
2021-04-19 10:30 ` [PATCH 2/2] Cygwin: pty: Fix race issue in inheritance of pseudo console Takashi Yano
2021-04-20  8:47 ` [PATCH 0/2] Fix race issues Corinna Vinschen
2021-04-20 12:35   ` David Allsopp

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=20210419103046.21838-2-takashi.yano@nifty.ne.jp \
    --to=takashi.yano@nifty.ne.jp \
    --cc=cygwin-patches@cygwin.com \
    /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).