From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Robert Pluim Newsgroups: gmane.emacs.bugs Subject: bug#45821: Emacs UDP support on Windows Date: Tue, 03 Jan 2023 09:51:11 +0100 Message-ID: <87tu18j91c.fsf@gmail.com> References: <87turmhvub.fsf@gnus.org> <87ee1992d5.fsf@gmail.com> <87a6bx8olp.fsf@gmail.com> <877cy5kzhw.fsf@gmail.com> <83wn651543.fsf@gnu.org> <87y1qljc8h.fsf@gmail.com> <83pmbx12gf.fsf@gnu.org> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="25173"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Eli Zaretskii , "45821@debbugs.gnu.org" <45821@debbugs.gnu.org> To: Alex Matei Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Tue Jan 03 09:53:22 2023 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pCd2w-0006OC-0e for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 03 Jan 2023 09:53:22 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pCd1g-0003s1-Rk; Tue, 03 Jan 2023 03:52:04 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pCd1e-0003r4-Qx for bug-gnu-emacs@gnu.org; Tue, 03 Jan 2023 03:52:02 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pCd1e-0008Cp-HW for bug-gnu-emacs@gnu.org; Tue, 03 Jan 2023 03:52:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pCd1e-00081m-BE for bug-gnu-emacs@gnu.org; Tue, 03 Jan 2023 03:52:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Robert Pluim Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 03 Jan 2023 08:52:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 45821 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 45821-submit@debbugs.gnu.org id=B45821.167273588330797 (code B ref 45821); Tue, 03 Jan 2023 08:52:02 +0000 Original-Received: (at 45821) by debbugs.gnu.org; 3 Jan 2023 08:51:23 +0000 Original-Received: from localhost ([127.0.0.1]:44673 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pCd10-00080d-Ty for submit@debbugs.gnu.org; Tue, 03 Jan 2023 03:51:23 -0500 Original-Received: from mail-wm1-f50.google.com ([209.85.128.50]:52772) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pCd0x-00080O-SM for 45821@debbugs.gnu.org; Tue, 03 Jan 2023 03:51:21 -0500 Original-Received: by mail-wm1-f50.google.com with SMTP id ay40so22169800wmb.2 for <45821@debbugs.gnu.org>; Tue, 03 Jan 2023 00:51:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:message-id:date:references:in-reply-to:subject:cc:to :from:from:to:cc:subject:date:message-id:reply-to; bh=ZcBxwZraFXKW4PrrU3HzQ2VkqXcmZIr0nLj0NMjbyeE=; b=d8P/L6QC9Ntlc3QR1KkpQw6kvC1n3KedXHj3xLmK/Ytp2PI2J3FhO/+R2snkmZVj4Z v0wVciCN0okM/XAV00CwFc7CxzbCKcTwRtwOyfxLSZp4dGhdB7cgknGGx3wtRD000oRj JpnIGlX6ZQYb8HHARPKVfTuyZTrZVtQHfX6UJ2B93j1R54YslNmzdO9+wanUx0/w1tSM hlckfzuj6aRe8xdccmF6QQ/lWDOIVtp8nGOgYiE+bHr4Rh1b2OtwKp4YyviLwfOZBql5 JsRNfBMjTns4QWuhfH5Y0sJOX5ZrtJigZuEk1AcXlDzns9v3OAqsBCYWkb//4z9TnCLT 5/NQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=mime-version:message-id:date:references:in-reply-to:subject:cc:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ZcBxwZraFXKW4PrrU3HzQ2VkqXcmZIr0nLj0NMjbyeE=; b=XiGABkO8Vr7nLg4RAzEH0wNg8ZinQfLtwFLP3WoKrSB5420KG/fM0KX2K0VW1cQ386 uMrFTAWGbOXaRFDsbMcEpOmO0NkpxwTs4MbRhMlc001oDG/MJNz1tQjXFjIMS8tFhrYM Le7/YhC1aH43RgF4HLaPYuYQ2lom6LSdOVS1utTebEZAge2CCcUOxoju/3cOEUeGqFrM huzdMpvnZ5sav1jZgL+Vx0M0ld5Bkytd7msCBBSPtVDcx3DjxsTe61Ztn2KdmNc619g/ y8pqs4sFYb+/14QDvrc5SqF7OIHtSA2Efd4fvx+M3JAiev3ejDng9Q7TXFlDMaoKOwpW dGmA== X-Gm-Message-State: AFqh2kqjHRThc+xTU40PeapSXOYud759FZNLyLuzayFAB4JFcAYyWkqy r9ZZwnPGIVnPOi8QX4xxjhZpfohdT7w= X-Google-Smtp-Source: AMrXdXuQEU2jVyGDeyno4vbqZcgRyYB2er44dtBcO8NSzmXdzj0195fGjXsNGe+5tfS+euv/400MTg== X-Received: by 2002:a05:600c:4e04:b0:3d2:381f:2db5 with SMTP id b4-20020a05600c4e0400b003d2381f2db5mr29868769wmq.22.1672735872854; Tue, 03 Jan 2023 00:51:12 -0800 (PST) Original-Received: from rltb ([82.66.8.55]) by smtp.gmail.com with ESMTPSA id k1-20020a5d5181000000b0024207478de3sm30486445wrv.93.2023.01.03.00.51.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Jan 2023 00:51:12 -0800 (PST) In-Reply-To: (Alex Matei's message of "Mon, 2 Jan 2023 22:56:04 +0000") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:252380 Archived-At: --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable >>>>> On Mon, 2 Jan 2023 22:56:04 +0000, Alex Matei 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 =E2=80=98eat=E2=80=99) one chara= cter 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 =3D=3D 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> =E2=80=98(async-shell-command)=E2=80=99 (-> file descriptor flags= 0x191) on a build Alex> with the patch vs one without the patch applied (see below, attac= hed 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 I don=E2=80=99t know if th= is was the Alex> only missing piece, but from all my tests I couldn=E2=80=99t 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 =F0=9F=99=8F, and g= iving me pointers on how to apply and debug it. I=CA=BCd 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 =20=20=20=20 Alex> p.s. Alex> - The patch also adds STATUS_READ_IN_PROGRESS state for the new Alex> =E2=80=98_sys_wait_readable=E2=80=99 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 c= ares Alex> about this state transition..) I think this is actually the fix, since I added it locally and TLS started working, but I don=CA=BCt understand the code enough to be definite about that. More testing required =F0=9F=98=80 Can you post the full patch just to ensure we=CA=BCre all talking about the same changes? I=CA=BCve attached what I=CA=BCm working from Robert --=20 --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=0001-Make-UDP-sockets-work-on-Windows.patch >From 2ae91ed495f3972ddac383bd5f63c47946d5cdb5 Mon Sep 17 00:00:00 2001 From: Robert Pluim 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 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 --=-=-=--