delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2016/01/07/12:30:52

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:cc:subject:message-id:reply-to
:references:mime-version:content-type:in-reply-to; q=dns; s=
default; b=NupBwnmwSe04lCYowmp5djwnpkcn6WEHHqFRMZ91wPXCJwTVN9t4g
mKrWWBA0/nEHu/MwTIuJ0D+jzVu9PY9VqLgUXrTlRK7K5Yv2V/zl8qViLiiOlFtu
mzKFzcpqoEzQ0oNZ2sedev/hvN6+GXeYnwuIDg7MHXQQKogNxiuEk4=
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:cc:subject:message-id:reply-to
:references:mime-version:content-type:in-reply-to; s=default;
bh=PZ9IOZv3Tg7xsvHG1ZLAwsEXWx4=; b=T5lEx410+s0jtUap+iVn3SSEO96D
FYQLcHdkxlTYsavkRCWX8H0GeN4bUn/x3MsB3imX7/nMH7wXfw5HpvQMedR07o6i
G4d9oWgkgGnw9JTZqWHhX74hN9pn/KwLKFFRYdTgBPTc8MNipZANdwG8T0JSTpwi
LGE/WCLkQWeZOJQ=
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=-101.9 required=5.0 tests=AWL,BAYES_50,KAM_LAZY_DOMAIN_SECURITY,KHOP_DYNAMIC,RCVD_IN_PBL,RDNS_DYNAMIC,USER_IN_WHITELIST autolearn=no version=3.3.2 spammy=corinna, vinschen, Maintainer, DOT
X-HELO: calimero.vinschen.de
Date: Thu, 7 Jan 2016 18:30:34 +0100
From: Corinna Vinschen <corinna-cygwin AT cygwin DOT com>
To: cygwin AT cygwin DOT com
Cc: john hood <cgull AT glup DOT org>
Subject: Re: cygwin.dll: bug with select on Windows console
Message-ID: <20160107173034.GE20447@calimero.vinschen.de>
Reply-To: cygwin AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com, john hood <cgull AT glup DOT org>
References: <5680953B DOT 8050200 AT glup DOT org>
MIME-Version: 1.0
In-Reply-To: <5680953B.8050200@glup.org>
User-Agent: Mutt/1.5.24 (2015-08-30)

--cPi+lWm09sJ+d57q
Content-Type: text/plain; charset=utf-8
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

Hi John,

On Dec 27 20:49, john hood wrote:
> Hi all,
>=20
> I'm one of the Mosh maintainers.  Recently a user reported a problem
> where Mosh exits suddenly soon after startup while he is typing at it,
> see <https://github.com/mobile-shell/mosh/issues/705>.
>=20
> The problem turns out to be that occasionally, select() times out,
> returns 0 as it should, but does *not* clear the fd_sets() passed in--
> they are left unaltered.  Mosh doesn't pay any attention to the count of
> ready fds and relies on the returned fd_sets being accurate.  Mosh sets
> the read fd_set and the error fd_set, and when it encounters this
> situation, it exits quietly when an error is found on one of the files
> involved (rather poor error handling on our part).
>=20
> This only seems to happen on Windows Console.  select() seems to operate
> reliably when used on a pty, whether from mintty, xterm, or sshd.
>=20
> My read of the POSIX standard is that select() should always set the
> fd_sets on a successful return (rv >=3D 0).  There is a bit of ambiguity
> around this point, but given that Cygwin is inconsistent with itself (on
> ptys) and with every other Unix platform, I think it's a bug.
>=20
> I've attached a little demo program for the bug.  Compile, run as
> "socket-t 1000" (the argument is the number of microseconds select()
> should wait), and mash keys on the keyboard for a little while.  It
> should report errors within 100 keystrokes.  I think there might be a
> dependency on the length of the wait passed to select(), I don't see the
> problem happening with the wait set to 100 seconds.

Unfortunately you missed to attach the testcase.  I checked Cygwin's
select and I can see what might be the reason for what you're observing.
I might even have a fix.  But what bugs me is that I don't see how this
condition could be met at all, especially not in case of selecting on
the console.  Your testcase would be greatly appreciated.

For further discussion I attached the patch I'm proposing.  AFAICS,
copying the content of r,w,e over to readfds,writefds,exceptfds doesn't
really make sense, unless r,w,e are still unchanged (i.e. zeroed out).
However, *if* r,w,e have set bits, sel.wait should have returned
select_ok, not select_set_zero.  I don't see how this might occur.
That's what your testcase hopefully helps to uncover.

Another hiccup with copying r,w,e to readfds,writefds,exceptfds and then
calling sel.poll is this:  The number of set bits is *only* determined
by sel.poll.  If there are any other bits set in r,w,e, the result of
sel.poll might not reflect this and the return value is not equal to the
number of bits set in the records.  Obviously this is not a problem in
your case since you don't check the return value, but other applications
might.


