From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2155) id ABE4A3858C27; Tue, 20 Apr 2021 08:46:56 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org ABE4A3858C27 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Corinna Vinschen To: cygwin-cvs@sourceware.org Subject: [newlib-cygwin] Cygwin: console: Fix race issue regarding cons_master_thread(). X-Act-Checkin: newlib-cygwin X-Git-Author: Takashi Yano X-Git-Refname: refs/heads/master X-Git-Oldrev: 9c6c2fb0f697e70f1d1eacad915ea5f76bb123e6 X-Git-Newrev: b4fc81edcc25b5c313ab3805dc0bcd015c370e62 Message-Id: <20210420084656.ABE4A3858C27@sourceware.org> Date: Tue, 20 Apr 2021 08:46:56 +0000 (GMT) X-BeenThere: cygwin-cvs@cygwin.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Cygwin core component git logs List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 20 Apr 2021 08:46:56 -0000 https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=b4fc81edcc25b5c313ab3805dc0bcd015c370e62 commit b4fc81edcc25b5c313ab3805dc0bcd015c370e62 Author: Takashi Yano Date: Mon Apr 19 19:30:45 2021 +0900 Cygwin: console: Fix race issue regarding cons_master_thread(). - 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 Diff: --- 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 62ee9e07f..ebe89fc9f 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);