feat(editor): 'autowriteall' on SIGHUP/SIGQUIT #32843

Problem:
Upon receiving a deadly signal, Nvim doesn't write buffers even if
the option 'autowriteall' is set.

Solution:
Write to all writable buffers upon SIGHUP or SIGQUIT (but not
SIGTERM), if the option 'autowriteall' is set.

Co-authored-by: Justin M. Keyes <justinkz@gmail.com>
This commit is contained in:
Skoh
2025-03-31 15:14:45 +02:00
committed by GitHub
parent 2e5958186a
commit 4dabeff308
3 changed files with 79 additions and 8 deletions

View File

@ -134,6 +134,7 @@ OPTIONS
• 'completefuzzycollect' enables fuzzy collection of candidates for (some)
|ins-completion| modes.
• 'autowriteall' write all buffers upon receiving `SIGHUP`, `SIGQUIT` or `SIGTSTP`.
PERFORMANCE

View File

@ -12,16 +12,18 @@
#include "nvim/eval.h"
#include "nvim/event/defs.h"
#include "nvim/event/signal.h"
#include "nvim/ex_cmds2.h"
#include "nvim/globals.h"
#include "nvim/log.h"
#include "nvim/main.h"
#include "nvim/option_vars.h"
#include "nvim/os/signal.h"
#ifdef SIGPWR
# include "nvim/memline.h"
#endif
static SignalWatcher spipe, shup, squit, sterm, susr1, swinch;
static SignalWatcher spipe, shup, squit, sterm, susr1, swinch, ststp;
#ifdef SIGPWR
static SignalWatcher spwr;
#endif
@ -48,6 +50,7 @@ void signal_init(void)
signal_watcher_init(&main_loop, &shup, NULL);
signal_watcher_init(&main_loop, &squit, NULL);
signal_watcher_init(&main_loop, &sterm, NULL);
signal_watcher_init(&main_loop, &ststp, NULL);
#ifdef SIGPWR
signal_watcher_init(&main_loop, &spwr, NULL);
#endif
@ -67,6 +70,7 @@ void signal_teardown(void)
signal_watcher_close(&shup, NULL);
signal_watcher_close(&squit, NULL);
signal_watcher_close(&sterm, NULL);
signal_watcher_close(&ststp, NULL);
#ifdef SIGPWR
signal_watcher_close(&spwr, NULL);
#endif
@ -88,6 +92,9 @@ void signal_start(void)
signal_watcher_start(&squit, on_signal, SIGQUIT);
#endif
signal_watcher_start(&sterm, on_signal, SIGTERM);
#ifdef SIGTSTP
signal_watcher_start(&ststp, on_signal, SIGTSTP);
#endif
#ifdef SIGPWR
signal_watcher_start(&spwr, on_signal, SIGPWR);
#endif
@ -109,6 +116,7 @@ void signal_stop(void)
signal_watcher_stop(&squit);
#endif
signal_watcher_stop(&sterm);
signal_watcher_stop(&ststp);
#ifdef SIGPWR
signal_watcher_stop(&spwr);
#endif
@ -143,6 +151,10 @@ static char *signal_name(int signum)
#endif
case SIGTERM:
return "SIGTERM";
#ifdef SIGTSTP
case SIGTSTP:
return "SIGTSTP";
#endif
#ifdef SIGQUIT
case SIGQUIT:
return "SIGQUIT";
@ -177,6 +189,10 @@ static void deadly_signal(int signum)
snprintf(IObuff, IOSIZE, "Nvim: Caught deadly signal '%s'\n", signal_name(signum));
if (p_awa && signum != SIGTERM) {
autowrite_all();
}
// Preserve files and exit.
preserve_exit(IObuff);
}
@ -197,6 +213,9 @@ static void on_signal(SignalWatcher *handle, int signum, void *data)
break;
#endif
case SIGTERM:
#ifdef SIGTSTP
case SIGTSTP:
#endif
#ifdef SIGQUIT
case SIGQUIT:
#endif

View File

@ -8,39 +8,90 @@ local fn = n.fn
local next_msg = n.next_msg
local is_os = t.is_os
local skip = t.skip
local read_file = t.read_file
local feed = n.feed
local retry = t.retry
if skip(is_os('win'), 'Only applies to POSIX systems') then
return
end
local function posix_kill(signame, pid)
os.execute('kill -s ' .. signame .. ' -- ' .. pid .. ' >/dev/null')
end
describe("'autowriteall' on signal exit", function()
before_each(clear)
local function test_deadly_sig(signame, awa, should_write)
local testfile = 'Xtest_SIG' .. signame .. (awa and '_awa' or '_noawa')
local teststr = 'Testaaaaaaa'
finally(function()
os.remove(testfile)
end)
if awa then
command('set autowriteall')
end
command('edit ' .. testfile)
feed('i' .. teststr)
vim.uv.kill(fn.getpid(), signame)
retry(nil, 5000, function()
eq((should_write and (teststr .. '\n') or nil), read_file(testfile))
end)
end
it('dont write if SIGTERM & awa on', function()
test_deadly_sig('sigterm', true, false)
end)
it('dont write if SIGTERM & awa off', function()
test_deadly_sig('sigterm', false, false)
end)
it('write if SIGHUP & awa on', function()
test_deadly_sig('sighup', true, true)
end)
it('dont write if SIGHUP & awa off', function()
test_deadly_sig('sighup', false, false)
end)
it('write if SIGTSTP & awa on', function()
test_deadly_sig('sigtstp', true, true)
end)
it('dont write if SIGTSTP & awa off', function()
test_deadly_sig('sigtstp', false, false)
end)
it('write if SIGQUIT & awa on', function()
test_deadly_sig('sigquit', true, true)
end)
it('dont write if SIGQUIT & awa off', function()
test_deadly_sig('sigquit', false, false)
end)
end)
describe('autocmd Signal', function()
before_each(clear)
it('matches *', function()
command('autocmd Signal * call rpcnotify(1, "foo")')
posix_kill('USR1', fn.getpid())
vim.uv.kill(fn.getpid(), 'sigusr1')
eq({ 'notification', 'foo', {} }, next_msg())
end)
it('matches SIGUSR1', function()
command('autocmd Signal SIGUSR1 call rpcnotify(1, "foo")')
posix_kill('USR1', fn.getpid())
vim.uv.kill(fn.getpid(), 'sigusr1')
eq({ 'notification', 'foo', {} }, next_msg())
end)
it('matches SIGWINCH', function()
command('autocmd Signal SIGWINCH call rpcnotify(1, "foo")')
posix_kill('WINCH', fn.getpid())
vim.uv.kill(fn.getpid(), 'sigwinch')
eq({ 'notification', 'foo', {} }, next_msg())
end)
it('does not match unknown patterns', function()
command('autocmd Signal SIGUSR2 call rpcnotify(1, "foo")')
posix_kill('USR1', fn.getpid())
vim.uv.kill(fn.getpid(), 'sigusr2')
eq(nil, next_msg(500))
end)
end)