Subject: bug#22789: 25.1.50; In last master build https connections stop working

From: Alain Schneble <[email-masked]>
Date: Fri, 4 Mar 2016 22:36:56 +0100

Alain Schneble <[email-masked]> writes:

> Eli Zaretskii <[email-masked]> writes:
>
>>> Date: Tue, 01 Mar 2016 18:05:36 +0200
>>> From: Eli Zaretskii <[email-masked]>
>>> Cc: [email-masked], [email-masked], [email-masked]
>>> 
>>> IOW, let's return to the w32-specific issues when the dust settles on
>>> the Posix code.
>>
>> It sounds like that part happened already, so do you still see any
>> w32-specific issues with this?
>
> Sorry for the delay.  It seems like there are still some issues, at
> least on my system and even without any debugger attached.  I'm
> currently trying to find the cause...

I have the impression that GnuTLS doesn't like it too much if we start
retrying the handshake many times before the socket is connected.  At
least on MS-Windows.  In nearly all of the cases of loading websites
with around 20 images, I observe arbitrary failures of
gnutls_try_handshake which usually end up with -10
GNUTLS_E_INVALID_SESSION.

I believe this because the following patch solves the issue on my
MS-Windows system: Postponing the handshake until after the socket is
connected.  Still, I must be honest: I'm in a kind of a trial-and-error
mode.  I do not really understand all the aspects of the current
implementation.  Anyway, I think a change in that direction would
probably be a good thing.  Do you agree?  It eliminates all the
handshake-retries that would otherwise happen before the socket is
connected.

I wonder if the issues that you observed with gdb attached would go away
with this patch as well...  You had these issues under GNU/Linux, right?
It's a bit embarrassing, but I did not yet have time to learn how to use
gdb to debug Emacs.  (But its on my todo list.)  Otherwise I would have
tried it out quickly.

BTW, `libgnutls-version' evaluates to 30408 on my MS-Windows.

And here is the intermediate-and-not-cleaned-up patch:

>From cefaba56a33046b588eab81b3ca58224830a44f9 Mon Sep 17 00:00:00 2001
From: Alain Schneble <[email-masked]>
Date: Fri, 4 Mar 2016 21:51:31 +0100
Subject: [PATCH] Wait for GnuTLS handshake until socket is connected

* src/gnutls.c (emacs_gnutls_handshake, gnutls_try_handshake): Skip
GnuTLS handshake when gnutls_boot is called on async socket (aka non
blocking client).

* src/process.c (connect_network_socket, wait_reading_process_output):
Proceed with GnuTLS handshake only after async socket has been
connected.
---
 src/gnutls.c  | 11 ++++++++---
 src/process.c |  9 ++++++---
 2 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/src/gnutls.c b/src/gnutls.c
index 988c010..ddf4648 100644
--- a/src/gnutls.c
+++ b/src/gnutls.c
@@ -403,9 +403,6 @@ gnutls_try_handshake (struct Lisp_Process *proc)
   gnutls_session_t state = proc->gnutls_state;
   int ret;
 
-  if (proc->is_non_blocking_client)
-    proc->gnutls_p = true;
-
   do
     {
       ret = gnutls_handshake (state);
@@ -426,6 +423,8 @@ gnutls_try_handshake (struct Lisp_Process *proc)
     {
       /* check_memory_full (gnutls_alert_send_appropriate (state, ret));  */
     }
+
+  //printf ("gnutls_try_handshake: proc fd=%d, ret=%d\n", proc->infd, ret);
   return ret;
 }
 
@@ -474,6 +473,12 @@ emacs_gnutls_handshake (struct Lisp_Process *proc)
       proc->gnutls_initstage = GNUTLS_STAGE_TRANSPORT_POINTERS_SET;
     }
 
+  if (proc->is_non_blocking_client)
+    {
+      proc->gnutls_p = true;      
+      return GNUTLS_E_AGAIN;
+    }
+
   return gnutls_try_handshake (proc);
 }
 
diff --git a/src/process.c b/src/process.c
index 4359f68..bd1c45f 100644
--- a/src/process.c
+++ b/src/process.c
@@ -3415,7 +3415,8 @@ connect_network_socket (Lisp_Object proc, Lisp_Object ip_addresses)
       if (p->gnutls_initstage == GNUTLS_STAGE_READY)
 	/* Run sentinels, etc. */
 	finish_after_tls_connection (proc);
-      else if (p->gnutls_initstage != GNUTLS_STAGE_HANDSHAKE_TRIED)
+      else if ((! p->is_non_blocking_client && p->gnutls_initstage != GNUTLS_STAGE_HANDSHAKE_TRIED) ||
+	       (p->is_non_blocking_client && p->gnutls_initstage != GNUTLS_STAGE_TRANSPORT_POINTERS_SET))
 	{
 	  deactivate_process (proc);
 	  if (NILP (boot))
@@ -4950,8 +4951,10 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
 #endif
 #ifdef HAVE_GNUTLS
 		/* Continue TLS negotiation. */
-		if (p->gnutls_initstage == GNUTLS_STAGE_HANDSHAKE_TRIED
-		    && p->is_non_blocking_client)
+		if ((p->gnutls_initstage == GNUTLS_STAGE_TRANSPORT_POINTERS_SET ||
+		     p->gnutls_initstage == GNUTLS_STAGE_HANDSHAKE_TRIED)
+		    && p->is_non_blocking_client
+		    && (! FD_ISSET (p->outfd, &connect_wait_mask)))
 		  {
 		    gnutls_try_handshake (p);
 		    p->gnutls_handshakes_tried++;
-- 
2.6.2.windows.1