Skip to content

Conversation

seandewar
Copy link
Member

@seandewar seandewar commented May 14, 2025

Problem: on_detach may be called after buf_freeall and other important
things, plus its textlock restrictions are insufficient. This can cause issues
such as leaks, internal errors and crashes.

Solution: disable buffer updates in buf_freeall, before autocommands (like the
order after #35355 and when do_ecmd reloads a buffer). Don't do so in
free_buffer_stuff; it's not safe to run user code there, and buf_freeall already
runs before then; just free them to avoid leaks if buf_freeall autocommands
registered more for some reason.

Fixes #28084
Fixes #33967
Fixes #35116

@github-actions github-actions bot added the api libnvim, Nvim RPC API label May 14, 2025
@seandewar seandewar force-pushed the on_detach-sooner branch 2 times, most recently from fd4d829 to 5ba2d12 Compare May 14, 2025 23:02
@seandewar seandewar added this to the 0.11.2 milestone May 15, 2025
@zeertzjq zeertzjq modified the milestones: 0.11.2, 0.11.3 May 30, 2025
@justinmk justinmk modified the milestones: 0.11.3, 0.11.4 Jul 14, 2025
@seandewar seandewar changed the title fix(api): call on_detach just before buffer stuff is freed fix(api): on_detach consistently before buf_freeall autocmds Aug 17, 2025
Comment on lines 787 to 791
// Disable buffer-updates for the current buffer.
buf_updates_unload(buf, false);
if (!bufref_valid(&bufref)) {
// on_detach callback deleted the buffer.
return;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on_detach has textlock set, and like the autocmds below has b_locked set here too. Makes it pretty hard to delete the buffer, but it might still be possible; it's at least consistent with below to include the check.

@seandewar seandewar marked this pull request as ready for review August 17, 2025 15:28
@seandewar seandewar force-pushed the on_detach-sooner branch 3 times, most recently from 2bc7e43 to 8b7feb8 Compare August 17, 2025 15:38
Problem: on_detach may be called after buf_freeall and other important things,
plus its textlock restrictions are insufficient. This can cause issues such as
leaks, internal errors and crashes.

Solution: disable buffer updates in buf_freeall, before autocommands (like the
order after neovim#35355 and when do_ecmd reloads a buffer). Don't do so in
free_buffer_stuff; it's not safe to run user code there, and buf_freeall already
runs before then; just free them to avoid leaks if buf_freeall autocommands
registered more for some reason.

Fixes neovim#28084
Fixes neovim#33967
Fixes neovim#35116
end)

describe('nvim_buf_attach on_detach', function()
it('is invoked before unloading buffer', function()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My test supersedes this one, so I've removed it (but kept the case when wiping the last listed non-reusable buffer)

@seandewar seandewar merged commit 2211953 into neovim:master Aug 17, 2025
52 of 54 checks passed
@seandewar seandewar deleted the on_detach-sooner branch August 17, 2025 21:32
@neovim neovim deleted a comment from neovim-backports bot Aug 17, 2025
@neovim-backports
Copy link

Successfully created backport PR for release-0.11:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api libnvim, Nvim RPC API ci:backport release-0.11

Projects

None yet

4 participants