Subject: 28.0.50; Clean up nnimap server buffers?

From: Eric Abrahamsen <[email-masked]>
Date: Mon, 28 Sep 2020 16:37:15 -0700

Someone noted on gnus.general that their imap connections are frequently
broken, and they end up with a lot of dead process buffers.

I'm talking to them about maybe making the keepalive timeout
configurable, but wouldn't also be tidy to clean up dead process
buffers? How does the attached patch look?

Eric

diff --git a/lisp/gnus/nnimap.el b/lisp/gnus/nnimap.el
index d797e893f5..7f2ebe279e 100644
--- a/lisp/gnus/nnimap.el
+++ b/lisp/gnus/nnimap.el
@@ -407,8 +407,15 @@ nnimap-keepalive
 		      (time-subtract
 		       now
 		       (nnimap-last-command-time nnimap-object))))
-            (ignore-errors              ;E.g. "buffer foo has no process".
-              (nnimap-send-command "NOOP"))))))))
+	    (condition-case err
+		(process-send-string "NOOP")
+	      (error
+	       (if (string-search "has no process" (cdr err))
+		   (let ((buf (current-buffer)))
+		     (setq nnimap-process-buffers
+			   (delq buf nnimap-process-buffers))
+		     (kill-buffer buf))
+		 (signal (car err) (cdr err)))))))))))
 
 (defun nnimap-open-connection (buffer)
   ;; Be backwards-compatible -- the earlier value of nnimap-stream was
@@ -1910,6 +1917,10 @@ nnimap-find-connection
 		     '(open run)))
 	  (get-buffer-process (cadr entry))
 	(setq nnimap-connection-alist (delq entry nnimap-connection-alist))
+	(setq nnimap-process-buffers
+	      (delq (cadr entry) nnimap-process-buffers))
+	(when (buffer-live-p (cadr entry))
+	  (kill-buffer (cadr entry)))
 	nil))))
 
 ;; Leave room for `open-network-stream' to issue a couple of IMAP