Subject: bug#63311: 30.0.50; [PATCH] smtpmail-send-it split

From: Manuel Giraud <[email-masked]>
Date: Wed, 10 May 2023 13:47:11 +0200

Hi Eli,

My strategy after the previous patch is summed up by this function:

--8<---------------cut here---------------start------------->8---
(defun smtpmail-async-send-it (cleanups)
  (let ((tembuf (generate-new-buffer " smtpmail temp"))
        (smtpmail-code-conv-from
	       (if enable-multibyte-characters
	           (let ((sendmail-coding-system smtpmail-code-conv-from))
	             (select-message-coding-system)))))
    (smtpmail-prepare-mail tembuf smtpmail-code-conv-from)
    (push #'(lambda () (kill-buffer tembuf)) cleanups)
    (make-thread #'(lambda ()
                     (smtpmail-send-mail tembuf)
                     (mapc #'funcall cleanups)))))
--8<---------------cut here---------------end--------------->8---

That is, I'd like carry around a 'cleanups' list that holds all the
functions that needed to be called after the mail was sent.

I then need to propagate this 'cleanups' parameter to callers of
'smtpmail-send-it'.  The problem is that encounter code with the
following form:

--8<---------------cut here---------------start------------->8---
(unwind-protect
    (let (haha)
      (do)
      (many)
      (things)
      (smtpmail-send-it-or-one-of-its-caller)
      (run)
      (other)
      (stuff))
  (kill-some-top-level-buffer))
--8<---------------cut here---------------end--------------->8---

I'm tempted to put '(run)(other)(stuff)(kill-some-top-level-buffer)'
into the cleanups list and remove the unwind-protect but then if
something goes wrong before sending the mail the
'(kill-some-top-level-buffer)' part won't be called.  OTOH, if I let the
'(unwind-protect…  (kill-some-top-level-buffer)' in place I could shoot
myself in the foot with the code that use this buffer into the thread.

So I guess, my question is how does one mix Lisp thread with
unwind-protect?

Best regards,
-- 
Manuel Giraud