GNU bug report logs - #65211
30.0.50; threads and accept-process-output

Please note: This is a static page, with minimal formatting, updated once a day.
Click here to see this page with the latest information and nicer formatting.

Package: emacs; Reported by: Helmut Eller <eller.helmut@HIDDEN>; Keywords: patch; dated Thu, 10 Aug 2023 15:06:02 UTC; Maintainer for emacs is bug-gnu-emacs@HIDDEN.
Added tag(s) patch. Request was from Stefan Kangas <stefankangas@HIDDEN> to control <at> debbugs.gnu.org. Full text available.

Message received at 65211 <at> debbugs.gnu.org:


Received: (at 65211) by debbugs.gnu.org; 11 Aug 2023 08:33:04 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Fri Aug 11 04:33:04 2023
Received: from localhost ([127.0.0.1]:44858 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1qUNZv-0002SG-Ie
	for submit <at> debbugs.gnu.org; Fri, 11 Aug 2023 04:33:04 -0400
Received: from mail-lj1-x22b.google.com ([2a00:1450:4864:20::22b]:56439)
 by debbugs.gnu.org with esmtp (Exim 4.84_2)
 (envelope-from <eller.helmut@HIDDEN>) id 1qUNZt-0002Rl-Hq
 for 65211 <at> debbugs.gnu.org; Fri, 11 Aug 2023 04:33:02 -0400
Received: by mail-lj1-x22b.google.com with SMTP id
 38308e7fff4ca-2b9a2033978so26718421fa.0
 for <65211 <at> debbugs.gnu.org>; Fri, 11 Aug 2023 01:33:01 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=gmail.com; s=20221208; t=1691742775; x=1692347575;
 h=mime-version:user-agent:message-id:date:references:in-reply-to
 :subject:cc:to:from:from:to:cc:subject:date:message-id:reply-to;
 bh=RRRTt6p2pi6raXPk1D3iPMwvaZhTCklLK5kXqf8DYtI=;
 b=kMrEEHB7qseVm2+31Xrjsi1hoO3tX8Pxb+e20nSRVThiywM3rppVS+GKe97dRoqNYW
 Lb/EMUqWlxcPBHB5S5v3NABRUelY1daOBASEVeQ7XULrisifYzOM3ZH5tvSZALi4sIDb
 O/RL689qTvp+qySSeEgAcfhAEMP5iR+YFqw0i+bG+DSpjgTZS6dw7cLUdUnQdLxVURPt
 4VgBLDn07yqqhsZM03zysT7KcexpSP1JU/pbyllb2TbuoGT3a58V2zqwlrnPj+Vr5I+4
 BHx1TI+xhAE2ucBMI6/BizgCH0ZBfJZSDd54/gJACI5EpJpWtlj/j7vSKVt7WU8+voC6
 qvcw==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20221208; t=1691742775; x=1692347575;
 h=mime-version:user-agent: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=RRRTt6p2pi6raXPk1D3iPMwvaZhTCklLK5kXqf8DYtI=;
 b=FzC5acn0z8Ewm+NOJT5YndWDN5xog/8yvFShUBBhAFqFl4w8jfJj36UeNlGQVd80dg
 hO9GqFNsm8lVRUWSF8LQYFk1GYIuGR6v3c84f89Ka+V52ojzbGv3Q11BMuGT2e8tioS+
 I2ZRKuKFCltXanFTWxPPq3kHHHpAHddslVxqgWcXgeNBnC48cXbN+CxGiz568x5cH5lZ
 AIQEwXPoH6jmRqkdjSFkShwA9FKBZMIYxwd6htdSPNN+Cx40rJW3Z59tzQGPiHqpI6cU
 WT97hJtYVssPhtaywPJ8M2B3/ZT3kveVbYLvwRFd8Bgtz5EosESYOb3XatlXFebrDZNu
 0CBw==
X-Gm-Message-State: AOJu0YwunQMTC+V0Z4tAb/yiUISIh8AchVU5YK9hwmjUjjBvJR4/qzjd
 NmS4+kdd6XJLqU+7dWRh8+eLTgwEMvw=
X-Google-Smtp-Source: AGHT+IEzDeCSW8MUsCHnWncqqYqVNSAVcuv8jUv2GyfiU4HsM9BdHZugQakxu92BHJ+EBjU1cadFGg==
X-Received: by 2002:a2e:b616:0:b0:2b6:ea3b:f082 with SMTP id
 r22-20020a2eb616000000b002b6ea3bf082mr1041416ljn.38.1691742774701; 
 Fri, 11 Aug 2023 01:32:54 -0700 (PDT)
Received: from caladan ([185.127.213.71]) by smtp.gmail.com with ESMTPSA id
 x16-20020a05600c2a5000b003fe13c3ece7sm7387488wme.10.2023.08.11.01.32.53
 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
 Fri, 11 Aug 2023 01:32:54 -0700 (PDT)
From: Helmut Eller <eller.helmut@HIDDEN>
To: Robert Pluim <rpluim@HIDDEN>
Subject: Re: bug#65211: 30.0.50; threads and accept-process-output
In-Reply-To: <87sf8q6hzb.fsf@HIDDEN> (Robert Pluim's message of "Thu, 10
 Aug 2023 17:21:28 +0200")
References: <m21qgb2b1r.fsf@HIDDEN> <87sf8q6hzb.fsf@HIDDEN>
Date: Fri, 11 Aug 2023 10:32:53 +0200
Message-ID: <m2il9mq8qy.fsf@HIDDEN>
User-Agent: Gnus/5.13 (Gnus v5.13)
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="=-=-="
X-Spam-Score: -0.0 (/)
X-Debbugs-Envelope-To: 65211
Cc: 65211 <at> debbugs.gnu.org
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -1.0 (-)

--=-=-=
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable

On Thu, Aug 10 2023, Robert Pluim wrote:

> Hmm. Maybe putting it in `deactivate_process' is better (not that I=CA=BC=
ve
> tested =F0=9F=98=BA)

After a night of sleep, it occurred to me that this could also be
considered a problem of incomplete initialization of the
fd_callback_info[fd] struct.

So I wondered who is supposed to initialize it.  Unfortunately, there is
half a dozen of add_<foo>_fd functions that set various bits, some call
each other, some duplicate code.  None of them sets the waiting_thread
slot.  And there is no single point that clears everything out (except
init_process_emacs).

I also discovered that recompute_max_desc considers an fd unused if the
.flags field is zero.  Luckily, recompute_max_desc is only called in
three places: delete_write_fd, delete_keyboard_wait_descriptor and
deactivate_process.  The call in deactivate_process seems redundant as
it calls the other two anyway.  It seems easier to re-initialize the
struct when it becomes unused than to initialize it when it is used the
first time.

So I'm proposing the patch below that moves the re-initialization to one
single place, namely: recompute_max_desc.

Helmut


--=-=-=
Content-Type: text/x-diff
Content-Disposition: inline;
 filename=0001-Re-initialize-fd_callback_data-fd-more-thoroughly.patch

From 675a76b297e9f050f719489a1419f0278ad8e9ae Mon Sep 17 00:00:00 2001
From: Helmut Eller <eller.helmut@HIDDEN>
Date: Fri, 11 Aug 2023 10:31:24 +0200
Subject: [PATCH] Re-initialize fd_callback_data[fd] more thoroughly

The waiting_thread field was not re-initialized properly when a a
closed file descriptor resued by another process. (bug#65211)

As recompute_max_desc must be called when a file descriptor becomes
unused, we can do some re-initialization there.

* src/process.c (recompute_max_desc): Take the fd that was just
deleted as argument fd.  If the flags field is 0, reset the rest of
the struct too.
(delete_write_fd, delete_keyboard_wait_descriptor): Update callers
accordingly.
(delete_read_fd): This is now just an alias for
delete_keyboard_wait_descriptor.
(deactivate_process): Remove the call to recompute_max_desc, as
delete_read_fd is called anyway.
---
 src/process.c | 46 ++++++++++++++++------------------------------
 1 file changed, 16 insertions(+), 30 deletions(-)

diff --git a/src/process.c b/src/process.c
index 08cb810ec13..cd5fd97e9d6 100644
--- a/src/process.c
+++ b/src/process.c
@@ -507,13 +507,6 @@ void
 delete_read_fd (int fd)
 {
   delete_keyboard_wait_descriptor (fd);
-
-  eassert (0 <= fd && fd < FD_SETSIZE);
-  if (fd_callback_info[fd].flags == 0)
-    {
-      fd_callback_info[fd].func = 0;
-      fd_callback_info[fd].data = 0;
-    }
 }
 
 /* Add a file descriptor FD to be monitored for when write is possible.
@@ -544,19 +537,22 @@ add_non_blocking_write_fd (int fd)
 }
 
 static void
-recompute_max_desc (void)
+recompute_max_desc (int fd)
 {
-  int fd;
-
+  eassert (0 <= fd && fd < FD_SETSIZE);
   eassert (max_desc < FD_SETSIZE);
-  for (fd = max_desc; fd >= 0; --fd)
-    {
+
+  if (fd_callback_info[fd].flags == 0)
+    fd_callback_info[fd] = (struct fd_callback_data){ 0 };
+
+  if (fd == max_desc)
+    for (; fd >= 0; --fd)
       if (fd_callback_info[fd].flags != 0)
-	{
-	  max_desc = fd;
-	  break;
-	}
-    }
+        {
+          max_desc = fd;
+          break;
+        }
+
   eassert (max_desc < FD_SETSIZE);
 }
 
@@ -569,17 +565,10 @@ delete_write_fd (int fd)
   if ((fd_callback_info[fd].flags & NON_BLOCKING_CONNECT_FD) != 0)
     {
       if (--num_pending_connects < 0)
-	emacs_abort ();
+        emacs_abort ();
     }
   fd_callback_info[fd].flags &= ~(FOR_WRITE | NON_BLOCKING_CONNECT_FD);
-  if (fd_callback_info[fd].flags == 0)
-    {
-      fd_callback_info[fd].func = 0;
-      fd_callback_info[fd].data = 0;
-
-      if (fd == max_desc)
-	recompute_max_desc ();
-    }
+  recompute_max_desc (fd);
 }
 
 static void
@@ -4809,8 +4798,6 @@ deactivate_process (Lisp_Object proc)
       delete_read_fd (inchannel);
       if ((fd_callback_info[inchannel].flags & NON_BLOCKING_CONNECT_FD) != 0)
 	delete_write_fd (inchannel);
-      if (inchannel == max_desc)
-	recompute_max_desc ();
     }
 }
 
@@ -8134,8 +8121,7 @@ delete_keyboard_wait_descriptor (int desc)
 
   fd_callback_info[desc].flags &= ~(FOR_READ | KEYBOARD_FD | PROCESS_FD);
 
-  if (desc == max_desc)
-    recompute_max_desc ();
+  recompute_max_desc (desc);
 #endif
 }
 
-- 
2.39.2


--=-=-=--




Information forwarded to bug-gnu-emacs@HIDDEN:
bug#65211; Package emacs. Full text available.

Message received at 65211 <at> debbugs.gnu.org:


Received: (at 65211) by debbugs.gnu.org; 10 Aug 2023 16:25:08 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Thu Aug 10 12:25:08 2023
Received: from localhost ([127.0.0.1]:43910 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1qU8TB-00018b-Oj
	for submit <at> debbugs.gnu.org; Thu, 10 Aug 2023 12:25:08 -0400
Received: from mail-wm1-x32c.google.com ([2a00:1450:4864:20::32c]:42331)
 by debbugs.gnu.org with esmtp (Exim 4.84_2)
 (envelope-from <eller.helmut@HIDDEN>) id 1qU8T5-00017w-UT
 for 65211 <at> debbugs.gnu.org; Thu, 10 Aug 2023 12:25:04 -0400
Received: by mail-wm1-x32c.google.com with SMTP id
 5b1f17b1804b1-3fe4b95c371so6510945e9.1
 for <65211 <at> debbugs.gnu.org>; Thu, 10 Aug 2023 09:24:59 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=gmail.com; s=20221208; t=1691684694; x=1692289494;
 h=content-transfer-encoding:mime-version:user-agent:message-id:date
 :references:in-reply-to:subject:cc:to:from:from:to:cc:subject:date
 :message-id:reply-to;
 bh=DJtmwUto7xkek64giLhRwUSgdKTRTS8kq95n+EuyMIE=;
 b=S+Wn7XCCPryYvtOcm69KLFg9hr73MoG68UoVemjv2mHTYbvD79eeEus9ebWVIMaZiU
 9tc58IkVaLRyg8j/5EepXy7rpJMikA3gcGsPxWDnroyB8I3XtXdcIWxuGy2EyM+J5szs
 Q3/Wmk7SlEnxF4APDaQRb2FWpU0cZEf8ISL5nSX/OsA5vdwgCeBwBcIs8sUjGlgoRGO6
 gfeGvlrA+lRdtRKFX6FfrGFW9CljJApIym707AGDSxcyCKLogcfE0zsSUbsZAHE9qCXw
 yYWXiNkOKMwG1OD5/peYuIa3t2VXdR3KhSzuts71wMfcmjOn2qwnybmUVXrZ6pTOkoFF
 MLLQ==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20221208; t=1691684694; x=1692289494;
 h=content-transfer-encoding:mime-version:user-agent: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=DJtmwUto7xkek64giLhRwUSgdKTRTS8kq95n+EuyMIE=;
 b=YIbRw5dtviv8wlrhnapqBLrHAbzkgHtcYhey8zoBpeWbOJIG4gBX3D3DyfgOeXlfE2
 s3v0ppaPgSalnIZP11Ffb9531AtY4pKg1TS8wBv3je7Yvn5FNN5f+kHxiXHHsOl3Zz0w
 CaWwKPRqh7Nlg3GV7ScivSpFI2F7DAR+qpIEDc19E8V3A6/yL1szBl9aqYFlROutZjKG
 USGmwaOz0vC6NHKRfuJDxd8rI/a+vzEo8d+pfGNLdVXEuloToGsa5ESWw6Rii5zzQLCt
 OyXbBb1J63fuFfwzHKuDCHSFmz/w5MjKYMjp/oEwK5XEr/9tEmQPscw117F9SMjdYShw
 Fvog==
X-Gm-Message-State: AOJu0Yw7zKgnhEY8GOXovBWcNEs1fsNVtsNFVUJn18iG4eDc8IGLus+F
 OBBTbV/Z2y9WsbY/pNbGCzsDvbN8jwA=
X-Google-Smtp-Source: AGHT+IG3YiQkbdMZTGEyUm0seBb2GJpfSVXtPUDgOrUBr04TIepnrP5fERncJgLpbJUk9ATtat+vCA==
X-Received: by 2002:a05:600c:3786:b0:3fe:23ec:2494 with SMTP id
 o6-20020a05600c378600b003fe23ec2494mr2120822wmr.7.1691684693926; 
 Thu, 10 Aug 2023 09:24:53 -0700 (PDT)
Received: from caladan (dialin-228086.xdsl.raiffeisen.net. [195.254.228.86])
 by smtp.gmail.com with ESMTPSA id
 t15-20020a7bc3cf000000b003fe601a7d46sm5511277wmj.45.2023.08.10.09.24.53
 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
 Thu, 10 Aug 2023 09:24:53 -0700 (PDT)
From: Helmut Eller <eller.helmut@HIDDEN>
To: Robert Pluim <rpluim@HIDDEN>
Subject: Re: bug#65211: 30.0.50; threads and accept-process-output
In-Reply-To: <87sf8q6hzb.fsf@HIDDEN> (Robert Pluim's message of "Thu, 10
 Aug 2023 17:21:28 +0200")
References: <m21qgb2b1r.fsf@HIDDEN> <87sf8q6hzb.fsf@HIDDEN>
Date: Thu, 10 Aug 2023 18:24:52 +0200
Message-ID: <m2o7jerhkb.fsf@HIDDEN>
User-Agent: Gnus/5.13 (Gnus v5.13)
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable
X-Spam-Score: -0.0 (/)
X-Debbugs-Envelope-To: 65211
Cc: 65211 <at> debbugs.gnu.org
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -1.0 (-)

On Thu, Aug 10 2023, Robert Pluim wrote:

> Hmm. Maybe putting it in `deactivate_process' is better (not that I=CA=BC=
ve
> tested =F0=9F=98=BA)

That's for the maintainers to decide.  It seems to work too.  To me,
delete_read_fd feels like the appropriate place.

For symmetry, delete_write_fd should probably also do something with
.waiting_thread, but coming up with a test case for that is beyond my
understanding of this code.

Helmut




Information forwarded to bug-gnu-emacs@HIDDEN:
bug#65211; Package emacs. Full text available.

Message received at 65211 <at> debbugs.gnu.org:


Received: (at 65211) by debbugs.gnu.org; 10 Aug 2023 15:21:45 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Thu Aug 10 11:21:44 2023
Received: from localhost ([127.0.0.1]:43843 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1qU7Tr-0007r7-HN
	for submit <at> debbugs.gnu.org; Thu, 10 Aug 2023 11:21:44 -0400
Received: from mail-wm1-x32e.google.com ([2a00:1450:4864:20::32e]:61706)
 by debbugs.gnu.org with esmtp (Exim 4.84_2)
 (envelope-from <rpluim@HIDDEN>) id 1qU7Tl-0007qp-6h
 for 65211 <at> debbugs.gnu.org; Thu, 10 Aug 2023 11:21:42 -0400
Received: by mail-wm1-x32e.google.com with SMTP id
 5b1f17b1804b1-3fe82a7864bso1341525e9.3
 for <65211 <at> debbugs.gnu.org>; Thu, 10 Aug 2023 08:21:37 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=gmail.com; s=20221208; t=1691680891; x=1692285691;
 h=content-transfer-encoding:mime-version:message-id:date:references
 :in-reply-to:subject:cc:to:from:from:to:cc:subject:date:message-id
 :reply-to; bh=K+YHQxPFLms+Vt0s/3XLF5z6vOaBx19fmcxSu+0u+dQ=;
 b=jJQ12GKXp0Yau+bJ4st07SfVE0HjkYwOiihyas4UoT7V2GpB5Z+Do7SEBOdtqCX9cA
 1F8UbmPWyYAQk7pMCM2w6xRgcGT9/EcnJ4MwHL2R14euKGihxfEBzvNLLgRhVNrwiPL2
 yK6L6mNx0krBslhaUPgfFtMwyu/ESMQ1aeK54x/KrhylUZs3/a7F6+HT0Ug3VMK5wiZz
 XFRZYHrO2uOTe5HdhsKIly+B1qxNIC+tz8J0AUBCa166TOQ/0q+ru0domrTVE33w0dCN
 B5h0JnDVAS2hT0VqFc6x8nGfecmwk9SEjvRPczDYVvFUW5GdNLqY47SoOw0KjEVa+J4G
 GcYg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20221208; t=1691680891; x=1692285691;
 h=content-transfer-encoding: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=K+YHQxPFLms+Vt0s/3XLF5z6vOaBx19fmcxSu+0u+dQ=;
 b=JjLxE7DzhzQctHBoyq46tQTkp4NYa5JrUf2mVE1k3fUhgq+uhbVQhKxu64YP29Hu8v
 DInGSWvdxQ1rQ50BRo6K4VO7qAQgfsCRHd3BO9YRmIqyzCFof1sCpU6/ToiM2aIkkS1Z
 hyCDV215f0Hkbk1d904AkzVXXTS4t3FY+SUeyPB427hpkn19FbHYQRR85C0Dv+M/5Yew
 AzsDuVu8x1xDWvk7x7eb3iWBFFW1KyxnZ90H42CZg4/8tNVNWa+cGaXO5hn/SE2i7sZM
 lWWgHZ4/CtYFUxzpQivnX6G7muCtUCk1o2YHB++hwqdTpAGhsLB90gYblD8EOkKUZ0pw
 6wNQ==
X-Gm-Message-State: AOJu0Yw5tS4JzXRBZipnrdnQ8DFByBgdjVlSutTmOsNdLvmtAY0M9za6
 94/i5MLNLGpfxfVYY4MNqk6nWGr4x2E=
X-Google-Smtp-Source: AGHT+IEWYcCuju+pnXCY6C90Q4Xtd0qpUzAGFY0I8NXKdu1sVi2AYnmaGUSkWEKxb0QOkK80RKCKlw==
X-Received: by 2002:a7b:c5d4:0:b0:3f9:b244:c294 with SMTP id
 n20-20020a7bc5d4000000b003f9b244c294mr2468551wmk.35.1691680890615; 
 Thu, 10 Aug 2023 08:21:30 -0700 (PDT)
Received: from rltb ([2a01:e0a:3f3:fb50:b086:92d7:f1ed:48e0])
 by smtp.gmail.com with ESMTPSA id
 p4-20020a1c7404000000b003fe1a092925sm2422649wmc.19.2023.08.10.08.21.29
 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
 Thu, 10 Aug 2023 08:21:29 -0700 (PDT)
From: Robert Pluim <rpluim@HIDDEN>
To: Helmut Eller <eller.helmut@HIDDEN>
Subject: Re: bug#65211: 30.0.50; threads and accept-process-output
In-Reply-To: <m21qgb2b1r.fsf@HIDDEN> (Helmut Eller's message of "Thu, 10
 Aug 2023 17:04:48 +0200")
References: <m21qgb2b1r.fsf@HIDDEN>
Date: Thu, 10 Aug 2023 17:21:28 +0200
Message-ID: <87sf8q6hzb.fsf@HIDDEN>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable
X-Spam-Score: -0.0 (/)
X-Debbugs-Envelope-To: 65211
Cc: 65211 <at> debbugs.gnu.org
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -1.0 (-)

>>>>> On Thu, 10 Aug 2023 17:04:48 +0200, Helmut Eller <eller.helmut@gmail.=
com> said:

    Helmut> The test case from below hangs when executed with
    Helmut>   emacs -Q --batch -l apo-tests.el -f ert-run-tests-batch-and-e=
xit

    Helmut> It's not 100% reproducible, but it seems to hang 80% of the tim=
e.

    Helmut> The test is fairly complicated and involves a TLS connection, a
    Helmut> sub-process and a background thread.  The background thread sta=
rts a
    Helmut> sub-process and reads all its output until the process terminat=
es.  Then
    Helmut> the main thread opens a TLS connection, sends a HTTP request an=
d tries
    Helmut> to read the response.  At this point, accept-process-output han=
gs even
    Helmut> though there is output available.

    Helmut> I think the reason for this problem is that the sub-process use=
s a file
    Helmut> descriptor (usually number 6) and sets
    Helmut> fd_callback_info[6].waiting_thread to the background thread (in
    Helmut> compute_non_keyboard_wait_mask) .  Later, the TLS connection re=
ceives a
    Helmut> file descriptor with the same number (6) because the sub-proces=
s closed
    Helmut> it already.  That would be fine, however
    Helmut> fd_callback_info[6].waiting_thread is still set to the backgrou=
nd
    Helmut> thread.  So this time compute_non_keyboard_wait_mask doesn't in=
clude 6
    Helmut> in the wait mask.  Because 6 is not in the wait mask,
    Helmut> emacs_gnutls_record_check_pending will not be called and Emacs =
doesn't
    Helmut> notice the buffered output.

    Helmut> The intention in the code seems to be that clear_waiting_thread=
_info
    Helmut> resets fd_callback_info[6].waiting_thread to NULL, however by t=
he time
    Helmut> that clear_waiting_thread_info is called, max_desc was reduced =
to 4,
    Helmut> because somebody closed file descriptors 5 and 6.  So
    Helmut> clear_waiting_thread_info doesn't touch fd_callback_info[6].

    Helmut> A possible solution would be to reset the .waiting_thread field=
 in
    Helmut> delete_read_fd, like so:

    Helmut> diff --git a/src/process.c b/src/process.c
    Helmut> index 08cb810ec13..74d0bf252ab 100644
    Helmut> --- a/src/process.c
    Helmut> +++ b/src/process.c
    Helmut> @@ -513,6 +513,9 @@ delete_read_fd (int fd)
    Helmut>      {
    Helmut>        fd_callback_info[fd].func =3D 0;
    Helmut>        fd_callback_info[fd].data =3D 0;
    Helmut> +
    Helmut> +      if (fd_callback_info[fd].waiting_thread =3D=3D current_t=
hread)
    Helmut> +	fd_callback_info[fd].waiting_thread =3D NULL;
    Helmut>      }
    Helmut>  }

Hmm. Maybe putting it in `deactivate_process' is better (not that I=CA=BCve
tested =F0=9F=98=BA)

Robert
--=20




Information forwarded to bug-gnu-emacs@HIDDEN:
bug#65211; Package emacs. Full text available.

Message received at submit <at> debbugs.gnu.org:


Received: (at submit) by debbugs.gnu.org; 10 Aug 2023 15:05:19 +0000
From debbugs-submit-bounces <at> debbugs.gnu.org Thu Aug 10 11:05:18 2023
Received: from localhost ([127.0.0.1]:43827 helo=debbugs.gnu.org)
	by debbugs.gnu.org with esmtp (Exim 4.84_2)
	(envelope-from <debbugs-submit-bounces <at> debbugs.gnu.org>)
	id 1qU7Dy-0007Rc-7n
	for submit <at> debbugs.gnu.org; Thu, 10 Aug 2023 11:05:18 -0400
Received: from lists.gnu.org ([2001:470:142::17]:40436)
 by debbugs.gnu.org with esmtp (Exim 4.84_2)
 (envelope-from <eller.helmut@HIDDEN>) id 1qU7Dt-0007RH-0B
 for submit <at> debbugs.gnu.org; Thu, 10 Aug 2023 11:05:16 -0400
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 <eller.helmut@HIDDEN>)
 id 1qU7Dn-0005K5-1z
 for bug-gnu-emacs@HIDDEN; Thu, 10 Aug 2023 11:05:07 -0400
Received: from mail-wm1-x332.google.com ([2a00:1450:4864:20::332])
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128)
 (Exim 4.90_1) (envelope-from <eller.helmut@HIDDEN>)
 id 1qU7Dj-000568-0n
 for bug-gnu-emacs@HIDDEN; Thu, 10 Aug 2023 11:05:06 -0400
Received: by mail-wm1-x332.google.com with SMTP id
 5b1f17b1804b1-3fe1e1142caso9245735e9.0
 for <bug-gnu-emacs@HIDDEN>; Thu, 10 Aug 2023 08:05:01 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=gmail.com; s=20221208; t=1691679899; x=1692284699;
 h=mime-version:message-id:date:subject:to:from:from:to:cc:subject
 :date:message-id:reply-to;
 bh=IZK2EG0B2I8A4f5F0huuFX5YXUnCxk/18h+ZVbeYTME=;
 b=iyi7dh2tSrjTicxCPzeQmEYd5IMkF/ULo7uKwV/ddiIj963Sb9xYzoy8SOgeyNRgfh
 ug8X3sFH+NQMDNZJ2NUdhrxb26EYPyQNxYrDz6n8xywO0oCuwK6bkI0Ztrjzd4KF67Z5
 JYSyWpGT9TbezmSc8JoISC+LgnGG0tR9znEi5qWo0Z9p68d14+zsfpdwjvMRJgj1wsNJ
 aToLCNSA3y05P2f4yis82SIzkZy9WRNzzVlRadsCbG36nc/2PFuT67LfrcDnBWEfdt6I
 poEEUyGvQXFjabVNVovm7NEovSQuRYMJpizG//KoZ6YSaWJCLJZ33i3kVINx+WJ2t6wV
 N+3w==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20221208; t=1691679899; x=1692284699;
 h=mime-version:message-id:date:subject:to:from:x-gm-message-state
 :from:to:cc:subject:date:message-id:reply-to;
 bh=IZK2EG0B2I8A4f5F0huuFX5YXUnCxk/18h+ZVbeYTME=;
 b=gpuLs1jyqik1JVfrVmto3Gi93OIKf8zRYMxmPmhRsHtQm+pgfzRX8r8koGSpXy7nwt
 Ya5HH5G0MEU4WDeDvP3T/RDM+c4zo7pNqHEYh8aQXAL0zk9zYmiVEEWPYU28j6joqpvn
 8G7ZdAWvGWDb3ZA7OQ03rHDpb2WJt5YgzK9ELhmYkFTs1BHDeRZqH/DhUxvLy8Vkl+Wy
 g6uLYV7eMLYBJrzC73Znc1K+i8Fih2hHgRXK6FkBgb8LiOOpc6RqcpcGSXV/mQJoG30y
 BFzu3F1gXVRkK9jBGuX9eYQOJHAOfvJyRkVVQ01QtCK0TJKMrpB0fCkUylMVN+Ku07Rm
 b8sA==
X-Gm-Message-State: AOJu0YxouJd7KtoGSVWSUPyxD9gUsOQmjKDuUOfb6zGdP6+ukh+9GL0j
 nxaE/pjzpf4J55jkoCGQzCA0HU57uCo=
X-Google-Smtp-Source: AGHT+IEFQrGrYAlxiIQ5aB3drnI2D2R56X4zgWEhK1hB8fpH6COe4nfKaqCLU1dtLoJiJm2552IzxA==
X-Received: by 2002:adf:fdc6:0:b0:317:731c:4d80 with SMTP id
 i6-20020adffdc6000000b00317731c4d80mr2099441wrs.24.1691679898899; 
 Thu, 10 Aug 2023 08:04:58 -0700 (PDT)
Received: from caladan (dialin-228086.xdsl.raiffeisen.net. [195.254.228.86])
 by smtp.gmail.com with ESMTPSA id
 b13-20020adfe30d000000b003113ed02080sm2420217wrj.95.2023.08.10.08.04.58
 for <bug-gnu-emacs@HIDDEN>
 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
 Thu, 10 Aug 2023 08:04:58 -0700 (PDT)
From: Helmut Eller <eller.helmut@HIDDEN>
To: bug-gnu-emacs@HIDDEN
Subject: 30.0.50; threads and accept-process-output
Date: Thu, 10 Aug 2023 17:04:48 +0200
Message-ID: <m21qgb2b1r.fsf@HIDDEN>
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="=-=-="
Received-SPF: pass client-ip=2a00:1450:4864:20::332;
 envelope-from=eller.helmut@HIDDEN; helo=mail-wm1-x332.google.com
X-Spam_score_int: -20
X-Spam_score: -2.1
X-Spam_bar: --
X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1,
 DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001,
 RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001,
 SPF_PASS=-0.001 autolearn=ham autolearn_force=no
X-Spam_action: no action
X-Spam-Score: 1.0 (+)
X-Debbugs-Envelope-To: submit
X-BeenThere: debbugs-submit <at> debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
List-Id: <debbugs-submit.debbugs.gnu.org>
List-Unsubscribe: <https://debbugs.gnu.org/cgi-bin/mailman/options/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=unsubscribe>
List-Archive: <https://debbugs.gnu.org/cgi-bin/mailman/private/debbugs-submit/>
List-Post: <mailto:debbugs-submit <at> debbugs.gnu.org>
List-Help: <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=help>
List-Subscribe: <https://debbugs.gnu.org/cgi-bin/mailman/listinfo/debbugs-submit>, 
 <mailto:debbugs-submit-request <at> debbugs.gnu.org?subject=subscribe>
Errors-To: debbugs-submit-bounces <at> debbugs.gnu.org
Sender: "Debbugs-submit" <debbugs-submit-bounces <at> debbugs.gnu.org>
X-Spam-Score: -0.0 (/)

--=-=-=
Content-Type: text/plain

The test case from below hangs when executed with

  emacs -Q --batch -l apo-tests.el -f ert-run-tests-batch-and-exit

It's not 100% reproducible, but it seems to hang 80% of the time.

The test is fairly complicated and involves a TLS connection, a
sub-process and a background thread.  The background thread starts a
sub-process and reads all its output until the process terminates.  Then
the main thread opens a TLS connection, sends a HTTP request and tries
to read the response.  At this point, accept-process-output hangs even
though there is output available.

I think the reason for this problem is that the sub-process uses a file
descriptor (usually number 6) and sets
fd_callback_info[6].waiting_thread to the background thread (in
compute_non_keyboard_wait_mask) .  Later, the TLS connection receives a
file descriptor with the same number (6) because the sub-process closed
it already.  That would be fine, however
fd_callback_info[6].waiting_thread is still set to the background
thread.  So this time compute_non_keyboard_wait_mask doesn't include 6
in the wait mask.  Because 6 is not in the wait mask,
emacs_gnutls_record_check_pending will not be called and Emacs doesn't
notice the buffered output.

The intention in the code seems to be that clear_waiting_thread_info
resets fd_callback_info[6].waiting_thread to NULL, however by the time
that clear_waiting_thread_info is called, max_desc was reduced to 4,
because somebody closed file descriptors 5 and 6.  So
clear_waiting_thread_info doesn't touch fd_callback_info[6].

A possible solution would be to reset the .waiting_thread field in
delete_read_fd, like so:

diff --git a/src/process.c b/src/process.c
index 08cb810ec13..74d0bf252ab 100644
--- a/src/process.c
+++ b/src/process.c
@@ -513,6 +513,9 @@ delete_read_fd (int fd)
     {
       fd_callback_info[fd].func = 0;
       fd_callback_info[fd].data = 0;
+
+      if (fd_callback_info[fd].waiting_thread == current_thread)
+	fd_callback_info[fd].waiting_thread = NULL;
     }
 }


With this change, the test doesn't hang.

Helmut


--=-=-=
Content-Type: text/plain; charset=utf-8
Content-Disposition: inline; filename=apo-tests.el
Content-Transfer-Encoding: quoted-printable

;; -*- lexical-binding: t -*-

(require 'ert)
(require 'gnutls)

(defun t--read (proc nchars)
  "Read at least NCHARS characters from process PROC.
Return 'ok on success; otherwise return 'not-live."
  (let* ((buf (process-buffer proc))
	 (size (+ (buffer-size buf) nchars))
	 (result nil))
    (t--accept-process-output proc 0)
    (while (eq result nil)
      (cond ((>=3D (buffer-size buf) size)
	     (setq result 'ok))
	    ((not (process-live-p proc))
	     (setq result 'not-live))
	    (t
	     (t--accept-process-output proc nil))))
    result))

;; a wrapper around accept-process-output for debugging
(defun t--accept-process-output (proc timeout)
  (let ((buf (process-buffer proc)))
    (message "%s accept-process-output %S %s %s =E2=80=A6"
	     (current-thread) proc (process-status proc) timeout)
    (let ((val (accept-process-output proc timeout)))
      (message "%s accept-process-output %S %s %s =3D> %S %d"
	       (current-thread) proc (process-status proc) timeout
	       val (buffer-size buf))
      val)))

(defun t--read-all (proc)
  (while (pcase-exhaustive (t--read proc 1)
	   ('ok t)
	   ('not-live nil)))
  (with-current-buffer (process-buffer proc)
    (buffer-string)))

(defun t--make-buffer (name)
  (with-current-buffer (generate-new-buffer name t)
    (buffer-disable-undo)
    (set-buffer-multibyte nil)
    (current-buffer)))

(ert-deftest t-tls ()
  (thread-join
   (make-thread
    (lambda ()
      (let* ((proc (make-process
		    :name "cat"
		    :command (list "cat")
		    :sentinel (lambda (_ _))
		    :buffer (t--make-buffer "cat"))))
	(process-send-eof proc)
	(should (equal (t--read-all proc) ""))))))
  (let* ((host "www.example.net")
	 (proc (make-network-process
		:name "tls"
		:host host
		:service 443
		:tls-parameters (cons 'gnutls-x509pki
				      (gnutls-boot-parameters
				       :hostname host))
		:buffer (t--make-buffer "tls")
		:sentinel (lambda (_ _))))
	 (request (format "GET / HTTP/1.1\r\nHost: %s\r\n\r\n" host)))
    (process-send-string proc request)
    (process-send-eof proc)
    (let* ((response (t--read-all proc)))
      (should (string-match "^HTTP/1.1 200 OK" response)))))

;; Local Variables:
;; read-symbol-shorthands: (("t-" . "apo-tests-"))
;; End:

--=-=-=
Content-Type: text/plain



In GNU Emacs 30.0.50 (build 71, x86_64-pc-linux-gnu, GTK+ Version
 3.24.37, cairo version 1.16.0) of 2023-08-10 built on caladan
Repository revision: 164588b174774eba0c3bd6999633a39bed748195
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 11.0.12101007
System Description: Debian GNU/Linux 12 (bookworm)

Configured using:
 'configure --enable-checking=yes --with-xpm=ifavailable
 --with-gif=ifavailable 'CFLAGS=-g -O1''

Configured features:
CAIRO DBUS FREETYPE GLIB GMP GNUTLS GSETTINGS HARFBUZZ JPEG LIBSELINUX
LIBSYSTEMD LIBXML2 MODULES NOTIFY INOTIFY PDUMPER PNG SECCOMP SOUND
SQLITE3 THREADS TIFF TOOLKIT_SCROLL_BARS WEBP X11 XDBE XIM XINPUT2 GTK3
ZLIB

Important settings:
  value of $LANG: C.UTF-8
  locale-coding-system: utf-8-unix

--=-=-=--




Acknowledgement sent to Helmut Eller <eller.helmut@HIDDEN>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs@HIDDEN. Full text available.
Report forwarded to bug-gnu-emacs@HIDDEN:
bug#65211; Package emacs. Full text available.
Please note: This is a static page, with minimal formatting, updated once a day.
Click here to see this page with the latest information and nicer formatting.
Last modified: Fri, 8 Sep 2023 17:30:02 UTC

GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997 nCipher Corporation Ltd, 1994-97 Ian Jackson.