delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2015/05/28/07:47:48

X-Recipient: archive-cygwin AT delorie DOT com
DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id
:list-unsubscribe:list-subscribe:list-archive:list-post
:list-help:sender:date:from:to:subject:message-id:reply-to
:references:mime-version:content-type:in-reply-to; q=dns; s=
default; b=fdCxOAE+r0YdyJSrEVn7pXi7nDUOWZkowGsQ15+nKx600MBauJDbE
YPuiQxkYQCDyZA1vkxUQEdupQDufWmZz0P/n2DWVsAkh0caxW2B8LXiX5ziwotgZ
csy1OWXQgdte9ut4hEMeYmhFaoJqIQJDMAZiixoH3gfxU2mLwXUwRM=
DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id
:list-unsubscribe:list-subscribe:list-archive:list-post
:list-help:sender:date:from:to:subject:message-id:reply-to
:references:mime-version:content-type:in-reply-to; s=default;
bh=YdgPfJZ6TxiBWOmsudsQ8HQtzlE=; b=v0lDfySeLmm2iLbnMmWPmKqJrFiq
x6SOfmRSND80yqZ8WnpeEp7a3r14kd7f6SbfOZdx9GK8W3n3LwqO2y0oFlmp4CN1
T4NdTjKaMR89UNlSiWDWLnqWNVAFQeEyc5VXf1W/NY5xrKhqlT44QA14+hCPVmMW
je+H07CBCEpU4wQ=
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
Authentication-Results: sourceware.org; auth=none
X-Virus-Found: No
X-Spam-SWARE-Status: No, score=-4.1 required=5.0 tests=AWL,BAYES_50,KAM_LAZY_DOMAIN_SECURITY autolearn=no version=3.3.2
X-HELO: calimero.vinschen.de
Date: Thu, 28 May 2015 13:47:28 +0200
From: Corinna Vinschen <corinna-cygwin AT cygwin DOT com>
To: cygwin AT cygwin DOT com
Subject: Re: cygwin-2 process handling
Message-ID: <20150528114728.GA27014@calimero.vinschen.de>
Reply-To: cygwin AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
References: <1573487218 DOT 1490468 DOT 1431969356192 DOT JavaMail DOT yahoo AT mail DOT yahoo DOT com> <555B6F71 DOT 4040906 AT cornell DOT edu> <555B7E03 DOT 40404 AT cornell DOT edu> <20150521205357 DOT 2c125b3bcaf877d0843b52b1 AT nifty DOT ne DOT jp> <20150527122312 DOT GF16927 AT calimero DOT vinschen DOT de> <20150527151734 DOT GM16927 AT calimero DOT vinschen DOT de>
MIME-Version: 1.0
In-Reply-To: <20150527151734.GM16927@calimero.vinschen.de>
User-Agent: Mutt/1.5.23 (2014-03-12)

--vkogqOf2sHV7VnPd
Content-Type: text/plain; charset=utf-8
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On May 27 17:17, Corinna Vinschen wrote:
> On May 27 14:23, Corinna Vinschen wrote:
> > Hi Takashi,
> >=20
> > On May 21 20:53, Takashi Yano wrote:
> > > [...]
> > > +  if (output_handle_local)
> > > +    {
> > > +      OBJECT_BASIC_INFORMATION obi;
> > > +      NTSTATUS status;
> > > +      ULONG hdl_cnt =3D 0;
> > > +
> > > +      status =3D NtQueryObject (output_handle_local, ObjectBasicInfo=
rmation,
> > > +	  &obi, sizeof obi, NULL);
> > > +      if (!NT_SUCCESS (status))
> > > +	debug_printf ("NtQueryObject: %y", status);
> > > +      else
> > > +	hdl_cnt =3D obi.HandleCount;
> > > +      termios_printf("HandleCount: %d", hdl_cnt);
> > > +      if (hdl_cnt =3D=3D 1)
> > > +	SetEvent (input_available_event);
> > > +      CloseHandle (output_handle_local);
> > > +    }
> >=20=20
> > Isn't that racy?  Consider two processes doing that at the same time.
> > Both calls to NtQueryObject could come up with hdl_cnt =3D=3D 2 and the
> > problem persists.
> >=20
> > Wouldn't it be safer to call SetEvent(input_available_event) all the
> > time from here?
>=20
> We discussed this already in March and only briefly talked about a
> change like this requiring changes to fhandler_pty_slave::read.
> However, I don't see this.  The read code already takes 0 bytes input
> and broken pipe scenarios into account.  Do you see something needing
> a change I don't?

Never mind, it doesn't work quite as expected, so changes would be
required.

I created another version of your patch which avoids duplicating the
tested handle and makes the test-and-close-handle operation atomic:

	* fhandler_tty.cc (fhandler_pty_common::close): Don't close output_mutex
	here.  Move into callers.
	(fhandler_pty_master::close): Use NtQueryObject instead of
	PeekNamedPipe to detect closing the last master handle.

diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index e91b3e3..12f6124 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -552,7 +552,10 @@ fhandler_pty_slave::close ()
 	get_output_handle_cyg ());
   if ((unsigned) myself->ctty =3D=3D FHDEV (DEV_PTYS_MAJOR, get_minor ()))
     fhandler_console::free_console ();	/* assumes that we are the last pty=
 closer */
-  return fhandler_pty_common::close ();
+  fhandler_pty_common::close ();
+  if (!ForceCloseHandle (output_mutex))
+    termios_printf ("CloseHandle (output_mutex<%p>), %E", output_mutex);
+  return 0;
 }
=20
 int
@@ -1259,8 +1262,6 @@ fhandler_pty_common::close ()
   termios_printf ("pty%d <%p,%p> closing", get_minor (), get_handle (), ge=
t_output_handle ());
   if (!ForceCloseHandle (input_mutex))
     termios_printf ("CloseHandle (input_mutex<%p>), %E", input_mutex);
-  if (!ForceCloseHandle (output_mutex))
-    termios_printf ("CloseHandle (output_mutex<%p>), %E", output_mutex);
   if (!ForceCloseHandle1 (get_handle (), from_pty))
     termios_printf ("CloseHandle (get_handle ()<%p>), %E", get_handle ());
   if (!ForceCloseHandle1 (get_output_handle (), to_pty))
@@ -1281,6 +1282,9 @@ fhandler_pty_master::cleanup ()
 int
 fhandler_pty_master::close ()
 {
+  OBJECT_BASIC_INFORMATION obi;
+  NTSTATUS status;
+
   termios_printf ("closing from_master(%p)/to_master(%p)/to_master_cyg(%p)=
 since we own them(%u)",
 		  from_master, to_master, to_master_cyg, dwProcessId);
   if (cygwin_finished_initializing)
@@ -1309,13 +1313,22 @@ fhandler_pty_master::close ()
 	}
     }
=20
-  fhandler_pty_common::close ();
-
   /* Check if the last master handle has been closed.  If so, set
      input_available_event to wake up potentially waiting slaves. */
-  if (!PeekNamedPipe (from_master, NULL, 0, NULL, NULL, NULL)
-      && GetLastError () =3D=3D ERROR_BROKEN_PIPE)=20
-    SetEvent (input_available_event);
+  acquire_output_mutex (INFINITE);
+  status =3D NtQueryObject (get_output_handle (), ObjectBasicInformation,
+			  &obi, sizeof obi, NULL);
+  fhandler_pty_common::close ();
+  release_output_mutex ();
+  if (!ForceCloseHandle (output_mutex))
+    termios_printf ("CloseHandle (output_mutex<%p>), %E", output_mutex);
+  if (!NT_SUCCESS (status))
+    debug_printf ("NtQueryObject: %y", status);
+  else if (obi.HandleCount =3D=3D 1)
+    {
+      termios_printf("Closing last master");
+      SetEvent (input_available_event);
+    }
=20
   if (!ForceCloseHandle (from_master))
     termios_printf ("error closing from_master %p, %E", from_master);

Does that look ok?  It fixes the reported problem for me.


Thanks,
Corinna

--=20
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Maintainer                 cygwin AT cygwin DOT com
Red Hat

--vkogqOf2sHV7VnPd
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iQIcBAEBCAAGBQJVZwBQAAoJEPU2Bp2uRE+gp4kQAJaOduM6cV//3y9AyVzb4IMp
1x8UrtE7XHAucmrbw9iYfnYSA6aHp9sX6/cfJzQ2kncr6scxl6NNezWROD4UbigW
Zkv04cINdb27yRHefbwLAbJT/5p2PpKCxN4RTcIMTtLKDmFwmEaOgeVtXGhSzLKr
311vXvjUz/AOcgHEVulTkRt6SPu2iASSdE/M/XQ+pxkGYYWrm/nxBbPCAtPfa4pT
EIDOxHgo6yiMHmrUN+anfN7ug2Z2bVIa2kt+4apXgc5irRqc+0yOVmUPoZ9n/S/H
DGtx4FSGx8w1fhqkY1BlaPgXanJxlX2jP5xPqBNxD8V0ShmUDfLoFQdbLT/RBiZl
hDQTpseyc9n9HmU6vrcpSfgyKnF5afMIWXaqfYf8bmOyTxdwCZvnXP8neYAbojAJ
neLGV+glbUpLChc/ObjDbG9KrkGghLIuU/vAX7unTCUESNEy5iluM3mK2xbDKbbr
KQyTt1OipZl/VZi2AHPXXhIPr2uY4I+qUaR0vvKG/VmvXBlacKkCynafrYDQvfwz
NItpGtJ/baUQFhUJlezW+Ly8P+S+fljDPSRrCq7kkxZdZpy5mdwH48chKp6OWvnz
yDPci/MBWe2KR50vhcz8ELZcmRIj/wFoU3DJK7kOGE7MEPzPhnz4BDqjxw1qaAhI
w/xrYvZSYZiBC+lF4QKK
=hNoR
-----END PGP SIGNATURE-----

--vkogqOf2sHV7VnPd--

- Raw text -


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