4
0
mirror of git://sourceware.org/git/newlib-cygwin.git synced 2025-02-18 23:12:15 +08:00

Cygwin: select: workaround FD_WRITE network event handling

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.

Workaround that by using the WinSock select function when peeking
at a socket and FD_WRITE gets indicated. WinSock select fortunately
indicates writability correctly.

Fixes: 70e476d27be8 ("(peek_socket): Use event handling for peeking socket.")
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
This commit is contained in:
Corinna Vinschen 2023-08-01 14:22:55 +02:00
parent 0e711d6cc9
commit dedbbd74d0
2 changed files with 28 additions and 3 deletions

View File

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

View File

@ -1757,6 +1757,15 @@ fhandler_base::select_except (select_stuff *ss)
return s;
}
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 |= ret || !!(events & (FD_READ | FD_ACCEPT | FD_CLOSE));
if (me->write_selected)
/* Don't check for FD_CLOSE here. Only an error case (ret == -1)
will set ready for writing. */
me->write_ready |= 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 fact,
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 = { 1, { fh->get_socket () } };
TIMEVAL t = { 0 };
if (_win32_select (0, NULL, &w, NULL, &t) == 0)
events &= ~FD_WRITE;
}
/* Don't check for FD_CLOSE here. Only an error case (ret == -1)
will set ready for writing. */
me->write_ready |= ret || !!(events & (FD_WRITE | FD_CONNECT));
}
if (me->except_selected)
me->except_ready |= !!(events & FD_OOB);