From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from [87.239.111.99] (localhost [127.0.0.1]) by dev.tarantool.org (Postfix) with ESMTP id 81ABE6F860; Tue, 24 Nov 2026 17:23:32 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 81ABE6F860 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=tarantool.org; s=dev; t=1795530212; bh=XJKHchlxF4V/VFYVck8eHpep4zJ5np4rJFjlSiPu3Qc=; h=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=czOnN2Uvi3h7Wct1MmcG02nQthHx9hCAeBrU0I2M6Cu3yFEkrcDGSjvj6NcbaBfbk rAUB9+OgZdLPzZkXodMG9pbGcm4GomTdcD+yBA4DoMUsbY8ZTqLJCQ7CDf7yoLee5Y X8a6lM/64CawAk+SyQunadWtyfjg08R5DEzbqd30= Received: from send152.i.mail.ru (send152.i.mail.ru [89.221.237.247]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 35E816F860 for ; Tue, 24 Nov 2026 17:22:33 +0300 (MSK) DKIM-Filter: OpenDKIM Filter v2.11.0 dev.tarantool.org 35E816F860 Received: by exim-smtp-56bb8ff957-dctjt with esmtpa (envelope-from ) id 1xVrPj-000000002sc-3KUM; Tue, 24 Nov 2026 17:22:32 +0300 To: Sergey Kaplun , Sergey Bronnikov Date: Tue, 24 Nov 2026 17:22:26 +0300 Message-ID: <18455ea22145b5eedb48285c4662388f5296b6cb.1795529449.git.e.temirgaleev@tarantool.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Mailru-Src: smtp X-4EC0790: 10 X-7564579A: 646B95376F6C166E X-77F55803: 4F1203BC0FB41BD9DFC18640EC9820970FA1CA492B8852C2D51BE0D88F5B032D182A05F5380850402CB330BAF970A2573DE06ABAFEAF670526D73C0F1AED20C8B1581956376A59BA239A53BAF60AA5ED X-7FA49CB5: FF5795518A3D127A4AD6D5ED66289B5278DA827A17800CE7B9D6DADD6B53929DEA1F7E6F0F101C67BD4B6F7A4D31EC0BCC500DACC3FED6E28638F802B75D45FF8AA50765F7900637AC83A81C8FD4AD23D82A6BABE6F325AC2E85FA5F3EDFCBAA7353EFBB5533756603DA5D6BE41EFC0D68FC7D1ECCFE08C65BA938FCFC953F775EC3C2EFEFDC2087389733CBF5DBD5E9D5E8D9A59859A8B6A70DDFFB3186CBC5CC7F00164DA146DA6F5DAA56C3B73B237318B6A418E8EAB8090A508E0FED6299176DF2183F8FC7C0C43C4D2AE8146675B3661434B16C20ACC84D3B47A649675FE827F84554CEF5019E625A9149C048EE9ECD01F8117BC8BEE2021AF6380DFAD18AA50765F790063735872C767BF85DA227C277FBC8AE2E8B486AF2577248527A75ECD9A6C639B01B4E70A05D1297E1BBCB5012B2E24CD356 X-C1DE0DAB: 0D63561A33F958A532A5D8722AF87FEA5002B1117B3ED6967D3CBEAB3B5BD88E1BDDAE3D1EA49BEA823CB91A9FED034534781492E4B8EEADD3CF082B023A3D3CC79554A2A72441328621D336A7BC284946AD531847A6065A17B107DEF921CE79BDAD6C7F3747799A X-C8649E89: 1C3962B70DF3F0AD73CAD6646DEDE191716CD42B3DD1D34CAB70F9BE574AE9C625B6776AC983F447FC0B9F89525902EE6F57B2FD27647F25E66C117BDB76D659718EEF4E7E13E5716BBCC42E5D6DF0738923171C736F2C3CA31F0EF68E7C1F6CED85548C6B841A55B8341EE9D5BE9A0AB0197A28E98A3F38D6FDF57D9C0C4EF68AB93074A3E40D8BC7CEAA0681F5848F4C41F94D744909CEEF9E35A692215F79327F81E6EEE0AEB67A0F9F91F029B6AF33F2C28C22F508233FCF178C6DD14203 X-D57D3AED: 3ZO7eAau8CL7WIMRKs4sN3D3tLDjz0dLbV79QFUyzQ2Ujvy7cMT6pYYqY16iZVKkSc3dCLJ7zSJH7+u4VD18S7Vl4ZUrpaVfd2+vE6kuoey4m4VkSEu53w8ahmwBjZKM/YPHZyZHvz5uv+WouB9+ObcCpyrx6l7KImUglyhkEat/+ysWwi0gdhEs0JGjl6ggRWTy1haxBpVdbIX1nthFXOcIETfglQORZ0zpDET4Zrk3igikrdHlWBiTkGLuVRw9DLEd9SnCO68= X-Mailru-Sender: 015BA6FC0EFA223AFF5DE35D6D355F437C77173CFBC70B460841F56765D88C40B9A1124361ED3C19A5E387BB0F8653968C34D365DA2E1307B60569B44082A3A3197AF8DA6C69D34BA7BD66BEAC8CCB0C4198E0F3ECE9B5443453F38A29522196 X-Mras: Ok Subject: [Tarantool-patches] [PATCH luajit v4 2/2] Improve CLI signal handling on POSIX. X-BeenThere: tarantool-patches@dev.tarantool.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , From: Evgeniy Temirgaleev via Tarantool-patches Reply-To: Evgeniy Temirgaleev Cc: tarantool-patches@dev.tarantool.org Errors-To: tarantool-patches-bounces@dev.tarantool.org Sender: "Tarantool-patches" From: Mike Pall (cherry picked from commit 423ac2144b0447c925596f4d93e073ce501ed4e1) An obsolete signal() function in glibc is implemented as a wrapper for POSIX sigaction() call with BSD semantics. In particular, it provides an SA_RESTART flag for SIGINT handler [1-2]. The latter makes certain system calls, e.g. read() or wait(), restartable across signals [3]. The patch switches from signal() to sigaction() for POSIX systems to obtain an explicit control of the signal flag set, which must not contain SA_RESTART flag. (Actually, the empty flag set is used.) 1. https://man7.org/linux/man-pages/man2/signal.2.html 2. https://ftp.gnu.org/gnu/glibc/glibc-2.44.tar.xz (sysdeps/posix/signal.c, line 44) 3. https://man7.org/linux/man-pages/man2/sigaction.2.html Temir Galeev: * added the description and the test for the problem Part of tarantool/tarantool#13442 --- src/luajit.c | 23 +++++-- ...ket-fix-cli-posix-sigint-handling.test.lua | 65 +++++++++++++++++++ .../script.lua | 32 +++++++++ 3 files changed, 116 insertions(+), 4 deletions(-) create mode 100644 test/tarantool-tests/lj-noticket-fix-cli-posix-sigint-handling.test.lua create mode 100644 test/tarantool-tests/lj-noticket-fix-cli-posix-sigint-handling/script.lua diff --git a/src/luajit.c b/src/luajit.c index d9d85361..d35bea54 100644 --- a/src/luajit.c +++ b/src/luajit.c @@ -35,6 +35,21 @@ #if !LJ_TARGET_CONSOLE #include + +#if LJ_TARGET_POSIX +/* Improve signal handling on POSIX. Try CTRL-C on: luajit -e 'io.read()' */ +static void signal_set(int sig, void (*h)(int)) +{ + struct sigaction sa; + memset(&sa, 0, sizeof(sa)); + sa.sa_handler = h; + sigemptyset(&sa.sa_mask); + sigaction(sig, &sa, NULL); +} +#else +#define signal_set signal +#endif + #endif static lua_State *globalL = NULL; @@ -54,8 +69,8 @@ static void lstop(lua_State *L, lua_Debug *ar) static void laction(int i) { - signal(i, SIG_DFL); /* if another SIGINT happens before lstop, - terminate process (default action) */ + /* Terminate process if another SIGINT happens (double CTRL-C). */ + signal_set(i, SIG_DFL); lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1); } #endif @@ -129,11 +144,11 @@ static int docall(lua_State *L, int narg, int clear) lua_pushcfunction(L, traceback); /* push traceback function */ lua_insert(L, base); /* put it under chunk and args */ #if !LJ_TARGET_CONSOLE - signal(SIGINT, laction); + signal_set(SIGINT, laction); #endif status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base); #if !LJ_TARGET_CONSOLE - signal(SIGINT, SIG_DFL); + signal_set(SIGINT, SIG_DFL); #endif lua_remove(L, base); /* remove traceback function */ /* force a complete garbage collection in case of errors */ diff --git a/test/tarantool-tests/lj-noticket-fix-cli-posix-sigint-handling.test.lua b/test/tarantool-tests/lj-noticket-fix-cli-posix-sigint-handling.test.lua new file mode 100644 index 00000000..9d1500c2 --- /dev/null +++ b/test/tarantool-tests/lj-noticket-fix-cli-posix-sigint-handling.test.lua @@ -0,0 +1,65 @@ +local tap = require('tap') + +-- The test checks that LuaJIT CLI stop hook is executed on +-- SIGINT on a blocking system call (read(), waitpid(), etc.). +-- +-- The test runs LuaJIT CLI with a script, which prints its PID +-- as a ready marker and blocks on a read() call. After the +-- child's PID number is read from child's pipe, the test +-- sends SIGINT to the child. +-- With a patch applied (no SA_RESTART flag for SIGINT handler) +-- read() call is interrupted and the CLI's Lua stop hook +-- is executed. +-- Without a patch (SA_RESTART flag is present for SIGINT +-- handler) read() call is restarted, the test and the child are +-- blocked on a pipe read until the child is terminated by +-- SIGALRM. The test reads nothing from the child and fails. + +local test = tap.test('lj-noticket-fix-cli-posix-sigint-handling'):skipcond({ + -- The patched code is not present in Tarantool binary: + ["Disabled because Tarantool uses its own signal handling"] = _TARANTOOL, + ['Disabled on non-POSIX systems'] = jit.os ~= 'BSD' and + jit.os ~= 'Linux' and + jit.os ~= 'OSX' and + jit.os ~= 'POSIX', +}) + +local ffi = require('ffi') +ffi.cdef[[ + int kill(int pid, int sig); +]] +local SIGINT = 2 + +test:plan(1) +local utils = require('utils') +local script = utils.exec.test_data(arg, 'script.lua') +local cmd = ('exec %s %s 2>&1'):format(utils.exec.luacmd(arg), script) +local proc = io.popen(cmd) + +local MARKER = 'CHILD_PID:' +local RMARKER = '^' .. MARKER .. '(%d+)$' +-- The child prints its PID after the CLI has installed the SIGINT +-- handler, so it is a readiness marker. Skip possible noise +-- lines, collect pre-marker output for error reporting. +local lines_for_diag = {} +local pid, line +repeat + line = proc:read('*l') + if line ~= nil then + table.insert(lines_for_diag, line) + local match = line:match(RMARKER) + if match ~= nil then pid = tonumber(match) end + end +until line == nil or pid ~= nil +if pid == nil then + test:diag('child output: %s', table.concat(lines_for_diag, '\n')) +end +assert(pid ~= nil, 'the child process must report its PID') + +local res = ffi.C.kill(pid, SIGINT) +assert(res == 0, 'SIGINT delivery failed') +local output = proc:read('*a') +proc:close() +test:diag('child output: %s', output) +test:like(output, 'interrupted!', 'SIGINT interrupts the blocking call') +test:done(true) diff --git a/test/tarantool-tests/lj-noticket-fix-cli-posix-sigint-handling/script.lua b/test/tarantool-tests/lj-noticket-fix-cli-posix-sigint-handling/script.lua new file mode 100644 index 00000000..47d668b5 --- /dev/null +++ b/test/tarantool-tests/lj-noticket-fix-cli-posix-sigint-handling/script.lua @@ -0,0 +1,32 @@ +local ffi = require('ffi') + +ffi.cdef[[ + int pipe(int *fds); + int getpid(void); + long read(int fd, void *buf, size_t count); + unsigned int alarm(unsigned int seconds); +]] + +-- The script runs via `io.popen(prog, 'r')` by the test process +-- and the child's stdout is connected to the parent's pipe read, +-- but the child's stdin is inherited from the test process and +-- may be `/dev/null` or closed, so a read from it can return +-- EOF immediately. +-- Hence a new pipe is created and its read end will block. +local fds = ffi.new('int[2]') +assert(ffi.C.pipe(fds) == 0) + +-- Fail instead of hanging forever if SIGINT does not break +-- the call. (SIGALRM isn't handled by CLI.) +ffi.C.alarm(10) + +-- The CLI installs the SIGINT handler before the script +-- starts, so this line is the readiness marker for the test. +local MARKER = 'CHILD_PID:' +io.write(MARKER, ffi.C.getpid(), '\n') +io.flush() + +-- Block in read(2) until SIGINT. Without SA_RESTART it returns +-- EINTR and the CLI runs the Lua hook, which raises +-- "interrupted!". +ffi.C.read(fds[0], ffi.new('char[1]'), 1) -- 2.49.0