unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Robert Pluim <rpluim@gmail.com>
To: Alex Matei <matei.alexandru@live.com>
Cc: Eli Zaretskii <eliz@gnu.org>,
	"45821@debbugs.gnu.org" <45821@debbugs.gnu.org>
Subject: bug#45821: Emacs UDP support on Windows
Date: Tue, 03 Jan 2023 09:51:11 +0100	[thread overview]
Message-ID: <87tu18j91c.fsf@gmail.com> (raw)
In-Reply-To: <DB9PR02MB73371FB353278A3F6AA9CD6C85F79@DB9PR02MB7337.eurprd02.prod.outlook.com> (Alex Matei's message of "Mon, 2 Jan 2023 22:56:04 +0000")

[-- Attachment #1: Type: text/plain, Size: 2707 bytes --]

>>>>> On Mon, 2 Jan 2023 22:56:04 +0000, Alex Matei <matei.alexandru@live.com> said:

    Alex> Thanks for the tips with logging!
    Alex> I think I found the fix (at least for one of the issues?)
    Alex> Please see the attached patch/diff for the fix.

    Alex> The code will always ignore (aka ‘eat’) one character if the file
    Alex> descriptor had the least significant bit set (0x1, so this would mean
    Alex> for any file descriptor with READ flag set)
    Alex> [cid:image004.png@01D91EBA.5498F140]

Hmm, my local version already had that fixed, so I guess I posted the
wrong patch to the bug. But I still think you found a fix (see below)

    Alex> Based on operator precedence rules, FILE_SOCKET == 0 will be evaluated
    Alex> first (before &) , resulting in 0, which would effectively cause the
    Alex> condition to always be false

    Alex> This behavior can be observed when executing a call to
    Alex> ‘(async-shell-command)’ (-> file descriptor flags 0x191) on a build
    Alex> with the patch vs one without the patch applied (see below, attached
    Alex> image)

    Alex>   * My suspicion is that this behavior is similar to what TLS was
    Alex> experiencing but just easier to reproduce (note: there might other
    Alex> issues with TLS , but so far, after applying this fix I have not run
    Alex> into any other issues)

    Alex> [cid:image005.png@01D91EBA.5498F140]


    Alex> @Robert Pluim<mailto:rpluim@gmail.com> I don’t know if this was the
    Alex> only missing piece, but from all my tests I couldn’t see any issues
    Alex> with TLS anymore, and with navigating https://gnu.org in EWW..
    Alex> I wanted to thank you for creating this patch 🙏, and giving me pointers on how to apply and debug it.

Iʼd run the networking portion of our test suite to see if everything
works as intended (you might need to install the GnuTLS cli tools). eg:

    cd test
    make network-stream-tests
    
    Alex> p.s.
    Alex> - The patch also adds STATUS_READ_IN_PROGRESS state for the new
    Alex> ‘_sys_wait_readable’ function , based on what the read ahead logic was
    Alex> doing already (idk if it is needed, but it is a nice mirror, and a
    Alex> good status code to reflect, although I am not convinced anyone cares
    Alex> about this state transition..)

I think this is actually the fix, since I added it locally and TLS
started working, but I donʼt understand the code enough to be definite
about that. More testing required 😀

Can you post the full patch just to ensure weʼre all talking about the
same changes? Iʼve attached what Iʼm working from

Robert
-- 


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Make-UDP-sockets-work-on-Windows.patch --]
[-- Type: text/x-diff, Size: 5046 bytes --]

From 2ae91ed495f3972ddac383bd5f63c47946d5cdb5 Mon Sep 17 00:00:00 2001
From: Robert Pluim <rpluim@gmail.com>
Date: Mon, 27 Sep 2021 17:25:27 +0200
Subject: [PATCH] Make UDP sockets work on Windows
To: emacs-devel@gnu.org

* admin/CPP-DEFINES: remove BROKEN_DATAGRAM_SOCKETS
* nt/inc/ms-w32.h: remove BROKEN_DATAGRAM_SOCKETS.
* src/process.c (DATAGRAM_SOCKETS): remove dependency on
!BROKEN_DATAGRAM_SOCKETS.
* src/w32.c (_sys_wait_readable): New function.  Calls
pfn_WSAEventSelect for sockets to see if data can be read, so the
1-byte readahead is no longer necessary.
(sys_read): Only do 1-byte readahead for non-sockets.
* src/w32.h: Add prototype for _sys_wait_readable.
* src/w32proc.c (reader_thread): Call _sys_wait_readable for socket
handles.
---
 admin/CPP-DEFINES |  1 -
 nt/inc/ms-w32.h   |  4 ----
 src/process.c     |  8 +++-----
 src/w32.c         | 49 +++++++++++++++++++++++++++++++++++++++++++----
 src/w32.h         |  1 +
 src/w32proc.c     |  2 ++
 6 files changed, 51 insertions(+), 14 deletions(-)

diff --git a/admin/CPP-DEFINES b/admin/CPP-DEFINES
index 06986ec8f48..2704bc57675 100644
--- a/admin/CPP-DEFINES
+++ b/admin/CPP-DEFINES
@@ -103,7 +103,6 @@ getting at the full user name.  Only MSDOS overrides the default.
 anymore, so they can be removed.
 
 AMPERSAND_FULL_NAME
-BROKEN_DATAGRAM_SOCKETS
 BROKEN_GET_CURRENT_DIR_NAME
 BROKEN_PTY_READ_AFTER_EAGAIN
 DEFAULT_SOUND_DEVICE
diff --git a/nt/inc/ms-w32.h b/nt/inc/ms-w32.h
index 58be1199345..535d24cd57b 100644
--- a/nt/inc/ms-w32.h
+++ b/nt/inc/ms-w32.h
@@ -89,10 +89,6 @@ #define _CALLBACK_ __cdecl
    Look in <sys/time.h> for a timeval structure.  */
 #define HAVE_TIMEVAL 1
 
-/* Our select emulation does 1-byte read-ahead waiting for received
-   packets, so datagrams are broken.  */
-#define BROKEN_DATAGRAM_SOCKETS 1
-
 #define MAIL_USE_SYSTEM_LOCK 1
 
 /* Define to 1 if GCC-style __attribute__ ((__aligned__ (expr))) works. */
diff --git a/src/process.c b/src/process.c
index 67d1d3e425f..8f7fccfe538 100644
--- a/src/process.c
+++ b/src/process.c
@@ -234,11 +234,9 @@ #define PIPECONN1_P(p) (EQ (p->type, Qpipe))
    "non-destructive" select.  So we require either native select,
    or emulation of select using FIONREAD.  */
 
-#ifndef BROKEN_DATAGRAM_SOCKETS
-# if defined HAVE_SELECT || defined USABLE_FIONREAD
-#  if defined HAVE_SENDTO && defined HAVE_RECVFROM && defined EMSGSIZE
-#   define DATAGRAM_SOCKETS
-#  endif
+#if defined HAVE_SELECT || defined USABLE_FIONREAD
+# if defined HAVE_SENDTO && defined HAVE_RECVFROM && defined EMSGSIZE
+#  define DATAGRAM_SOCKETS
 # endif
 #endif
 
diff --git a/src/w32.c b/src/w32.c
index 47d79abc5b0..ef7d3de861b 100644
--- a/src/w32.c
+++ b/src/w32.c
@@ -8940,6 +8940,43 @@ _sys_wait_accept (int fd)
   return cp->status;
 }
 
