Skip to content

Commit

Permalink
lua: avoid tarantool console quit if sigint signal sending
Browse files Browse the repository at this point in the history
Tarantool console quits if you type Ctrl+C.

This patch fixes console behavior on typing Ctrl+C. The patch is intended to make user experience of using tarantool console better.

Relates to #2717
  • Loading branch information
vr009 committed Nov 22, 2021
1 parent 71a789f commit 3572eb0
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
#include "ssl_cert_paths_discover.h"
#include "core/errinj.h"


static pid_t master_pid = getpid();
static struct pidfh *pid_file_handle;
static char *script = NULL;
Expand Down Expand Up @@ -173,11 +174,31 @@ signal_cb(ev_loop *loop, struct ev_signal *w, int revents)
* a bug that the server suddenly died. Make such case
* explicit in the log.
*/

if (pid_file)
say_crit("got signal %d - %s", w->signum, strsignal(w->signum));
tarantool_exit(0);
}

static void
signal_int_cb(ev_loop *loop, struct ev_signal *w, int revents)
{
(void) loop;
(void) w;
(void) revents;

if (pid_file)
say_crit("got signal %d - %s", w->signum, strsignal(w->signum));

/* Regenerate the prompt on a newline. */
printf("\n");
rl_on_new_line();

/* Clear the previous text. */
rl_replace_line("", 0);
rl_redisplay();
}

static void
signal_sigwinch_cb(ev_loop *loop, struct ev_signal *w, int revents)
{
Expand Down Expand Up @@ -251,7 +272,7 @@ signal_init(void)
crash_signal_init();

ev_signal_init(&ev_sigs[0], sig_checkpoint, SIGUSR1);
ev_signal_init(&ev_sigs[1], signal_cb, SIGINT);
ev_signal_init(&ev_sigs[1], signal_int_cb, SIGINT);
ev_signal_init(&ev_sigs[2], signal_cb, SIGTERM);
ev_signal_init(&ev_sigs[3], signal_sigwinch_cb, SIGWINCH);
ev_signal_init(&ev_sigs[4], say_logrotate, SIGHUP);
Expand Down
37 changes: 37 additions & 0 deletions test/app-tap/gh-2717-no-quit-sigint.test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env tarantool

local fiber = require('fiber')
local tap = require('tap')
local popen = require('popen')
local process_timeout = require('process_timeout')

--
-- gh-2717: tarantool quit on sigint
--

local TARANTOOL_PATH = arg[-1]

local res = tap.test('gh-2717', function(test)
test:plan(1)
local n = 2
local prompt = "tarantool>"
local ph = popen.shell(TARANTOOL_PATH .. " -i", 'r')
assert(ph.pid, "popen error while executing" .. TARANTOOL_PATH)

local res = "tarantool> \n"
for i = 1, n do
fiber.sleep(0.5)
ph:signal(popen.signal.SIGINT)
fiber.sleep(0.5)

res = res .. "tarantool> \n"
end
local output = ph:read()
assert(process_timeout.process_is_alive(ph.pid), " process stopped by SIGINT")

ph:close()
print(output)

test:like(output, prompt .. " \n" .. prompt, " SIGINT doesn't kill the process ")
end)
os.exit(res and 0 or 1)

0 comments on commit 3572eb0

Please sign in to comment.