Thanks,
Corinna


diff --git a/winsup/cygwin/select.cc b/winsup/cygwin/select.cc
index 524e578..647c758 100644
--- a/winsup/cygwin/select.cc
+++ b/winsup/cygwin/select.cc
@@ -77,8 +77,6 @@ details. */
   (fd_set *) __res; \
 })
=20
-#define copyfd_set(to, from, n) memcpy (to, from, sizeof_fd_set (n));
-
 #define set_handle_or_return_if_not_open(h, s) \
   h =3D (s)->fh->get_io_handle_cyg (); \
   if (cygheap->fdtab.not_open ((s)->fd)) \
@@ -132,6 +130,7 @@ select (int maxfds, fd_set *readfds, fd_set *writefds, =
fd_set *exceptfds,
 	DWORD ms)
 {
   int res =3D select_stuff::select_loop;
+  int ret =3D 0;
=20
   /* Record the current time for later use. */
   LONGLONG start_time =3D gtod.msecs ();
@@ -187,14 +186,15 @@ select (int maxfds, fd_set *readfds, fd_set *writefds=
, fd_set *exceptfds,
       select_printf ("res %d", res);
       if (res >=3D 0)
 	{
-	  copyfd_set (readfds, r, maxfds);
-	  copyfd_set (writefds, w, maxfds);
-	  copyfd_set (exceptfds, e, maxfds);
-	  if (res =3D=3D select_stuff::select_set_zero)
-	    res =3D 0;
-	  else
-	    /* Set the bit mask from sel records */
-	    res =3D sel.poll (readfds, writefds, exceptfds) ?: select_stuff::sele=
ct_loop;
+	  UNIX_FD_ZERO (readfds, maxfds);
+	  UNIX_FD_ZERO (writefds, maxfds);
+	  UNIX_FD_ZERO (writefds, maxfds);
+	  /* Set bit mask from sel records, even in case of a timeout.  This
+	     also sets ret to the right value >=3D 0, matching the number of
+	     bits set in the fds records. */
+	  ret =3D sel.poll (readfds, writefds, exceptfds);
+	  if (!ret && res !=3D select_stuff::select_set_zero)
+	    res =3D select_stuff::select_loop;
 	}
       /* Always clean up everything here.  If we're looping then build it
 	 all up again.  */
@@ -219,9 +219,9 @@ select (int maxfds, fd_set *readfds, fd_set *writefds, =
fd_set *exceptfds,
 	}
     }
=20
-  if (res < -1)
-    res =3D -1;
-  return res;
+  if (res < 0)
+    ret =3D -1;
+  return ret;
 }
=20
 extern "C" int

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

--cPi+lWm09sJ+d57q
Content-Type: application/pgp-signature; name="signature.asc"

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

iQIcBAEBCAAGBQJWjqC5AAoJEPU2Bp2uRE+gEqkP/iNWf7gcTiGGktw52kSJx1/s
VmOXkF6/S/UAa+QpyFmLSMq+pm+ZluEL8vDr0lROHWyb1WA1g9kaeaKCQIifc2pd
6TfsR1EdACFJXVShEoP4S1ccFhYBMBWsbovoZFwmbWtkfZoeuBRJucb0brQO+pes
+3E3pNR6AvrMdEtX9zm2xS+YDDLlD0Y37AL7d9M04kqpBa2FagbShp+lD4vhk/sE
uvwNGV/CqEobSqWZzCkF8lQeDZGHfNZr4NSlI5/qPYtt3hPLGdc3zQb9D6FgWjSA
yUqQ0Q457lQZqrwnf99jb2j7LqZT3WvfA8xtDPvhc2nCJquGhTCAWNwc7NCf9/2p
/O5aOcpakoCweQmJlbFID6gdGRZ01D05lqHMGDcNqngU5D94/eV9alvyImTUIam2
qIFleweENfL6NybG69iduJ+/yce2cmypUglDTDZjJa7rQz5jFNfpdfBj5RIkl99b
ouKb0ipqURktqkl8Rx3fndN7LNL97BfPBoeRHWIryoBra4UYQYuKWtVJY3SdeEjf
4eWhF+TnPUBm0c/Pr4hVw578FL6qavyn4+NXKh1JuSVOvis2kPovCMqMCFpdDhB/
UXtlm9ocH5OCoeQ8PJd3lfERpZoEThK0A6HUlAfe+nh2ftIiZKvAYUdq2wYaCw0T
lEcURweBhw6hF4HAc30R
=Lvgc
-----END PGP SIGNATURE-----

--cPi+lWm09sJ+d57q--

- Raw text -


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