emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: ai_flags in calls to getaddrinfo, broader call for reproducibility c


From: Robert Pluim
Subject: Re: ai_flags in calls to getaddrinfo, broader call for reproducibility check
Date: Mon, 11 Jan 2021 21:12:44 +0100

Stefan Monnier <monnier@iro.umontreal.ca> writes:

>> Because the select implementation on windows does a one-byte readahead
>> to see if data is available, which breaks UDP. I had a patch at one
>> point to fix this, but I remember Eli not being very enthusiastic
>> about it. Iʼve attached what I think is the right version below (my
>> windows box died, so I can't be sure)
>
> How 'bout installing it but make it conditional on some config var?
> And maybe set that config var if/when a UDP socket is requested?

I guess thatʼs possible. Iʼve now found the actual patch. If Eli
thinks itʼs worth persuing, I can make it conditional, unless someone
wants to spare me the trouble of setting up a working Windows dev
environment :-).

As it stands you need to arrange for WORKING_SELECT_EMULATION to be
defined.

diff --git i/nt/inc/ms-w32.h w/nt/inc/ms-w32.h
index 1cce2c3062..ea6ba38dea 100644
--- i/nt/inc/ms-w32.h
+++ w/nt/inc/ms-w32.h
@@ -63,10 +63,11 @@ #define _CALLBACK_ __cdecl
    Look in <sys/time.h> for a timeval structure.  */
 #define HAVE_TIMEVAL 1
 
+#ifndef WORKING_SELECT_EMULATION
 /* Our select emulation does 1-byte read-ahead waiting for received
    packets, so datagrams are broken.  */
 #define BROKEN_DATAGRAM_SOCKETS 1
-
+#endif
 #define MAIL_USE_SYSTEM_LOCK 1
 
 /* Define to 1 if GCC-style __attribute__ ((__aligned__ (expr))) works. */
diff --git i/src/w32.c w/src/w32.c
index 698e10e234..c0457ff00f 100644
--- i/src/w32.c
+++ w/src/w32.c
@@ -8798,6 +8798,45 @@ _sys_wait_accept (int fd)
   return cp->status;
 }
 
+#ifdef WORKING_SELECT_EMULATION
+int
+_sys_wait_readable (int fd)
+{
+  HANDLE hEv;
+  child_process * cp;
+  int rc;
+
+  if (fd < 0 || fd >= MAXDESC)
+    return STATUS_READ_ERROR;
+
+  cp = fd_info[fd].cp;
+
+  if (cp == NULL || cp->fd != fd || cp->status != STATUS_READ_READY)
+    return STATUS_READ_ERROR;
+
+  cp->status = STATUS_READ_FAILED;
+
+  hEv = pfn_WSACreateEvent ();
+  rc = pfn_WSAEventSelect (SOCK_HANDLE (fd), hEv, FD_READ);
+  if (rc != SOCKET_ERROR)
+    {
+      do
+        {
+          rc = WaitForSingleObject (hEv, 500);
+          Sleep (5);
+        } while (rc == WAIT_TIMEOUT
+                 && cp->status != STATUS_READ_ERROR
+                 && cp->char_avail);
+      pfn_WSAEventSelect (SOCK_HANDLE (fd), NULL, 0);
+      if (rc == WAIT_OBJECT_0)
+        cp->status = STATUS_READ_SUCCEEDED;
+    }
+  pfn_WSACloseEvent (hEv);
+
+  return cp->status;
+}
+#endif
+
 int
 _sys_wait_connect (int fd)
 {
@@ -8923,10 +8962,16 @@ sys_read (int fd, char * buffer, unsigned int count)
              return -1;
 
            case STATUS_READ_SUCCEEDED:
-             /* consume read-ahead char */
-             *buffer++ = cp->chr;
-             count--;
-             nchars++;
+#ifdef WORKING_SELECT_EMULATION
+              /* select on sockets no longer requires a 1-byte read.  */
+              if (fd_info[fd].flags & FILE_SOCKET == 0)
+#endif
+               {
+                 /* consume read-ahead char */
+                 *buffer++ = cp->chr;
+                 count--;
+                 nchars++;
+               }
              cp->status = STATUS_READ_ACKNOWLEDGED;
              ResetEvent (cp->char_avail);
 
diff --git i/src/w32.h w/src/w32.h
index cf1dadf64c..cabe39fb6d 100644
--- i/src/w32.h
+++ w/src/w32.h
@@ -175,6 +175,9 @@ #define FILE_SERIAL             0x0800
 
 extern int _sys_read_ahead (int fd);
 extern int _sys_wait_accept (int fd);
+#ifdef WORKING_SELECT_EMULATION
+extern int _sys_wait_readable (int fd);
+#endif
 extern int _sys_wait_connect (int fd);
 
 extern HMODULE w32_delayed_load (Lisp_Object);
diff --git i/src/w32proc.c w/src/w32proc.c
index de33726905..376e49d13d 100644
--- i/src/w32proc.c
+++ w/src/w32proc.c
@@ -1225,7 +1225,12 @@ reader_thread (void *arg)
       else if (cp->fd >= 0 && (fd_info[cp->fd].flags & FILE_LISTEN) != 0)
        rc = _sys_wait_accept (cp->fd);
       else
-       rc = _sys_read_ahead (cp->fd);
+#ifdef WORKING_SELECT_EMULATION
+        if (fd_info[cp->fd].flags & FILE_SOCKET)
+          rc = _sys_wait_readable (cp->fd);
+        else
+#endif
+          rc = _sys_read_ahead (cp->fd);
 
       /* Don't bother waiting for the event if we already have been
         told to exit by delete_child.  */



reply via email to

[Prev in Thread] Current Thread [Next in Thread]