From a1dc119bfb777f813f7967256fc0835c34d2c0be Mon Sep 17 00:00:00 2001 From: Manuel Giraud Date: Thu, 3 Feb 2022 13:52:41 +0100 Subject: [PATCH 3/4] mutex on nnmail cache. --- lisp/gnus/nnmail.el | 83 ++++++++++++++++++++++++--------------------- 1 file changed, 44 insertions(+), 39 deletions(-) diff --git a/lisp/gnus/nnmail.el b/lisp/gnus/nnmail.el index af9c439280..642ca16e28 100644 --- a/lisp/gnus/nnmail.el +++ b/lisp/gnus/nnmail.el @@ -1556,6 +1556,7 @@ nnmail-message-id ;;; (defvar nnmail-cache-buffer nil) +(defvar nnmail-cache-buffer-mutex (make-mutex)) (defun nnmail-cache-open () (if (or (not nnmail-treat-duplicates) @@ -1574,22 +1575,23 @@ nnmail-cache-close (when (and nnmail-treat-duplicates (buffer-live-p nnmail-cache-buffer) (buffer-modified-p nnmail-cache-buffer)) - (with-current-buffer nnmail-cache-buffer - ;; Weed out the excess number of Message-IDs. - (goto-char (point-max)) - (when (search-backward "\n" nil t nnmail-message-id-cache-length) - (progn - (beginning-of-line) - (delete-region (point-min) (point)))) - ;; Save the buffer. - (or (file-exists-p (file-name-directory nnmail-message-id-cache-file)) - (make-directory (file-name-directory nnmail-message-id-cache-file) - t)) - (nnmail-write-region (point-min) (point-max) - nnmail-message-id-cache-file nil 'silent) - (set-buffer-modified-p nil) - (setq nnmail-cache-buffer nil) - (gnus-kill-buffer (current-buffer))))) + (with-mutex nnmail-cache-buffer-mutex + (with-current-buffer nnmail-cache-buffer + ;; Weed out the excess number of Message-IDs. + (goto-char (point-max)) + (when (search-backward "\n" nil t nnmail-message-id-cache-length) + (progn + (beginning-of-line) + (delete-region (point-min) (point)))) + ;; Save the buffer. + (or (file-exists-p (file-name-directory nnmail-message-id-cache-file)) + (make-directory (file-name-directory nnmail-message-id-cache-file) + t)) + (nnmail-write-region (point-min) (point-max) + nnmail-message-id-cache-file nil 'silent) + (set-buffer-modified-p nil) + (setq nnmail-cache-buffer nil) + (gnus-kill-buffer (current-buffer)))))) (defun nnmail-cache-insert (id grp &optional subject sender) (when (stringp id) @@ -1605,18 +1607,19 @@ nnmail-cache-insert ;; pass the first (of possibly >1) group which matches. -Josh (unless (gnus-buffer-live-p nnmail-cache-buffer) (nnmail-cache-open)) - (with-current-buffer nnmail-cache-buffer - (goto-char (point-max)) - (if (and grp (not (string= "" grp)) - (gnus-methods-equal-p gnus-command-method - (nnmail-cache-primary-mail-backend))) - (let ((regexp (if (consp nnmail-cache-ignore-groups) - (mapconcat #'identity nnmail-cache-ignore-groups - "\\|") - nnmail-cache-ignore-groups))) - (unless (and regexp (string-match regexp grp)) - (insert id "\t" grp "\n"))) - (insert id "\n")))))) + (with-mutex nnmail-cache-buffer-mutex + (with-current-buffer nnmail-cache-buffer + (goto-char (point-max)) + (if (and grp (not (string= "" grp)) + (gnus-methods-equal-p gnus-command-method + (nnmail-cache-primary-mail-backend))) + (let ((regexp (if (consp nnmail-cache-ignore-groups) + (mapconcat #'identity nnmail-cache-ignore-groups + "\\|") + nnmail-cache-ignore-groups))) + (unless (and regexp (string-match regexp grp)) + (insert id "\t" grp "\n"))) + (insert id "\n"))))))) (defun nnmail-cache-primary-mail-backend () (let ((be-list (cons gnus-select-method gnus-secondary-select-methods)) @@ -1638,14 +1641,15 @@ nnmail-cache-primary-mail-backend ;; cache. (defun nnmail-cache-fetch-group (id) (when (and nnmail-treat-duplicates nnmail-cache-buffer) - (with-current-buffer nnmail-cache-buffer - (goto-char (point-max)) - (when (search-backward id nil t) - (beginning-of-line) - (skip-chars-forward "^\n\r\t") - (unless (looking-at "[\r\n]") - (forward-char 1) - (buffer-substring (point) (point-at-eol))))))) + (with-mutex nnmail-cache-buffer-mutex + (with-current-buffer nnmail-cache-buffer + (goto-char (point-max)) + (when (search-backward id nil t) + (beginning-of-line) + (skip-chars-forward "^\n\r\t") + (unless (looking-at "[\r\n]") + (forward-char 1) + (buffer-substring (point) (point-at-eol)))))))) ;; Function for nnmail-split-fancy: look up all references in the ;; cache and if a match is found, return that group. @@ -1682,9 +1686,10 @@ nnmail-split-fancy-with-parent (defun nnmail-cache-id-exists-p (id) (when nnmail-treat-duplicates - (with-current-buffer nnmail-cache-buffer - (goto-char (point-max)) - (search-backward id nil t)))) + (with-mutex nnmail-cache-buffer-mutex + (with-current-buffer nnmail-cache-buffer + (goto-char (point-max)) + (search-backward id nil t))))) (defun nnmail-fetch-field (header) (save-excursion -- 2.35.1