all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Alain Schneble <a.s@realize.ch>
To: Lars Ingebrigtsen <larsi@gnus.org>
Cc: Eli Zaretskii <eliz@gnu.org>, emacs-devel@gnu.org
Subject: Re: Asynchronous DNS
Date: Mon, 15 Feb 2016 01:02:07 +0100	[thread overview]
Message-ID: <86oabi6dts.fsf@realize.ch> (raw)
In-Reply-To: <87fuwv6ai4.fsf@gnus.org> (Lars Ingebrigtsen's message of "Sun, 14 Feb 2016 18:01:39 +1100")

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

Lars Ingebrigtsen <larsi@gnus.org> writes:

> The issue is losing asynchronicity.  A function that previously called
>
> (progn
>   (make-network-stream ... :nowait t)
>   (set-process-coding-system ...))
>
> would not block.  With the proposed blockers (unless we add fine-grained
> code to all the process function to only block if we haven't even done
> DNS yet), this code will block.  And that's a regression.

By the way, I tried it out with the fine-grained approach.  See patch
below.  The majority of the functions touched will block if DNS requests
are still pending.  process-send-* functions block while process is in
"connect" state.  I didn't have time to verify it with TLS connections
though, and also just very quickly with http connections and some hand
crafted scenarios...

> But, I mean, we could examine all these process functions and only have
> them block where we need them to, and things would work fine.  I don't
> really have much confidence in us being able to do so completely
> transparently, though.

There is potential to refactor at least two functions,
set-process-coding-system and set-process-window-size, to not require
blocking at all.  The former will require adaptions in
set_network_socket_coding_system, to check whether coding systems have
already been set, so that they do not get overwritten, e.g. in this
scenario:

(progn
  (make-network-stream ... :nowait t)
  (set-process-coding-system ...))

And set-process-coding-system could just jump over the call to
setup_process_coding_systems in case of a network process with pending
DNS requests (i.e. socket not yet available).

While with set-process-window-size, I asked myself if this one makes any
sense with network processes at all.  Should it signal an error or what
would be the use case to call this for a network process?

And here is the small patch I used:


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: Patch -- Make-process-functions-wait-for-DNS-completion --]
[-- Type: text/x-patch, Size: 6439 bytes --]

From 1ceb2e58aa1a7d688439aa9e4c0ed11fb0e50534 Mon Sep 17 00:00:00 2001
From: Alain Schneble <a.s@realize.ch>
Date: Mon, 15 Feb 2016 00:11:06 +0100
Subject: [PATCH] Make process functions wait for DNS completion

* src/process.c (set-process-filter, set-process-window-size,
process-contact, process-datagram-address, set-process-datagram-address,
set-network-process-option): make functions wait (block) on network
process until pending DNS requests have been processed and associated
socket initialized.

* src/process.c (process-send-region, process-send-string,
process-send-eof): make functions wait (block) while network process is
in connect state.
---
 src/process.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 84 insertions(+), 7 deletions(-)

diff --git a/src/process.c b/src/process.c
index 497b069..9816e93 100644
--- a/src/process.c
+++ b/src/process.c
@@ -284,6 +284,7 @@ static Lisp_Object chan_process[FD_SETSIZE];
 #ifdef HAVE_GETADDRINFO_A
 /* Pending DNS requests. */
 static Lisp_Object dns_processes;
+static void wait_for_socket_fds (Lisp_Object process);
 #endif
 
 /* Alist of elements (NAME . PROCESS).  */
@@ -1029,6 +1030,12 @@ The string argument is normally a multibyte string, except:
   struct Lisp_Process *p;
 
   CHECK_PROCESS (process);
