Subject: kill-buffer fails silently when a thread exists where it's current
From: Dmitry Gutov <[email-masked]>
To: [email-masked]
Date: Wed, 12 Mar 2025 03:16:08 +0200
List-Id: <debbugs-submit.debbugs.gnu.org>

This stems from a private bug report about diff-hl when it uses a thread 
to update the fringe highlights.

To reproduce:

   1. Install, enable diff-hl-mode.
   2. (setq diff-hl-update-async t)
   3. Visit a code buffer in a (e.g.) Git repo, save it.
   4. Make an edit, don't save.
   5. Evaluate this:

   (progn
     (save-buffer)
     (kill-buffer))

Current:

The result is that the buffer is not killed. And that happens silently, 
no errors or anything. Only further examination and reading the sources 
led to understanding the reason.

Expected:

It probably should be killed. After the thread is signaled some error 
(perhaps) and is aborted. And if the buffer can't be killed, 
'kill-buffer' itself should exit with an error.

As I understand the behavior is old (2013) and comes from the 
'thread_check_current_buffer' call in Fkill_buffer. But it's not 
mentioned in kill-buffer's docstring or the manual.

Alternative repro:

If you don't have diff-hl installed, you could replace 1 and 2 with:

   (defun foo ()
     (make-thread (lambda () (sleep-for 0.1))))

   (add-hook 'after-save-hook #'foo)