DMARC-Filter: OpenDMARC Filter v1.4.2 delorie.com 64I1vhlA009198 Authentication-Results: delorie.com; dmarc=pass (p=none dis=none) header.from=cygwin.com Authentication-Results: delorie.com; spf=pass smtp.mailfrom=cygwin.com DKIM-Filter: OpenDKIM Filter v2.11.0 delorie.com 64I1vhlA009198 Authentication-Results: delorie.com; dkim=pass (1024-bit key, unprotected) header.d=cygwin.com header.i=@cygwin.com header.a=rsa-sha256 header.s=default header.b=RYZkTb11 X-Recipient: archive-cygwin AT delorie DOT com DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 70D394BA9009 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cygwin.com; s=default; t=1779069461; bh=7RtGta0eaZ60cd1XamsOtH7PeR7VEtKjR38XAarOYYM=; h=Date:To:Subject:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=RYZkTb11nlkUPWCqEqZNA/ml7tD2SiPVMkt/JmbhaV3HN4A7MeiDHjofqm8DLSiGH QgTpx25JasDk2u+dOVF25hWoyuCpwBmgQvsrbXcwv4JigpIkvaoqLwae2cK9d38kwV K8Ky58XRHNmgFbcyDytUoc4I5X+7htepdee0ihjU= X-Original-To: cygwin AT cygwin DOT com Delivered-To: cygwin AT cygwin DOT com DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 299A84BA79A0 ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 299A84BA79A0 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1779069410; cv=none; b=I8Qq8kbBDzhE9IixvT8mIb1K75E0XQGpY2LhFp1pKqgiAvu+9+T7jdJ9dsydfFfKhBPN/w/nVjUtaYq3pP/5GniAiNetfiSBBy+slJ8mVUfl40ioYK5GbzY3I0jytxc3pi2famZCePrcqOxZBpBk1As32vETggttUn0EDlf3TQo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1779069410; c=relaxed/simple; bh=rRRkf4F61dktTFExURzZXlcBL3TYp33lGTwFUIcDNC4=; h=Date:From:To:Subject:Message-Id:Mime-Version:DKIM-Signature; b=ZAqIVhqPRLALQ1opcycL+KcfbaSQv/2/QtIFdP1TaJNhuZWKAzcIwxfsZqJwb8CJc6dPb1STWwIWIJBzfB4UUMXiaG90/6g1ay7OxXMqzxq5D+gQzCF2POQnQCUxsmX+xSG/nUL69O1PXqK56G+fmRqLR8loNAlxjlGm06zZ7FY= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=nifty.ne.jp header.i=@nifty.ne.jp header.a=rsa-sha256 header.s=default-1th84yt82rvi header.b=IU5R6fwB DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 299A84BA79A0 Date: Mon, 18 May 2026 10:56:45 +0900 To: cygwin AT cygwin DOT com Subject: Re: On exit, winsup/cygwin/fhandler/console.cc waits on closed handle, deadlock possible Message-Id: <20260518105645.db872ca25d139621348a9853@nifty.ne.jp> In-Reply-To: <20260518101326.064ca1b0be2542194b7022ef@nifty.ne.jp> References: <20260513125959 DOT 52327763 AT arachnoid> <20260514231148 DOT 60c1743e AT Tarkus> <20260518100404 DOT fa8ecf8901503c72bb610ece AT nifty DOT ne DOT jp> <20260518101326 DOT 064ca1b0be2542194b7022ef AT nifty DOT ne DOT jp> X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.30; i686-pc-mingw32) Mime-Version: 1.0 X-BeenThere: cygwin AT cygwin DOT com X-Mailman-Version: 2.1.30 Precedence: list List-Id: General Cygwin discussions and problem reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Takashi Yano via Cygwin Reply-To: Takashi Yano Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: cygwin-bounces~archive-cygwin=delorie DOT com AT cygwin DOT com Sender: "Cygwin" On Mon, 18 May 2026 10:13:26 +0900 Takashi Yano via Cygwin wrote: > On Mon, 18 May 2026 10:04:04 +0900 > Takashi Yano wrote: > > On Thu, 14 May 2026 23:11:48 +0300 > > Ivan Krylov wrote: > > > On Wed, 13 May 2026 12:59:59 +0300 > > > Ivan Krylov wrote: > > > > > > > I think it doesn't close the race window completely. > > > > > > Here's a patch that I have more confidence in: > > > > > > diff --git a/winsup/cygwin/fhandler/console.cc b/winsup/cygwin/fhandler/console.cc > > > index c76347f6f..a97df4cef 100644 > > > --- a/winsup/cygwin/fhandler/console.cc > > > +++ b/winsup/cygwin/fhandler/console.cc > > > @@ -62,6 +62,7 @@ fhandler_console::console_state NO_COPY > > > > > > static bool NO_COPY inside_pcon_checked = false; > > > static bool NO_COPY inside_pcon = false; > > > +static LONG NO_COPY inside_pcon_cleanup = 0; > > > static int NO_COPY parent_pty; > > > static HANDLE NO_COPY parent_pty_input_mutex = NULL; > > > > > > @@ -530,8 +531,13 @@ fhandler_console::cons_master_thread (handle_set_t *p, tty *ttyp) > > > default: /* Error */ > > > free (input_rec); > > > free (input_tmp); > > > - if (inside_pcon && parent_pty_input_mutex) > > > + if (inside_pcon && parent_pty_input_mutex) { > > > ReleaseMutex (parent_pty_input_mutex); > > > + if (InterlockedCompareExchange(&inside_pcon_cleanup, 0, 0)) { > > > + ForceCloseHandle (parent_pty_input_mutex); > > > + inside_pcon = false; > > > + } > > > + } > > > ReleaseMutex (p->input_mutex); > > > return; > > > } > > > @@ -713,8 +719,13 @@ remove_record: > > > while (true); > > > } > > > skip_writeback: > > > - if (inside_pcon && parent_pty_input_mutex) > > > + if (inside_pcon && parent_pty_input_mutex) { > > > ReleaseMutex (parent_pty_input_mutex); > > > + if (InterlockedCompareExchange(&inside_pcon_cleanup, 0, 0)) { > > > + ForceCloseHandle (parent_pty_input_mutex); > > > + inside_pcon = false; > > > + } > > > + } > > > ReleaseMutex (p->input_mutex); > > > cygwait (40); > > > } > > > @@ -2048,7 +2059,7 @@ fhandler_console::pcon_hand_over_proc (void) > > > else > > > system_printf("Acquiring pcon_ho_mutex failed."); > > > ReleaseMutex (mtx); > > > - ForceCloseHandle (parent_pty_input_mutex); > > > + InterlockedExchange(&inside_pcon_cleanup, 1); > > > } > > > > > > bool > > > > > > Instead of manipulating the mutex handle shared between threads and > > > leaving a race window between if(parent_pty_input_mutex) and > > > WaitForSingleObject(parent_pty_input_mutex, ...), tell the > > > cons_master_thread to close the handle and stop using it. > > > > Thanks for the report and the patch. > > > > The root cause of the problem is closing parent_pty_input_mutex before > > stopping cons_master_thread. > > > > What abount just moving > > > - ForceCloseHandle (parent_pty_input_mutex); > > into fhandler_console::close() like: > > > > diff --git a/winsup/cygwin/fhandler/console.cc b/winsup/cygwin/fhandler/console.cc > > index c76347f6f..c4950885a 100644 > > --- a/winsup/cygwin/fhandler/console.cc > > +++ b/winsup/cygwin/fhandler/console.cc > > @@ -2048,7 +2048,6 @@ fhandler_console::pcon_hand_over_proc (void) > > else > > system_printf("Acquiring pcon_ho_mutex failed."); > > ReleaseMutex (mtx); > > - ForceCloseHandle (parent_pty_input_mutex); > > } > > > > bool > > @@ -2157,6 +2156,9 @@ fhandler_console::close (int flag) > > CloseHandle (output_mutex); > > output_mutex = NULL; > > > > + ForceCloseHandle (parent_pty_input_mutex); > > Hmm, this should be: > + if (parent_pty_input_mutex) > + CloseHandle (parent_pty_input_mutex); > > > + parent_pty_input_mutex = NULL; > > + > > WaitForSingleObject (shared_info_mutex, INFINITE); > > if (--shared_info_state[unit] == 0 && shared_console_info[unit]) > > { This might be better: diff --git a/winsup/cygwin/fhandler/console.cc b/winsup/cygwin/fhandler/console.cc index c76347f6f..6fd4cd965 100644 --- a/winsup/cygwin/fhandler/console.cc +++ b/winsup/cygwin/fhandler/console.cc @@ -2018,7 +2018,6 @@ fhandler_console::setup_pcon_hand_over () if (get_console_process_id (owner, true, false, false, false)) { inside_pcon = true; - atexit (fhandler_console::pcon_hand_over_proc); parent_pty = i; parent_pty_input_mutex = cygwin_shared->tty[i]->open_input_mutex (MAXIMUM_ALLOWED); @@ -2157,6 +2156,8 @@ fhandler_console::close (int flag) CloseHandle (output_mutex); output_mutex = NULL; + pcon_hand_over_proc (); + WaitForSingleObject (shared_info_mutex, INFINITE); if (--shared_info_state[unit] == 0 && shared_console_info[unit]) { -- Takashi Yano -- Problem reports: https://cygwin.com/problems.html FAQ: https://cygwin.com/faq/ Documentation: https://cygwin.com/docs.html Unsubscribe info: https://cygwin.com/ml/#unsubscribe-simple