Subject: bug#76473: closed (Re: bug#76473: 31.0.50; gnutls_pproc leak)

From: [email-masked] (GNU bug Tracking System)
Date: Sat, 22 Feb 2025 14:35:07 +0000

Your bug report

#76473: 31.0.50; gnutls_pproc leak

which was filed against the emacs package, has been closed.

The explanation is attached below, along with your original report.
If you require more details, please reply to [email-masked].

-- 
76473: https://debbugs.gnu.org/cgi/bugreport.cgi?bug=76473
GNU Bug Tracking System
Contact [email-masked] with problems

Subject: Re: bug#76473: 31.0.50; gnutls_pproc leak
From: Pip Cet <[email-masked]>
To: Eli Zaretskii <[email-masked]>
Cc: [email-masked], [email-masked]
Date: Sat, 22 Feb 2025 14:34:27 +0000
List-Id: <debbugs-submit.debbugs.gnu.org>

"Eli Zaretskii" <[email-masked]> writes:

>> Date: Fri, 21 Feb 2025 19:58:19 +0000
>> From: Pip Cet <[email-masked]>
>> Cc: [email-masked], [email-masked]
>>
>> "Eli Zaretskii" <[email-masked]> writes:
>>
>> >> Date: Fri, 21 Feb 2025 18:44:17 +0000
>> >> From: Pip Cet <[email-masked]>
>> >> Cc: [email-masked], [email-masked]
>> >>
>> >> "Eli Zaretskii" <[email-masked]> writes:
>> >>
>> >> >> Cc: [email-masked]
>> >> >> Date: Fri, 21 Feb 2025 18:02:34 +0000
>> >> >> From:  Pip Cet via "Bug reports for GNU Emacs,
>> >> >>  the Swiss army knife of text editors" <[email-masked]>
>> >> >>
>> >> >> > This patch would fix that:
>> >> >>
>> >> >> I believe it would!  Is anyone testing the w32/w64 + gnutls combination?
>> >> >
>> >> > In general, or on the igc branch?
>> >>
>> >> The latter.  This bug is specific to feature/igc, I think, since that's
>> >> the only branch that has gnutls_pproc.
>> >
>> > If you or Helmut tell me what to try and what to notice, I can do
>> > that.
>>
>> Can you watch the PVEC_PROCESS line (in M-x igc-stats) or the
>> xzalloc-ambig line (in M-x igc-roots-stats) while creating and
>> destroying a number of gnutls processes?
>>
>> Without the patch, the numbers should go up but never down.  With the
>> patch, all should be fine :-)
>
> I indeed see that with the patch, igc-collect and/or deleting the
> processes (in the list-processes display) makes the number of
> PVEC_PROCESS objects go down, whereas without the patch it stays at
> the same value it had before (and goes up when I invoke eww for
> another HTTPS connection).  However, even with the patch, the number
> never goes back to zero, only to 1.  Don't know if it's important or
> not.

Thanks!  Since there is no process in M-x list-processes (such as a
server process), I think it's most likely that an "ambiguous" reference
to a destroyed process survives on the bytecode stack or the C stack.
So my guess it's that's a harmless side-effect of conservative garbage
collection.

> Anyway, the patch seems to be TRT, thanks.

Thanks, Helmut!  I've changed the subject line to reference the bug
number, but otherwise what I just pushed should be identical to what you
sent!

Closing this bug.

Pip

Subject: 31.0.50; gnutls_pproc leak
From: Helmut Eller <[email-masked]>
To: [email-masked]
Date: Fri, 21 Feb 2025 16:16:42 +0100
List-Id: <debbugs-submit.debbugs.gnu.org>

On the features/igc branch, it looks like the process->gnutls_pproc
field is never freed.

This patch would fix that:

>From d13ef81273c45f60add3a7adbea8274a916f469a Mon Sep 17 00:00:00 2001
From: Helmut Eller <[email-masked]>
Date: Fri, 21 Feb 2025 15:31:58 +0100
Subject: [PATCH] Don't forget to free process->gnutls_pproc if we HAVE_MPS.

* src/gnutls.c (emacs_gnutls_deinit): Free gnutls_pproc here.
* src/alloc.c (cleanup_vector): Not here.
---
 src/alloc.c  |  7 +------
 src/gnutls.c | 11 +++++++++++
 2 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/src/alloc.c b/src/alloc.c
index 7e74fd53335..74e85a9962b 100644
--- a/src/alloc.c
+++ b/src/alloc.c
@@ -3450,12 +3450,6 @@ cleanup_vector (struct Lisp_Vector *vector)
 	hash_table_allocated_bytes -= bytes;
       }
       break;
-    case PVEC_PROCESS:
-      {
-	struct Lisp_Process *p = PSEUDOVEC_STRUCT (vector, Lisp_Process);
-	xfree (p->gnutls_pproc);
-      }
-      break;
     /* Keep the switch exhaustive.  */
     case PVEC_NORMAL_VECTOR:
     case PVEC_FREE:
@@ -3465,6 +3459,7 @@ cleanup_vector (struct Lisp_Vector *vector)
     case PVEC_WINDOW:
     case PVEC_BOOL_VECTOR:
     case PVEC_BUFFER:
+    case PVEC_PROCESS:
     case PVEC_TERMINAL:
     case PVEC_WINDOW_CONFIGURATION:
     case PVEC_OTHER:
diff --git a/src/gnutls.c b/src/gnutls.c
index 50c6bc67e33..7eaf01f6b6e 100644
--- a/src/gnutls.c
+++ b/src/gnutls.c
@@ -985,6 +985,17 @@ emacs_gnutls_deinit (Lisp_Object proc)
   if (XPROCESS (proc)->gnutls_certificates)
     gnutls_deinit_certificates (XPROCESS (proc));
 
+  struct Lisp_Process *p = XPROCESS (proc);
+  if (p->gnutls_pproc)
+    {
+#ifdef HAVE_MPS
+      igc_xfree (p->gnutls_pproc);
+#else
+      xfree (p->gnutls_pproc);
+#endif
+      p->gnutls_pproc = NULL;
+    }
+
   XPROCESS (proc)->gnutls_p = false;
   return Qt;
 }
-- 
2.39.5

Helmut

In GNU Emacs 31.0.50 (build 10, x86_64-pc-linux-gnu, GTK+ Version
 3.24.38, cairo version 1.16.0) of 2025-02-21 built on caladan
Repository revision: d13ef81273c45f60add3a7adbea8274a916f469a
Repository branch: fix-gnutls-leak
Windowing system distributor 'The X.Org Foundation', version 11.0.12401004
System Description: Debian GNU/Linux 12 (bookworm)

Configured using:
 'configure --config-cache --enable-checking=all,igc_check_fwd,igc_debug
 --with-mps=debug --with-xpm=ifavailable --with-gif=ifavailable
 --with-native-compilation=no 'CFLAGS=-g -O0 --std=gnu99
 -I/scratch/emacs/mps-install/include -L/scratch/emacs/mps-install/lib''

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