+
+#ifdef HAVE_GETADDRINFO_A
+  if (NETCONN_P (process))
+    wait_for_socket_fds (process);
+#endif
+
   p = XPROCESS (process);
 
   /* Don't signal an error if the process's input file descriptor
@@ -1113,6 +1120,11 @@ DEFUN ("set-process-window-size", Fset_process_window_size,
 {
   CHECK_PROCESS (process);
 
+#ifdef HAVE_GETADDRINFO_A
+  if (NETCONN_P (process))
+    wait_for_socket_fds (process);
+#endif
+
   /* All known platforms store window sizes as 'unsigned short'.  */
   CHECK_RANGED_INTEGER (height, 0, USHRT_MAX);
   CHECK_RANGED_INTEGER (width, 0, USHRT_MAX);
@@ -1194,6 +1206,12 @@ list of keywords.  */)
   contact = XPROCESS (process)->childp;
 
 #ifdef DATAGRAM_SOCKETS
+
+#ifdef HAVE_GETADDRINFO_A
+  if (NETCONN_P (process))
+    wait_for_socket_fds (process);
+#endif
+
   if (DATAGRAM_CONN_P (process)
       && (EQ (key, Qt) || EQ (key, QCremote)))
     contact = Fplist_put (contact, QCremote,
@@ -2423,6 +2441,11 @@ DEFUN ("process-datagram-address", Fprocess_datagram_address, Sprocess_datagram_
 
   CHECK_PROCESS (process);
 
+#ifdef HAVE_GETADDRINFO_A
+  if (NETCONN_P (process))
+    wait_for_socket_fds (process);
+#endif
+
   if (!DATAGRAM_CONN_P (process))
     return Qnil;
 
@@ -2442,6 +2465,11 @@ Returns nil upon error setting address, ADDRESS otherwise.  */)
 
   CHECK_PROCESS (process);
 
+#ifdef HAVE_GETADDRINFO_A
+  if (NETCONN_P (process))
+    wait_for_socket_fds (process);
+#endif
+
   if (!DATAGRAM_CONN_P (process))
     return Qnil;
 
@@ -2610,6 +2638,10 @@ OPTION is not a supported option, return nil instead; otherwise return t.  */)
   if (!NETCONN1_P (p))
     error ("Process is not a network process");
 
+#ifdef HAVE_GETADDRINFO_A
+  wait_for_socket_fds (process);
+#endif
+
   s = p->infd;
   if (s < 0)
     error ("Process is not running");
@@ -3693,7 +3725,7 @@ usage: (make-network-process &rest ARGS)  */)
 #endif
 
 #ifdef HAVE_GETADDRINFO_A
-  if (EQ (Fplist_get (contact, QCnowait), Qdns) &&
+  if (EQ (Fplist_get (contact, QCnowait), Qt) &&
       !NILP (host))
     {
       int ret;
@@ -4650,6 +4682,24 @@ check_for_dns (Lisp_Object proc)
 
   return ip_addresses;
 }
+
+static void
+wait_for_socket_fds(Lisp_Object process)
+{
+  while (XPROCESS(process)->dns_requests)
+    {
+      wait_reading_process_output (0, 20 * 1000 * 1000, 0, 0, Qnil, NULL, 0);
+    }
+}
+
+static void
+wait_while_connecting(Lisp_Object process)
+{
+  while (EQ (Qconnect, XPROCESS(process)->status))
+    {
+      wait_reading_process_output (0, 20 * 1000 * 1000, 0, 0, Qnil, NULL, 0);
+    }
+}
 #endif /* HAVE_GETADDRINFO_A */
 
 /* This variable is different from waiting_for_input in keyboard.c.
@@ -6143,6 +6193,11 @@ Output from processes can arrive in between bunches.  */)
   if (XINT (start) < GPT && XINT (end) > GPT)
     move_gap_both (XINT (start), start_byte);
 
+#ifdef HAVE_GETADDRINFO_A
+  if (NETCONN_P (proc))
+    wait_while_connecting (proc);
+#endif
+  
   send_process (proc, (char *) BYTE_POS_ADDR (start_byte),
 		end_byte - start_byte, Fcurrent_buffer ());
 
