delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2026/05/14/16:12:56

DMARC-Filter: OpenDMARC Filter v1.4.2 delorie.com 64EKCtEc3316087
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 64EKCtEc3316087
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=dBnnvdel
X-Recipient: archive-cygwin AT delorie DOT com
DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 00FD14BA2E30
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cygwin.com;
s=default; t=1778789574;
bh=xp2uOdtSj77zOZEDMVAm96E5SBI/PYuR5CJmX075zHo=;
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=dBnnvdelnpO2lRLoh4npM/u9iutlzqkiLaVmCNVevtu/RCimSZNlUSXM7pSmIQXlD
7xvlpnZj9epiatsVmD8MestkOaDR1VTz+D7Xc+6aAvzc5ys4JlIVJgDDXfaja5oP0h
EYpDs1yiUz6hYDMBo/CSGRXhXenujJxYMGxOuInk=
X-Original-To: cygwin AT cygwin DOT com
Delivered-To: cygwin AT cygwin DOT com
DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EE4024BA9003
ARC-Filter: OpenARC Filter v1.0.0 sourceware.org EE4024BA9003
ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1778789513; cv=none;
b=pOwpBmmWnDlX7ok6PuRongWn902Vg2y+nFC1vsXZJFnw9zBMcFc906d1dAxvVXhtVqnYGnqxJQrJsKf8mnvo+Ya2LIRSYMgChPK3rFHZj02IW6Ec3nIvsOcaARSMtm0vlvu56QAL/gaXb7Tue7PYsmYF0wR1nKQzXCqX366y6qk=
ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key;
t=1778789513; c=relaxed/simple;
bh=gPCx8XAj5wP+IsBOQOZphq+5C/zlW7lQMiUCbvfa8ec=;
h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version;
b=n+dXFULgfM6YMrolvhASHjkRIu2iTz+4cLZ9mnrzJhY35aMJJu9GU9d+gI8bQG0XYffDl4lZ41VonJ5iPz9/IMUjLnVgrZohFzrWyN8sr2Zn962prmO2eBLIST+Z3F5Qg7wOegU5+qQr74Nq1O4H5WXPOlYfdEOuzyNC7Nylpp8=
ARC-Authentication-Results: i=1; sourceware.org;
dkim=pass (2048-bit key, secure)
header.d=disroot.org header.i=@disroot.org header.a=rsa-sha256 header.s=mail
header.b=V4LAIaIg
DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EE4024BA9003
X-Virus-Scanned: SPAM Filter at disroot.org
Date: Thu, 14 May 2026 23:11:48 +0300
To: cygwin AT cygwin DOT com
Subject: Re: On exit, winsup/cygwin/fhandler/console.cc waits on closed
handle, deadlock possible
Message-ID: <20260514231148.60c1743e@Tarkus>
In-Reply-To: <20260513125959.52327763@arachnoid>
References: <20260513125959 DOT 52327763 AT arachnoid>
MIME-Version: 1.0
X-BeenThere: cygwin AT cygwin DOT com
X-Mailman-Version: 2.1.30
List-Id: General Cygwin discussions and problem reports <cygwin.cygwin.com>
List-Unsubscribe: <https://cygwin.com/mailman/options/cygwin>,
<mailto:cygwin-request AT cygwin DOT com?subject=unsubscribe>
List-Archive: <https://cygwin.com/pipermail/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-request AT cygwin DOT com?subject=help>
List-Subscribe: <https://cygwin.com/mailman/listinfo/cygwin>,
<mailto:cygwin-request AT cygwin DOT com?subject=subscribe>
From: Ivan Krylov via Cygwin <cygwin AT cygwin DOT com>
Reply-To: Ivan Krylov <ikrylov AT disroot DOT org>
Errors-To: cygwin-bounces~archive-cygwin=delorie DOT com AT cygwin DOT com
Sender: "Cygwin" <cygwin-bounces~archive-cygwin=delorie DOT com AT cygwin DOT com>

On Wed, 13 May 2026 12:59:59 +0300
Ivan Krylov <ikrylov AT disroot DOT org> 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.

-- 
Best regards,
Ivan

-- 
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

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019