GNU bug report logs - #75949
kill-buffer-quit-windows breaks kill-buffer-hook

Previous Next

Package: emacs;

Reported by: Juri Linkov <juri <at> linkov.net>

Date: Thu, 30 Jan 2025 17:14:02 UTC

Severity: normal

Fixed in version 31.0.50

Done: Juri Linkov <juri <at> linkov.net>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 75949 in the body.
You can then email your comments to 75949 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to rudalics <at> gmx.at, bug-gnu-emacs <at> gnu.org:
bug#75949; Package emacs. (Thu, 30 Jan 2025 17:14:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Juri Linkov <juri <at> linkov.net>:
New bug report received and forwarded. Copy sent to rudalics <at> gmx.at, bug-gnu-emacs <at> gnu.org. (Thu, 30 Jan 2025 17:14:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Juri Linkov <juri <at> linkov.net>
To: bug-gnu-emacs <at> gnu.org
Subject: kill-buffer-quit-windows breaks kill-buffer-hook
Date: Thu, 30 Jan 2025 19:04:09 +0200
Please try to evaluate in the *scratch* buffer:

(progn
  (setopt kill-buffer-quit-windows t)
  (view-echo-area-messages)
  (pop-to-buffer-same-window (get-buffer-create "foo"))
  (add-hook 'kill-buffer-hook (lambda () (message "%S" (current-buffer))))
  (add-hook 'kill-buffer-hook (lambda () (kill-buffer (get-buffer "bar")))
	    nil t)
  (display-buffer (get-buffer-create "bar"))
  (switch-to-buffer (get-buffer "*scratch*"))
  (kill-buffer (get-buffer "foo")))

Then observe in the *Messages* buffer that the hook 'kill-buffer-hook'
is run in the *scratch* buffer that is never killed.

Afterwards try to set kill-buffer-quit-windows back to the default value nil,
and evaluate again.  Then everything is correct.

It seems that kill-buffer-quit-windows somehow messes up the current buffer
while running 'kill-buffer-hook'.

This is a distilled test case from the real problem
in treesit--explorer-kill-explorer-buffer
that prevents (add-hook 'kill-buffer-hook #'save-place-to-alist)
from saving places in the right buffer.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#75949; Package emacs. (Fri, 31 Jan 2025 08:50:02 GMT) Full text and rfc822 format available.

Message #8 received at 75949 <at> debbugs.gnu.org (full text, mbox):

From: martin rudalics <rudalics <at> gmx.at>
To: Juri Linkov <juri <at> linkov.net>, 75949 <at> debbugs.gnu.org
Subject: Re: bug#75949: kill-buffer-quit-windows breaks kill-buffer-hook
Date: Fri, 31 Jan 2025 09:49:48 +0100
> Then observe in the *Messages* buffer that the hook 'kill-buffer-hook'
> is run in the *scratch* buffer that is never killed.
>
> Afterwards try to set kill-buffer-quit-windows back to the default value nil,
> and evaluate again.  Then everything is correct.
>
> It seems that kill-buffer-quit-windows somehow messes up the current buffer
> while running 'kill-buffer-hook'.

I checked in a fix.  In general, no function on 'kill-buffer-hook' can
be sure that the buffer to be killed is current because another function
on that hook might have changed the current buffer (or even killed it)
before.  That promise, like similar ones in
'window-configuration-change-hook', should never have been made.

An appropriate fix could be possibly made in run_hook_with_args.  But
that might change behaviors that rely on hook-2 running after hook-1
changed the current buffer.  Ideally, we'd have 'kill-buffer-functions'
with the buffer to be killed as sole argument (and the function on the
hook would still have to check whether that buffer is alive).

Thanks, martin




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#75949; Package emacs. (Sat, 01 Feb 2025 17:28:02 GMT) Full text and rfc822 format available.

Message #11 received at 75949 <at> debbugs.gnu.org (full text, mbox):

From: Juri Linkov <juri <at> linkov.net>
To: martin rudalics <rudalics <at> gmx.at>
Cc: 75949 <at> debbugs.gnu.org
Subject: Re: bug#75949: kill-buffer-quit-windows breaks kill-buffer-hook
Date: Sat, 01 Feb 2025 19:25:47 +0200
close 75949 31.0.50
thanks

>> Then observe in the *Messages* buffer that the hook 'kill-buffer-hook'
>> is run in the *scratch* buffer that is never killed.
>>
>> Afterwards try to set kill-buffer-quit-windows back to the default value nil,
>> and evaluate again.  Then everything is correct.
>>
>> It seems that kill-buffer-quit-windows somehow messes up the current buffer
>> while running 'kill-buffer-hook'.
>
> I checked in a fix.

Thanks, I confirm there is no problem anymore.

> In general, no function on 'kill-buffer-hook' can be sure
> that the buffer to be killed is current because another function
> on that hook might have changed the current buffer (or even killed it)
> before.  That promise, like similar ones in
> 'window-configuration-change-hook', should never have been made.
>
> An appropriate fix could be possibly made in run_hook_with_args.  But
> that might change behaviors that rely on hook-2 running after hook-1
> changed the current buffer.  Ideally, we'd have 'kill-buffer-functions'
> with the buffer to be killed as sole argument (and the function on the
> hook would still have to check whether that buffer is alive).

Since a new argument can't be added, another variant would be at least
to set the current buffer before every call of the hook functions.
I.e. instead of

  (run-hooks 'kill-buffer-hook)

to do something like

  (run-hook-wrapped 'kill-buffer-hook
                    (lambda (fun)
                      (with-current-buffer current-buffer
                        (funcall fun))
                      nil))

But this won't help when buffers are switched inside hook functions.




bug marked as fixed in version 31.0.50, send any further explanations to 75949 <at> debbugs.gnu.org and Juri Linkov <juri <at> linkov.net> Request was from Juri Linkov <juri <at> linkov.net> to control <at> debbugs.gnu.org. (Sat, 01 Feb 2025 17:28:03 GMT) Full text and rfc822 format available.

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#75949; Package emacs. (Sun, 02 Feb 2025 08:53:02 GMT) Full text and rfc822 format available.

Message #16 received at 75949 <at> debbugs.gnu.org (full text, mbox):

From: martin rudalics <rudalics <at> gmx.at>
To: Juri Linkov <juri <at> linkov.net>
Cc: 75949 <at> debbugs.gnu.org
Subject: Re: bug#75949: kill-buffer-quit-windows breaks kill-buffer-hook
Date: Sun, 2 Feb 2025 09:52:49 +0100
> Since a new argument can't be added, another variant would be at least
> to set the current buffer before every call of the hook functions.
> I.e. instead of
>
>    (run-hooks 'kill-buffer-hook)
>
> to do something like
>
>    (run-hook-wrapped 'kill-buffer-hook
>                      (lambda (fun)
>                        (with-current-buffer current-buffer
>                          (funcall fun))
>                        nil))
>
> But this won't help when buffers are switched inside hook functions.

The problem is that we run hooks completely in C so wrapping is at least
not that simple.  Whatever it is, nesting 'kill-buffer-hook' as in

 (add-hook 'kill-buffer-hook (lambda () (kill-buffer (get-buffer "bar")))
	    nil t)

is not really recommended.

martin




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#75949; Package emacs. (Mon, 03 Feb 2025 07:59:02 GMT) Full text and rfc822 format available.

Message #19 received at 75949 <at> debbugs.gnu.org (full text, mbox):

From: Juri Linkov <juri <at> linkov.net>
To: martin rudalics <rudalics <at> gmx.at>
Cc: 75949 <at> debbugs.gnu.org
Subject: Re: bug#75949: kill-buffer-quit-windows breaks kill-buffer-hook
Date: Mon, 03 Feb 2025 09:52:47 +0200
> Whatever it is, nesting 'kill-buffer-hook' as in
>
>  (add-hook 'kill-buffer-hook (lambda () (kill-buffer (get-buffer "bar")))
> 	    nil t)
>
> is not really recommended.

What is a recommended way to do this?  This is a valid use case
when killing one buffer requires also killing a dependent buffer.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#75949; Package emacs. (Mon, 03 Feb 2025 08:34:03 GMT) Full text and rfc822 format available.

Message #22 received at 75949 <at> debbugs.gnu.org (full text, mbox):

From: martin rudalics <rudalics <at> gmx.at>
To: Juri Linkov <juri <at> linkov.net>
Cc: 75949 <at> debbugs.gnu.org
Subject: Re: bug#75949: kill-buffer-quit-windows breaks kill-buffer-hook
Date: Mon, 3 Feb 2025 09:33:45 +0100
>> Whatever it is, nesting 'kill-buffer-hook' as in
>>
>>   (add-hook 'kill-buffer-hook (lambda () (kill-buffer (get-buffer "bar")))
>> 	    nil t)
>>
>> is not really recommended.
>
> What is a recommended way to do this?  This is a valid use case
> when killing one buffer requires also killing a dependent buffer.

Any function on any hook that changes the corresponding infrastructure
should preserve the state around any such change.  The basic values that
should remain unchanged are the selected window, the selected window of
each frame and the current buffer (unless the function deletes them).

martin




bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Mon, 03 Mar 2025 12:24:10 GMT) Full text and rfc822 format available.

This bug report was last modified 111 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.