+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;
+}
+
 int
 _sys_wait_connect (int fd)
 {
@@ -9065,10 +9102,14 @@ sys_read (int fd, char * buffer, unsigned int count)
 	      return -1;
 
 	    case STATUS_READ_SUCCEEDED:
-	      /* consume read-ahead char */
-	      *buffer++ = cp->chr;
-	      count--;
-	      nchars++;
+              /* select on sockets no longer requires a 1-byte read.  */
+              if ((fd_info[fd].flags & FILE_SOCKET) == 0)
+		{
+		  /* consume read-ahead char */
+		  *buffer++ = cp->chr;
+		  count--;
+		  nchars++;
+		}
 	      cp->status = STATUS_READ_ACKNOWLEDGED;
 	      ResetEvent (cp->char_avail);
 
diff --git a/src/w32.h b/src/w32.h
index a3d0b75359a..4eba0fabe94 100644
--- a/src/w32.h
+++ b/src/w32.h
@@ -177,6 +177,7 @@ #define FILE_DONT_CLOSE         0x1000
 
 extern int _sys_read_ahead (int fd);
 extern int _sys_wait_accept (int fd);
+extern int _sys_wait_readable (int fd);
 extern int _sys_wait_connect (int fd);
 
 extern HMODULE w32_delayed_load (Lisp_Object);
diff --git a/src/w32proc.c b/src/w32proc.c
index 77a4ac1ff7e..19d9fc7d0e7 100644
--- a/src/w32proc.c
+++ b/src/w32proc.c
@@ -1243,6 +1243,8 @@ reader_thread (void *arg)
 	rc = _sys_wait_connect (fd);
       else if (fd >= 0 && (fd_info[fd].flags & FILE_LISTEN) != 0)
 	rc = _sys_wait_accept (fd);
+      else if (fd_info[fd].flags & FILE_SOCKET)
+	rc = _sys_wait_readable (fd);
       else
 	rc = _sys_read_ahead (fd);
 
-- 
2.38.1.420.g319605f8f0


  reply	other threads:[~2023-01-03  8:51 UTC|newest]

Thread overview: 70+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-12 18:08 bug#45821: 28.0.50; Add UDP support for Emacs on Windows Lars Ingebrigtsen
2021-01-12 18:38 ` Eli Zaretskii
2021-01-13  9:17   ` Robert Pluim
2021-07-20 15:41     ` Lars Ingebrigtsen
2021-09-27 16:47       ` Robert Pluim
2021-09-27 17:45         ` Robert Pluim
2021-10-04 17:47           ` Robert Pluim
2021-10-04 17:59             ` Eli Zaretskii
2021-09-27 18:45         ` Eli Zaretskii
2021-10-11 11:56           ` Stefan Kangas
2021-10-11 12:06             ` Robert Pluim
2022-05-03 18:56 ` bug#45821: Emacs UDP support " Alex Matei
2022-05-04  7:35   ` Robert Pluim
2022-05-04 11:50     ` Alex Matei
2022-05-04 12:32       ` Robert Pluim
2022-09-11 11:26         ` bug#45821: 28.0.50; Add UDP support for Emacs " Lars Ingebrigtsen
2023-01-01 23:01         ` bug#45821: Emacs UDP support " Alex Matei
2023-01-02  0:47           ` Alex Matei
2023-01-02 10:22             ` Robert Pluim
2023-01-02 12:41               ` Eli Zaretskii
2023-01-02 13:29                 ` Robert Pluim
2023-01-02 13:38                   ` Eli Zaretskii
2023-01-02 22:56                     ` Alex Matei
2023-01-03  8:51                       ` Robert Pluim [this message]
2023-01-03 20:22                         ` Alex Matei
2023-01-04  9:32                           ` Robert Pluim
2023-01-04 10:15                             ` Alex Matei
2023-01-04 10:50                               ` Robert Pluim
2023-01-05 19:06                                 ` Alex Matei
2023-01-05 20:53                                   ` Alex Matei
2023-01-05 21:01                                     ` Alex Matei
2023-01-06  7:56                                       ` Robert Pluim
2023-01-07 11:24                                         ` Robert Pluim
2023-01-08 15:31                                           ` Alex Matei
2023-01-08 15:42                                             ` Robert Pluim
2023-01-08 15:43                                               ` Alex Matei
2023-01-08 16:00                                                 ` Alex Matei
2023-01-08 16:08                                                   ` Eli Zaretskii
2023-01-08 16:10                                                     ` Alex Matei
2023-01-10 12:41                                                       ` Alex Matei
2023-01-10 13:56                                                         ` Robert Pluim
2023-01-11 13:09                                                           ` Alex Matei
2023-01-11 13:23                                                             ` Alex Matei
2023-01-12  9:22                                                               ` Robert Pluim
2023-01-12 10:08                                                                 ` Eli Zaretskii
2023-01-12 10:14                                                                   ` Alex Matei
2023-01-12 10:24                                                                   ` Robert Pluim
2023-01-12 10:46                                                                     ` Eli Zaretskii
2023-01-12 10:57                                                                       ` Alex Matei
2023-01-12 10:59                                                                         ` Alex Matei
2023-01-12 11:03                                                                         ` Eli Zaretskii
2023-01-12 11:12                                                                           ` Alex Matei
2023-01-12 11:21                                                                             ` Robert Pluim
2023-01-12 11:23                                                                               ` Alex Matei
2023-01-12 11:18                                                                       ` Robert Pluim
2023-01-12 12:25                                                                         ` Eli Zaretskii
2023-01-12 13:28                                                                           ` Robert Pluim
2023-01-28 21:17                                                                             ` Alex Matei
2023-01-29  6:13                                                                               ` Eli Zaretskii
2023-01-02 10:10           ` Robert Pluim
2023-01-02 16:01             ` Alex Matei
2023-01-03 13:30               ` Po Lu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-01-03 19:18                 ` Alex Matei
2023-01-02 12:07           ` Eli Zaretskii
2023-01-02 15:59             ` Alex Matei
2023-01-02 17:01               ` Eli Zaretskii
2023-01-02 17:57                 ` Alex Matei
2023-01-02 19:07                   ` Alex Matei
2023-01-02 19:22                     ` Eli Zaretskii
2023-01-02 19:24                       ` Alex Matei

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87tu18j91c.fsf@gmail.com \
    --to=rpluim@gmail.com \
    --cc=45821@debbugs.gnu.org \
    --cc=eliz@gnu.org \
    --cc=matei.alexandru@live.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).