From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2155) id 524D33858C2A; Tue, 1 Aug 2023 12:23:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 524D33858C2A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1690892610; bh=RtrH1xw0dsQiPgnSOzMPbzDJxBS/ksmPrpr7xpuVTHQ=; h=From:To:Subject:Date:From; b=poyFTdRXMGV9p6UDty7MgUlOKHQyM4mI49hST8tjIqPCTs2KeoCVURnemtKTk6ecg 2TbVryYZ55owZOE8pwNSNfGQikfWgb+etvYmvrSwldRLs/omQKZsXZ6CkEk2kqfWk2 nBIwD1dAHrGmlk4QfaUhqofSeN3UdoFa8aXHyfYA= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Corinna Vinschen To: cygwin-cvs@sourceware.org Subject: [newlib-cygwin/main] Cygwin: select: workaround FD_WRITE network event handling X-Act-Checkin: newlib-cygwin X-Git-Author: Corinna Vinschen X-Git-Refname: refs/heads/main X-Git-Oldrev: 0e711d6cc9b5206335fe8562817b6d5e6cad876e X-Git-Newrev: dedbbd74d0a8f3b7dfae6188321703a47bb8a2b3 Message-Id: <20230801122330.524D33858C2A@sourceware.org> Date: Tue, 1 Aug 2023 12:23:30 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dnewlib-cygwin.git;h=3Ddedbbd74d0a= 8f3b7dfae6188321703a47bb8a2b3 commit dedbbd74d0a8f3b7dfae6188321703a47bb8a2b3 Author: Corinna Vinschen AuthorDate: Tue Aug 1 14:22:55 2023 +0200 Commit: Corinna Vinschen CommitDate: Tue Aug 1 14:22:55 2023 +0200 Cygwin: select: workaround FD_WRITE network event handling =20 The FD_WRITE event is a false friend. It indicates ready to write even if the next send fails with WSAEWOULDBLOCK. *After* the fact, FD_WRITE will be cleared until sending is again possible, but that is too late for a select/write loop. =20 Workaround that by using the WinSock select function when peeking at a socket and FD_WRITE gets indicated. WinSock select fortunately indicates writability correctly. =20 Fixes: 70e476d27be8 ("(peek_socket): Use event handling for peeking soc= ket.") Signed-off-by: Corinna Vinschen Diff: --- winsup/cygwin/autoload.cc | 1 + winsup/cygwin/select.cc | 30 +++++++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/winsup/cygwin/autoload.cc b/winsup/cygwin/autoload.cc index c9ad92530c83..67149f828266 100644 --- a/winsup/cygwin/autoload.cc +++ b/winsup/cygwin/autoload.cc @@ -645,6 +645,7 @@ LoadDLLfunc (getsockname, ws2_32) LoadDLLfunc (getsockopt, ws2_32) LoadDLLfunc (ioctlsocket, ws2_32) LoadDLLfunc (listen, ws2_32) +LoadDLLfunc_pfx_only (select, ws2_32) LoadDLLfunc (setsockopt, ws2_32) LoadDLLfunc (shutdown, ws2_32) LoadDLLfunc (socket, ws2_32) diff --git a/winsup/cygwin/select.cc b/winsup/cygwin/select.cc index bad4c37f3b2e..7b9473849ec2 100644 --- a/winsup/cygwin/select.cc +++ b/winsup/cygwin/select.cc @@ -1757,6 +1757,15 @@ fhandler_base::select_except (select_stuff *ss) return s; } =20 +struct wfd_set +{ + u_int fd_count; + SOCKET fd_array[1]; +}; + +/* autoload exposes the Winsock select function only with a _win32_ prefix= . */ +extern "C" int _win32_select(int, wfd_set *, wfd_set *, wfd_set *, TIMEVAL= *); + static int peek_socket (select_record *me, bool) { @@ -1772,9 +1781,24 @@ peek_socket (select_record *me, bool) if (me->read_selected) me->read_ready |=3D ret || !!(events & (FD_READ | FD_ACCEPT | FD_CLOSE= )); if (me->write_selected) - /* Don't check for FD_CLOSE here. Only an error case (ret =3D=3D -1) - will set ready for writing. */ - me->write_ready |=3D ret || !!(events & (FD_WRITE | FD_CONNECT)); + { + /* The FD_WRITE event is a false friend. It indicates ready to write + even if the next send fails with WSAEWOULDBLOCK. *After* the fac= t, + FD_WRITE will be cleared until sending is again possible. + For the time being, workaround that here by using the WinSock + select function. It indicates writability correctly. */ + if (events & FD_WRITE) + { + wfd_set w =3D { 1, { fh->get_socket () } }; + TIMEVAL t =3D { 0 }; + + if (_win32_select (0, NULL, &w, NULL, &t) =3D=3D 0) + events &=3D ~FD_WRITE; + } + /* Don't check for FD_CLOSE here. Only an error case (ret =3D=3D -1) + will set ready for writing. */ + me->write_ready |=3D ret || !!(events & (FD_WRITE | FD_CONNECT)); + } if (me->except_selected) me->except_ready |=3D !!(events & FD_OOB);