diff options
author | Skoh <101289702+SkohTV@users.noreply.github.com> | 2025-03-31 15:14:45 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-03-31 06:14:45 -0700 |
commit | 4dabeff308222307ede8e74a2bd341713a7f7d81 (patch) | |
tree | c033d839e8ef54644ebc849bfc91ab39792fcb91 | |
parent | 2e5958186aa36e90199e36de5dd9831ec6aefba3 (diff) | |
download | rneovim-4dabeff308222307ede8e74a2bd341713a7f7d81.tar.gz rneovim-4dabeff308222307ede8e74a2bd341713a7f7d81.tar.bz2 rneovim-4dabeff308222307ede8e74a2bd341713a7f7d81.zip |
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>
-rw-r--r-- | runtime/doc/news.txt | 1 | ||||
-rw-r--r-- | src/nvim/os/signal.c | 21 | ||||
-rw-r--r-- | test/functional/autocmd/signal_spec.lua | 65 |
3 files changed, 79 insertions, 8 deletions
diff --git a/runtime/doc/news.txt b/runtime/doc/news.txt index 2644bb24b2..c8fa343e46 100644 --- a/runtime/doc/news.txt +++ b/runtime/doc/news.txt @@ -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 diff --git a/src/nvim/os/signal.c b/src/nvim/os/signal.c index 11f99d0a0f..55efce0337 100644 --- a/src/nvim/os/signal.c +++ b/src/nvim/os/signal.c @@ -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 diff --git a/test/functional/autocmd/signal_spec.lua b/test/functional/autocmd/signal_spec.lua index 4416afb3ba..03e991f21c 100644 --- a/test/functional/autocmd/signal_spec.lua +++ b/test/functional/autocmd/signal_spec.lua @@ -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) |