@@ -6162,6 +6217,12 @@ Output from processes can arrive in between bunches.  */)
   Lisp_Object proc;
   CHECK_STRING (string);
   proc = get_process (process);
+
+#ifdef HAVE_GETADDRINFO_A
+  if (NETCONN_P (proc))
+    wait_while_connecting (proc);
+#endif
+
   send_process (proc, SSDATA (string),
 		SBYTES (string), string);
   return Qnil;
@@ -6576,10 +6637,17 @@ process has been transmitted to the serial port.  */)
   struct coding_system *coding = NULL;
   int outfd;
 
-  if (DATAGRAM_CONN_P (process))
+  proc = get_process (process);
+
+#ifdef HAVE_GETADDRINFO_A
+  if (NETCONN_P (proc))
+    wait_while_connecting (proc);
+#endif
+
+  if (DATAGRAM_CONN_P (proc))
     return process;
 
-  proc = get_process (process);
+  
   outfd = XPROCESS (proc)->outfd;
   if (outfd >= 0)
     coding = proc_encode_coding_system[outfd];
@@ -7030,7 +7098,14 @@ encode subprocess input.  */)
   register struct Lisp_Process *p;
 
   CHECK_PROCESS (process);
+
+#ifdef HAVE_GETADDRINFO_A
+  if (NETCONN_P (process))
+    wait_for_socket_fds (process);
+#endif
+
   p = XPROCESS (process);
+  
   if (p->infd < 0)
     error ("Input file descriptor of %s closed", SDATA (p->name));
   if (p->outfd < 0)
@@ -7067,6 +7142,12 @@ suppressed.  */)
   register struct Lisp_Process *p;
 
   CHECK_PROCESS (process);
+
+#ifdef HAVE_GETADDRINFO_A
+  if (NETCONN_P (process))
+    wait_for_socket_fds (process);
+#endif
+
   p = XPROCESS (process);
   if (NILP (flag))
     pset_decode_coding_system
@@ -7757,7 +7838,6 @@ syms_of_process (void)
   DEFSYM (QCcoding, ":coding");
   DEFSYM (QCserver, ":server");
   DEFSYM (QCnowait, ":nowait");
-  DEFSYM (Qdns, "dns");
   DEFSYM (QCsentinel, ":sentinel");
   DEFSYM (QCtls_parameters, ":tls-parameters");
   DEFSYM (QClog, ":log");
