# HG changeset patch # User jbe # Date 1496779106 -7200 # Node ID 1a4f89f4c712359281618c240c5a9d3d56295584 # Parent dec24bd2592c49f10e7ebc64cdf0b6556d4013b5 Modified behavior of moonbridge_io.signalsocket(...); Fixed usage of moonbridge_io.signalsocket(...) in moonbridge_http module diff -r dec24bd2592c -r 1a4f89f4c712 moonbridge_http.lua --- a/moonbridge_http.lua Tue Jun 06 20:19:09 2017 +0200 +++ b/moonbridge_http.lua Tue Jun 06 21:58:26 2017 +0200 @@ -203,10 +203,10 @@ local response_timeout = default("response_timeout", 3600) local drain_timeout = default("drain_timeout", 2) local poll = options.poll_function or moonbridge_io.poll - -- install SIGTERM handler: - local sigterm_socket = moonbridge_io.signalsocket("TERM") -- return socket handler: return function(socket) + -- install SIGTERM handler and initialize some variables: + local sigterm_socket = moonbridge_io.signalsocket("TERM") local socket_set = {[socket] = true} -- used for poll function local socket_term_set = { -- used for poll function [socket] = true, diff -r dec24bd2592c -r 1a4f89f4c712 moonbridge_io.c --- a/moonbridge_io.c Tue Jun 06 20:19:09 2017 +0200 +++ b/moonbridge_io.c Tue Jun 06 21:58:26 2017 +0200 @@ -66,6 +66,7 @@ #define MOONBR_IO_CHILD_MT_REGKEY "moonbridge_io_child" #define MOONBR_IO_CHILD_PT_REGKEY "moonbridge_io_child_pt" #define MOONBR_IO_SIGNALS_REGKEY "moonbridge_io_signals" +#define MOONBR_IO_SIGNALSOCKETS_REGKEY "moonbridge_io_signalsockets" #ifdef MOONBR_IO_USE_TLS @@ -1641,13 +1642,20 @@ if (socketpair(PF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, moonbr_io_signalfds+2*sig)) { luaL_error(L, "Could not create socket pair for signal queueing"); } - errno = 0; - signal(sig, moonbr_io_signalhandler); - if (errno) luaL_error(L, "Could not install signal handler (invalid signal number %i?)", sig); } - fd = dup(moonbr_io_signalfd_read(sig)); + errno = 0; + signal(sig, moonbr_io_signalhandler); + if (errno) luaL_error(L, "Could not install signal handler (invalid signal number %i?)", sig); + lua_getfield(L, LUA_REGISTRYINDEX, MOONBR_IO_SIGNALSOCKETS_REGKEY); + lua_pushinteger(L, sig); + lua_gettable(L, -2); + if (!lua_isnil(L, -1)) return 1; + fd = dup(moonbr_io_signalfd_read(sig)); /* avoid close on Lua error */ if (fd < 0) luaL_error(L, "Could not create duplicated file descriptor for signal socket"); moonbr_io_pushhandle(L, fd); + lua_pushinteger(L, sig); + lua_pushvalue(L, -2); + lua_settable(L, -5); /* store reference to signal socket in cache table */ return 1; } @@ -2110,6 +2118,9 @@ lua_pushvalue(L, -1); lua_setfield(L, -3, "signals"); lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_SIGNALS_REGKEY); + + lua_newtable(L); // signal sockets + lua_setfield(L, LUA_REGISTRYINDEX, MOONBR_IO_SIGNALSOCKETS_REGKEY); luaL_setfuncs(L, moonbr_io_module_funcs, 0); return 1; diff -r dec24bd2592c -r 1a4f89f4c712 reference.txt --- a/reference.txt Tue Jun 06 20:19:09 2017 +0200 +++ b/reference.txt Tue Jun 06 21:58:26 2017 +0200 @@ -399,15 +399,14 @@ ### moonbridge_io.signalsocket(signal) This function installs a signal handler. As argument, either the signal number -is passed (e.g. 15) or a name (e.g. "TERM"). - -The function returns a new socket object that receives a character (".") each -time a signal is received. +is passed (e.g. 15) or a name (e.g. "TERM"). The function returns a socket +object that receives a character (".") each time a signal is received. -Note that each socket object has an independent buffer. It is thus recommended -to always read all bytes, e.g. by using the expression: +The function can be called multiple times, in which case the same socket object +is returned. The returned socket should never be closed by the caller. -#(assert(sigsock:read_nb())) > 0 -- true if signal occurred +The process should not be forked after calling this function (except for +replacing the process with another program). ### moonbridge_io.tcpconnect(hostname, port)