From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mout.kundenserver.de (mout.kundenserver.de [212.227.126.135]) by sourceware.org (Postfix) with ESMTPS id AABD93945065 for ; Thu, 12 Mar 2020 14:44:48 +0000 (GMT) Received: from calimero.vinschen.de ([24.134.7.25]) by mrelayeu.kundenserver.de (mreue012 [212.227.15.167]) with ESMTPSA (Nemesis) id 1MGQ85-1j7E951ATN-00Goa1 for ; Thu, 12 Mar 2020 15:44:46 +0100 Received: by calimero.vinschen.de (Postfix, from userid 500) id CCF1CA82791; Thu, 12 Mar 2020 15:44:45 +0100 (CET) Date: Thu, 12 Mar 2020 15:44:45 +0100 From: Corinna Vinschen To: cygwin@cygwin.com Subject: Re: Setting termios VMIN > 0 and VTIME > 0 on non blocking file Message-ID: <20200312144445.GP4042@calimero.vinschen.de> Reply-To: cygwin@cygwin.com Mail-Followup-To: cygwin@cygwin.com References: <9e4c6428-59de-df07-9e33-44ba95d5497f@gmail.com> <20200312114041.GJ4042@calimero.vinschen.de> <365dd437-7553-eb4e-3253-aba3bab74895@gmail.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="jllsgs4PL/sXFNaa" Content-Disposition: inline In-Reply-To: <365dd437-7553-eb4e-3253-aba3bab74895@gmail.com> X-Provags-ID: V03:K1:ME/YLRObGbiuQj8CanUJ+kUklocskVaHH+xTpxP0VU1UAVCWfwv pgoK35zHgILcn5cGe5HQiDrRUMR3K0M5r/B7/3H4qjcXZHNXsMN+SawPO0Et3TfZkd5TXcr iRuq/Grcl7Ujw5awGsEL4AiXQSCPfVQLjR0sfUGztDD/q+6gdmZ0NUCZkSb42r7irUvTS2n lToPn7WZnxXB/Dnh5LDhA== X-UI-Out-Filterresults: notjunk:1;V03:K0:ZPaPW+/c5UI=:uDhkN7XRDy40KJT1/pqbEQ YeGaSupRpeohnOAXF1Iy+kXrVrgELmFSGN9ThTvRjQiQ5y05eH5vaTD8d98wEbXV1srhyAFUq vGB3MNqXpBQpbW0pZrwW1/vAChzWz6j4Wz7zAveG+QJVcz88hfcz9/KbSfR61Qhf7lB8n/vs0 KOr9HwO2HwkgFpAc5szBjHioUZntqEqKR3i93uro1PiMlm54P4R8Q81lXws+JbzLydfEkuDdY ho0uxuQShkYj5e0py3pQhOG3mBEogp3nPcNbN2t54Q6q3EXU+AIrUOELbHd6P07HJXIzEPAr1 XcAzVkIZv7QFk+U0Dnw45lZmyQpfnGkdbexE7el0XL/VgKXygxehkg2t6lLWbRSU3yCf29CJ4 iTHiJY2ZeZdVfjB1BYpAimpqaDII0YqTg3o1zazWXzi4lvaMZfwRazKuOH/6qJhqcl95js9cf E6dFVOGWr+9UXcym/i1Owq0YZe8ylpGVPcXBrj2tDQb5fR+ZNjV3GtjK5JHQaqegUcNrAUaYd IG9S9uhZiufyyYICE5fQ9EdIZLHGkRX3cTqqEs+gUheLHzbqpx6uISJDTNb3Yz1wS1ida8Fbe 3TERYOSB8R2WB21LepPyoyT4Viy4rTLSw48SgWFVSXCcnIpzUzSM2q1z5bSA06x9znZaaHh1z tZhYcIVqDJXbEXixyyghLD3FeUhDZ/HfSu4KNxgvlkRjbH7eJTa2bVoAvxACUp/xEMyE8/Joh ew39//GqfFBroasC6wqsTM65dINLWgqy6RNTeFuqUt5N0BSIn19BwkByuygH1bS+ZrQTPB3D5 TKUo6vTz81zwrfmu+gOv4+w2US+jAq3tHvvwSYDlqQDPkt2fggRkdxv1hD22bQ50TOOKjbL X-Spam-Status: No, score=-124.4 required=5.0 tests=BAYES_00, GARBLED_BODY, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, GOOD_FROM_CORINNA_CYGWIN, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: cygwin@cygwin.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Cygwin mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 12 Mar 2020 14:44:49 -0000 --jllsgs4PL/sXFNaa Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Mar 12 15:20, =C3=85ke Rehnman via Cygwin wrote: >=20 > On 2020-03-12 12:40, Corinna Vinschen wrote: > >=20 > > I inspected the serial I/O read function and I only see a subtil > > difference in terms of VMIN/VTIME which doesn't seem to be the culprit > > at first glance. In O_NONBLOCK mode, the underlying Windows function > > ReadFile is called unconditionally. My current hunch is this: > >=20 > > - If VMIN>0 && VTIME>0, the Windows equivalent of tcsetattr is > > told that VMIN>0. > >=20 > > - So, assuming VMIN =3D=3D 2 in Windows. If VTIME > 0, the Cygwin read > > function sets the number of bytes_to_read to 1 (this is old code, > > don't ask why). > Do you have a pointer to this? I can not find it... That's because it's not true. I created this mail during lunch break and screwed up. > > - My assumption now is that the ReadFile function fails because it's > > supposed to return only 1 char, but VMIN is > 1. > >=20 > > There are two issues here. > >=20 > > - If my assumption is correct, the tcsetattr function must not > > set the Windows VMIN equivalent to !=3D 0 in the O_NONBLOCK case. > >=20 > > - Also, as mention above, if VTIME > 0, Cygwin's read sets the number of > > bytes_to_read to 1. Weirdly, it does so even if there are more than > > 1 byte in the inbound queue. This sounds wrong to me. > >=20 > I think the problem is if the number of bytes requested are more than what To clarify: number of bytes =3D=3D VMIN? > is in the buffer it is going to overlap the read function (because of VTI= ME) > and immediately after that CancelIO is called. Contrary to what is mentio= ned > in the source code I think CancelIO is actually discarding data... So far we didn't have that experience. CancelIO is usually safe in this regard. > =C2=A0 /* Use CancelIo rather than PurgeComm (PURGE_RXABORT) since > =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0 PurgeComm apparently discards= in-flight bytes while CancelIo > =C2=A0=C2=A0=C2=A0 =C2=A0=C2=A0=C2=A0=C2=A0 only stops the overlapped IO = routine. */ >=20 >=20 > My suggestion is the following patch: >=20 > diff --git a/winsup/cygwin/fhandler_serial.cc > b/winsup/cygwin/fhandler_serial.cc > index 69e5768f6..afa8871bf 100644 > --- a/winsup/cygwin/fhandler_serial.cc > +++ b/winsup/cygwin/fhandler_serial.cc > @@ -898,7 +898,11 @@ fhandler_serial::tcsetattr (int action, const struct > termios *t) > =C2=A0=C2=A0 { > =C2=A0=C2=A0=C2=A0=C2=A0 memset (&to, 0, sizeof (to)); >=20 > -=C2=A0=C2=A0=C2=A0 if ((vmin_ > 0) && (vtime_ =3D=3D 0)) > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 if (is_nonblocking()) > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 { > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0= =C2=A0=C2=A0 to.ReadIntervalTimeout =3D MAXDWORD; > +=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 } > +=C2=A0=C2=A0=C2=A0 else if ((vmin_ > 0) && (vtime_ =3D=3D 0)) What if you switch to !O_NONBLOCK after calling tcsetattr? The setting of ReadIntervalTimeout would be lost then. Either we have to repeat calling SetCommTimeouts every time we switch mode, or we have to do the above setting temporary every time we call ReadFile in non blocking mode. Corinna --=20 Corinna Vinschen Cygwin Maintainer --jllsgs4PL/sXFNaa Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAEBCAAdFiEEoVYPmneWZnwT6kwF9TYGna5ET6AFAl5qSt0ACgkQ9TYGna5E T6AOcw//VFp4Ad+RksjMK9cBVZO9xH1jA1tlJwOE1U4ay6ArKiVGVZCWMckhXc/J SYaFhn2uovS00A9Qo8cQNDZZJZIXwYs3/16BuxBr0rkrJMbLcIs5qOwcGlS2UiJy eRPCqkZYtWWmamXsUqggcOiSuPlBYt9a4NQzt8v2joeb44y8dXj7S2Cc+sV9JvyA 3Svo0tPXah37dZ3wXZVg7rKtRUVSAlqt6PSsBA7gn6wdtEqRvOcHLZWA7BZ6vt0n nmyo+6kCHfz6qOj4pyTWNu8blGXZwNe5oPcCVYDwVCEZqy+3x4ayBNANeIRkurfD qeYklui9P+Fcjc696syucU7smdfQOGWpUNhMvC4RqS2DGTKhAa9ZdVM/uEoedTva OyF+0ocZOvFs89J8pAPsMv0Oms7WeppmP77qN1ZkLAFlWRuHY/1PR13veQL8b4aR 65eUyExFbPqnOf5DwhVycnkYglJegPU6rOf4Q6UKKkpHI11VgzdwLUfnv4103s9U NMSBoQU3z/ldOEIjAKJ5NpYoTVJ7SrBRs4S0Z7nlSK7V7pusaCMmmnK70b0277K5 XcAMqApjsutg9oZvMC0c/OWpNBpRxG6vqkNXWJUV2wT4gr/2ETxOBTP8FG/mLIVr VZLExwFFxFFCEY4C+Sz0EyCX5CS6nNADqJQaywl8jc7LRlbQ3TQ= =lLsn -----END PGP SIGNATURE----- --jllsgs4PL/sXFNaa--