From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.ciao.gmane.io!not-for-mail From: Derek Zhou Newsgroups: gmane.emacs.bugs Subject: bug#40665: 28.0.50; tls hang on local ssl Date: Thu, 23 Apr 2020 02:20:53 +0000 (UTC) Message-ID: <86y2qnaqnv.fsf@mail.3qin.us> References: <86wo6fo78r.fsf@mail.3qin.us> <86d086dkgq.fsf@mail.3qin.us> <86eeslecnf.fsf@mail.3qin.us> <86blnnebh3.fsf@mail.3qin.us> <86zhb5hecx.fsf@mail.3qin.us> <86eeshpqdb.fsf@mail.3qin.us> <86zhb5q7sw.fsf@mail.3qin.us> <86y2qorj76.fsf@mail.3qin.us> <86368w1tge.fsf@mail.3qin.us> <864ktcfpm5.fsf@mail.3qin.us> <86368wfp6d.fsf@mail.3qin.us> <86y2qniu5m.fsf@mail.3qin.us> <86wo67im9w.fsf@mail.3qin.us> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="ciao.gmane.io:159.69.161.202"; logging-data="27254"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: mu4e 1.2.0; emacs 27.0.91 Cc: 40665@debbugs.gnu.org To: Robert Pluim Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Thu Apr 23 04:21:18 2020 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 1jRRUL-0006yh-Oh for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 23 Apr 2020 04:21:17 +0200 Original-Received: from localhost ([::1]:60816 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jRRUK-0001HE-Fl for geb-bug-gnu-emacs@m.gmane-mx.org; Wed, 22 Apr 2020 22:21:16 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:51440) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jRRUC-0001H2-Lc for bug-gnu-emacs@gnu.org; Wed, 22 Apr 2020 22:21:09 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.90_1) (envelope-from ) id 1jRRU6-000507-6c for bug-gnu-emacs@gnu.org; Wed, 22 Apr 2020 22:21:08 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:42160) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1jRRU5-0004yy-PC for bug-gnu-emacs@gnu.org; Wed, 22 Apr 2020 22:21:01 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1jRRU5-0007wX-IH for bug-gnu-emacs@gnu.org; Wed, 22 Apr 2020 22:21:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Derek Zhou Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Thu, 23 Apr 2020 02:21:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 40665 X-GNU-PR-Package: emacs Original-Received: via spool by 40665-submit@debbugs.gnu.org id=B40665.158760846030519 (code B ref 40665); Thu, 23 Apr 2020 02:21:01 +0000 Original-Received: (at 40665) by debbugs.gnu.org; 23 Apr 2020 02:21:00 +0000 Original-Received: from localhost ([127.0.0.1]:53706 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1jRRU4-0007wB-9H for submit@debbugs.gnu.org; Wed, 22 Apr 2020 22:21:00 -0400 Original-Received: from xtrwkvxq.outbound-mail.sendgrid.net ([167.89.24.164]:45462) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1jRRU2-0007vy-UD for 40665@debbugs.gnu.org; Wed, 22 Apr 2020 22:20:59 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=3qin.us; h=references:from:subject:in-reply-to:mime-version:to:cc:content-type; s=s1; bh=YSBL356F4i3giDO3Xh5L22j6D89NEYjsYAh5aqgh5vg=; b=vdA8ZDY6G8S8/VTkzHnBRELRLzsV8dHDpOuBP7J7KrNaNcOwAeHd8aPMqDz0kJRrqgMV uQXgnI/4iJr4xvTbC31slldvRE7E3MpKmySc5vPTIYASVsCushWMZ4Rrmlhj4S9LMYnOpw k8Cd6Uz7pVscPXXfLRb+50ZGActSXi0pw= Original-Received: by filterdrecv-p3iad2-8ddf98858-z54vx with SMTP id filterdrecv-p3iad2-8ddf98858-z54vx-21-5EA0FB85-8 2020-04-23 02:20:53.384376477 +0000 UTC m=+2423603.867016331 Original-Received: from mail.3qin.us (unknown) by ismtpd0009p1sjc2.sendgrid.net (SG) with ESMTP id nvF7R327Qqy68X_ch1CH2w Thu, 23 Apr 2020 02:20:53.056 +0000 (UTC) Original-Received: from derek by mail.3qin.us with local (Exim 4.92) (envelope-from ) id 1jRRTw-0004Nr-9Y; Wed, 22 Apr 2020 22:20:52 -0400 In-reply-to: X-SG-EID: nTv1epxWypfl0GRwygk8G5JZcpWJeBCcUM8ly3qzpbbVyfDgYog/9eXX7sL09UAmosklGUdZx2QG0m7m2WRJ7oYA+nNCiV/BwDtID7zPL0a+dXJ/ExIZdVql7tI8iKEWyaFVyDOtInWxxGDaznSETCvtX3grmhhoMNHT3YLb+ACJOKjrHqMelPi6/XVlB7erXWXiUy8r+LUmpW6cCj4B4SI5X2rj11PCirfb98DK5xZ9N8CCaFhX4dmj4syVTJbZ X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-Received-From: 209.51.188.43 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" Xref: news.gmane.io gmane.emacs.bugs:178841 Archived-At: --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Robert Pluim writes: > OK, that does make sense, and might even be more correct, but it=CA=BCs a > bigger change. You'll need more than just me to agree with it. > Patch reworked: * before the select, check every interesting gnutls stream for available data in the buffer * if some of them hit, and either there is no wait_proc or the wait_proc is one of the gnutls streams with new data, set the select timeout to 0 * after the select, merge the gnutls buffer status into the select returns The patch is not much longer than before, still a net reduction of code lines. I've done some light testing and haven't found any problem.=20 --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=check_pending_before_select.patch diff --git a/src/process.c b/src/process.c index 91d426103d..783ce098b3 100644 --- a/src/process.c +++ b/src/process.c @@ -5497,6 +5497,10 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd, } else { +#ifdef HAVE_GNUTLS + int tls_nfds; + fd_set tls_available; +#endif /* Set the timeout for adaptive read buffering if any process has non-zero read_output_skip and non-zero read_output_delay, and we are not reading output for a @@ -5566,6 +5570,36 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd, } #endif +#ifdef HAVE_GNUTLS + /* GnuTLS buffers data internally. We need to check if some + data is available in the buffers manually before the select. + And if so, we need to skip the select which could block */ + FD_ZERO (&tls_available); + tls_nfds = 0; + for (channel = 0; channel < FD_SETSIZE; ++channel) + if (! NILP (chan_process[channel]) && FD_ISSET(channel, &Available)) + { + struct Lisp_Process *p = + XPROCESS (chan_process[channel]); + if (p && p->gnutls_p && p->gnutls_state + && ((emacs_gnutls_record_check_pending + (p->gnutls_state)) + > 0)) + { + tls_nfds++; + eassert (p->infd == channel); + FD_SET (p->infd, &tls_available); + } + } + /* if wait_proc is somebody else, we have to wait in select as usual. + Otherwisr, clobber the timeout */ + if ((tls_nfds > 0) && + (!wait_proc || + (wait_proc->infd >= 0 && + FD_ISSET(wait_proc->infd, &tls_available)))) + timeout = make_timespec (0, 0); +#endif + /* Non-macOS HAVE_GLIB builds call thread_select in xgselect.c. */ #if defined HAVE_GLIB && !defined HAVE_NS nfds = xg_select (max_desc + 1, @@ -5584,60 +5618,22 @@ wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd, #endif /* !HAVE_GLIB */ #ifdef HAVE_GNUTLS - /* GnuTLS buffers data internally. In lowat mode it leaves - some data in the TCP buffers so that select works, but - with custom pull/push functions we need to check if some - data is available in the buffers manually. */ - if (nfds == 0) + /* merge tls_available into Available */ + if (tls_nfds > 0) + { + if (nfds == 0 || (nfds < 0 && errno == EINTR)) { - fd_set tls_available; - int set = 0; - - FD_ZERO (&tls_available); - if (! wait_proc) - { - /* We're not waiting on a specific process, so loop - through all the channels and check for data. - This is a workaround needed for some versions of - the gnutls library -- 2.12.14 has been confirmed - to need it. */ - for (channel = 0; channel < FD_SETSIZE; ++channel) - if (! NILP (chan_process[channel])) - { - struct Lisp_Process *p = - XPROCESS (chan_process[channel]); - if (p && p->gnutls_p && p->gnutls_state - && ((emacs_gnutls_record_check_pending - (p->gnutls_state)) - > 0)) - { - nfds++; - eassert (p->infd == channel); - FD_SET (p->infd, &tls_available); - set++; - } - } - } - else - { - /* Check this specific channel. */ - if (wait_proc->gnutls_p /* Check for valid process. */ - && wait_proc->gnutls_state - /* Do we have pending data? */ - && ((emacs_gnutls_record_check_pending - (wait_proc->gnutls_state)) - > 0)) - { - nfds = 1; - eassert (0 <= wait_proc->infd); - /* Set to Available. */ - FD_SET (wait_proc->infd, &tls_available); - set++; - } - } - if (set) - Available = tls_available; + /* fast path, just copy */ + nfds = tls_nfds; + Available = tls_available; } + else if (nfds > 0) + /* slow path, merge one by one. + Note: nfds does not need to be accurate, just positive is enough */ + for (channel = 0; channel < FD_SETSIZE; ++channel) + if (FD_ISSET(channel, &tls_available)) + FD_SET(channel, &Available); + } #endif } --=-=-=--