@@ -7921,9 +8001,6 @@ The variable takes effect when `start-process' is called.  */);
 
 #ifdef NON_BLOCKING_CONNECT
    ADD_SUBFEATURE (QCnowait, Qt);
-#ifdef HAVE_GETADDRINFO_A
-   ADD_SUBFEATURE (QCnowait, Qdns);
-#endif
 #endif
 #ifdef DATAGRAM_SOCKETS
    ADD_SUBFEATURE (QCtype, Qdatagram);
-- 
2.6.2.windows.1


  parent reply	other threads:[~2016-02-15  0:02 UTC|newest]

Thread overview: 190+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-01-23 13:50 Asynchronous DNS Lars Magne Ingebrigtsen
2016-01-23 14:56 ` Elias Mårtenson
2016-01-24 21:17   ` Lars Magne Ingebrigtsen
2016-01-25  8:58     ` Elias Mårtenson
2016-01-25 15:51       ` Eli Zaretskii
2016-01-25 17:15         ` Elias Mårtenson
2016-01-25 17:26           ` Eli Zaretskii
2016-01-23 21:42 ` Paul Eggert
2016-01-24 13:53   ` Lars Magne Ingebrigtsen
2016-01-24 14:01     ` Lars Magne Ingebrigtsen
2016-01-24 14:14       ` Lars Magne Ingebrigtsen
2016-01-24 19:58         ` Paul Eggert
2016-01-24  4:49 ` Stefan Monnier
2016-01-24 13:56   ` Lars Magne Ingebrigtsen
2016-01-24 17:16     ` Lars Magne Ingebrigtsen
2016-01-25  2:06       ` Stefan Monnier
2016-01-26 19:48 ` John Wiegley
2016-01-26 22:05 ` Florian Weimer
2016-01-30  0:11 ` Lars Ingebrigtsen
2016-01-30  2:39   ` Alex Dunn
2016-01-30  2:58     ` Lars Ingebrigtsen
2016-01-30  4:11       ` Alex Dunn
2016-01-30  4:34         ` Lars Ingebrigtsen
2016-02-01  3:55           ` Lars Ingebrigtsen
2016-01-30  7:23   ` Eli Zaretskii
2016-01-30  7:46     ` Lars Ingebrigtsen
2016-01-30  8:46       ` Eli Zaretskii
2016-01-30 22:46         ` Lars Ingebrigtsen
2016-01-31  6:12       ` Ken Raeburn
2016-02-01  2:46         ` Lars Ingebrigtsen
2016-01-31 14:03   ` Andy Moreton
2016-02-01  2:08     ` Lars Ingebrigtsen
2016-02-01  2:36       ` Lars Ingebrigtsen
2016-02-01 18:51         ` Eli Zaretskii
2016-02-02  1:15           ` Lars Ingebrigtsen
2016-02-02  3:38             ` Eli Zaretskii
2016-02-02  3:48               ` Lars Ingebrigtsen
2016-02-02 13:25                 ` Stefan Monnier
2016-02-02 16:24                   ` Eli Zaretskii
2016-02-02 17:11                     ` Stefan Monnier
2016-02-02 16:12                 ` Eli Zaretskii
2016-02-03  0:42                   ` Lars Ingebrigtsen
2016-02-03 15:49                     ` Eli Zaretskii
2016-02-04  2:22                       ` Lars Ingebrigtsen
2016-02-04  8:18                         ` Alain Schneble
2016-02-04  8:55                           ` Lars Ingebrigtsen
2016-02-04 10:13                             ` Alain Schneble
2016-02-05  2:08                               ` Lars Ingebrigtsen
2016-02-05  7:19                                 ` Eli Zaretskii
2016-02-05  7:32                                   ` Lars Ingebrigtsen
2016-02-13 23:47                                     ` Alain Schneble
2016-02-14  2:22                                       ` Lars Ingebrigtsen
2016-02-14 11:19                                         ` Alain Schneble
2016-02-14 16:40                                           ` Eli Zaretskii
2016-02-16 21:37                                             ` Alain Schneble
2016-02-20  1:28                                               ` Lars Ingebrigtsen
2016-02-20 17:55                                                 ` Paul Eggert
2016-02-20 20:18                                                   ` Alain Schneble
2016-02-20 20:33                                                     ` Paul Eggert
2016-02-21 10:29                                                       ` Alain Schneble
2016-02-26 23:54                                                         ` YAMAMOTO Mitsuharu
2016-02-27  3:57                                                           ` Lars Ingebrigtsen
2016-02-27  9:22                                                             ` Alain Schneble
2016-02-28  4:43                                                               ` Lars Ingebrigtsen
2016-02-28  9:44                                                                 ` Andreas Schwab
2016-02-29  2:59                                                                   ` Lars Ingebrigtsen
2016-02-29  3:30                                                                     ` Lars Ingebrigtsen
2016-02-29  8:41                                                                       ` Andreas Schwab
2016-02-20 19:31                                                 ` Alain Schneble
2016-02-20 19:57                                                   ` Alain Schneble
2016-02-21  2:36                                                     ` Lars Ingebrigtsen
2016-02-21  3:03                                                       ` Lars Ingebrigtsen
2016-02-21 18:55                                                         ` Alain Schneble
2016-02-21 20:23                                                           ` Eli Zaretskii
2016-02-22  2:15                                                             ` Lars Ingebrigtsen
2016-02-22  3:39                                                               ` Eli Zaretskii
2016-02-22  4:03                                                                 ` Lars Ingebrigtsen
2016-02-22  5:45                                                                   ` Lars Ingebrigtsen
2016-02-22  2:07                                                           ` Lars Ingebrigtsen
2016-02-24  6:26                                                             ` Lars Ingebrigtsen
2016-02-24  6:29                                                               ` Paul Eggert
2016-02-24  6:52                                                                 ` Lars Ingebrigtsen
2016-02-24  8:27                                                               ` John Wiegley
2016-02-21 19:35                                                         ` Alain Schneble
2016-02-22  2:01                                                           ` Lars Ingebrigtsen
2016-02-20 10:57                                               ` Eli Zaretskii
2016-02-16  2:09                                       ` Lars Ingebrigtsen
2016-02-05  9:37                                   ` Alain Schneble
2016-02-04 16:30                         ` Eli Zaretskii
2016-02-05  2:31                           ` Lars Ingebrigtsen
2016-02-05  7:20                             ` Eli Zaretskii
2016-02-03  2:49                   ` Lars Ingebrigtsen
2016-02-02  6:41             ` Alain Schneble
2016-02-02  7:06               ` Lars Ingebrigtsen
2016-02-02  7:49                 ` Alain Schneble
2016-02-02 21:27                   ` Alain Schneble
2016-02-03  0:22                   ` Lars Ingebrigtsen
2016-02-03 10:22                     ` Yuri Khan
2016-02-04  0:18                       ` Lars Ingebrigtsen
2016-02-01 11:58       ` Andy Moreton
2016-02-01 19:10         ` Eli Zaretskii
2016-02-01 22:18           ` Andy Moreton
2016-02-02  1:54         ` Lars Ingebrigtsen
2016-02-02  2:05           ` YAMAMOTO Mitsuharu
2016-02-02  2:18             ` Lars Ingebrigtsen
2016-02-02  3:42           ` Eli Zaretskii
2016-02-03  0:50             ` Lars Ingebrigtsen
2016-02-03  2:43               ` Lars Ingebrigtsen
2016-02-03 15:50               ` Eli Zaretskii
2016-02-04  2:25                 ` Lars Ingebrigtsen
2016-02-04 16:31                   ` Eli Zaretskii
2016-02-05  2:32                     ` Lars Ingebrigtsen
2016-02-05  7:21                       ` Eli Zaretskii
2016-02-05  7:33                         ` Lars Ingebrigtsen
2016-02-06  7:49                           ` Lars Ingebrigtsen
2016-02-06  8:19                             ` Eli Zaretskii
2016-02-07  0:34                               ` Alain Schneble
2016-02-07  1:38                                 ` Lars Ingebrigtsen
2016-02-07 11:41                                   ` Alain Schneble
2016-02-07 19:16                                     ` Eli Zaretskii
2016-02-07 20:24                                       ` Alain Schneble
2016-02-08  1:55                                     ` Lars Ingebrigtsen
2016-02-08  3:40                                       ` Lars Ingebrigtsen
2016-02-08  7:40                                       ` Alain Schneble
2016-02-08  7:52                                         ` Lars Ingebrigtsen
2016-02-08  8:10                                           ` Alain Schneble
2016-02-09  0:37                                             ` Lars Ingebrigtsen
2016-02-09  9:02                                               ` Alain Schneble
2016-02-09 13:46                                                 ` Lars Ingebrigtsen
2016-02-09 17:00                                                 ` Eli Zaretskii
2016-02-09 20:43                                                   ` Alain Schneble
2016-02-09 20:48                                                     ` Eli Zaretskii
2016-02-09 23:22                                                     ` Lars Ingebrigtsen
2016-02-10 10:39                                                       ` Alain Schneble
2016-02-12  2:15                                                         ` Lars Ingebrigtsen
2016-02-12 10:12                                                           ` Alain Schneble
2016-02-13  4:04                                                             ` Lars Ingebrigtsen
2016-02-13 10:16                                                               ` Alain Schneble
2016-02-14  2:20                                                                 ` Lars Ingebrigtsen
2016-02-14  5:56                                                                   ` Eli Zaretskii
2016-02-14  7:01                                                                     ` Lars Ingebrigtsen
2016-02-14 13:56                                                                       ` Stefan Monnier
2016-02-15  0:19                                                                         ` Alain Schneble
2016-02-15  4:22                                                                         ` Lars Ingebrigtsen
2016-02-14 16:32                                                                       ` Eli Zaretskii
2016-02-15  0:14                                                                         ` Alain Schneble
2016-02-15  3:41                                                                           ` Eli Zaretskii
2016-02-15  4:30                                                                             ` Lars Ingebrigtsen
2016-02-15  0:02                                                                       ` Alain Schneble [this message]
2016-02-15  4:39                                                                         ` Lars Ingebrigtsen
2016-02-15  6:14                                                                           ` Lars Ingebrigtsen
2016-02-15  6:25                                                                             ` Lars Ingebrigtsen
2016-02-15 10:55                                                                             ` Eli Zaretskii
2016-02-15 12:01                                                                               ` Andreas Schwab
2016-02-15 13:50                                                                                 ` Eli Zaretskii
2016-02-16  2:26                                                                               ` Lars Ingebrigtsen
2016-02-15 10:50                                                                           ` Eli Zaretskii
2016-02-15 11:06                                                                             ` Alain Schneble
2016-02-15 13:49                                                                               ` Eli Zaretskii
2016-02-15 15:04                                                                                 ` Alain Schneble
2016-02-15 16:40                                                                                   ` Alain Schneble
2016-02-16  2:13                                                                                     ` Lars Ingebrigtsen
2016-02-16  6:48                                                                                       ` Alain Schneble
2016-02-15 18:13                                                                           ` Alain Schneble
2016-02-12 10:35                                                           ` Andreas Schwab
2016-02-12 11:37                                                             ` Alain Schneble
2016-02-08 18:11                                           ` Eli Zaretskii
2016-02-09  0:47                                             ` Lars Ingebrigtsen
2016-02-09 16:56                                               ` Eli Zaretskii
2016-02-08 10:43                                       ` Andreas Schwab
2016-02-08 11:55                                         ` Alain Schneble
2016-02-08 12:55                                           ` Andreas Schwab
2016-02-08 14:25                                             ` Alain Schneble
2016-02-08 14:31                                               ` Andreas Schwab
2016-02-09  0:40                                                 ` Lars Ingebrigtsen
2016-02-09  9:15                                                   ` Alain Schneble
2016-02-09  9:35                                                     ` Alain Schneble
2016-02-07 15:55                                 ` Eli Zaretskii
2016-02-07 17:45                                   ` Alain Schneble
2016-02-08  2:03                                   ` Lars Ingebrigtsen
2016-02-08 15:56                                     ` John Wiegley
2016-02-08 20:30                                       ` Rasmus
2016-02-09  0:34                                       ` Lars Ingebrigtsen
2016-02-08 18:22                                     ` Eli Zaretskii
2016-02-07  1:35                               ` Lars Ingebrigtsen
2016-02-07 16:07                                 ` Eli Zaretskii
2016-02-08  2:05                                   ` Lars Ingebrigtsen
2016-02-08 18:20                                     ` Eli Zaretskii
2016-02-07 17:27                             ` John Wiegley
2016-02-08  1:26                               ` Lars Ingebrigtsen

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

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

  git send-email \
    --in-reply-to=86oabi6dts.fsf@realize.ch \
    --to=a.s@realize.ch \
    --cc=eliz@gnu.org \
    --cc=emacs-devel@gnu.org \
    --cc=larsi@gnus.org \
    /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 external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.