comparison plugins/mod_posix.lua @ 13452:69faf3552d52

mod_posix: Move POSIX signal handling into util.startup to avoid race When libunbound is initialized, it spawns a thread to work in. In case a module initializes libunbound, e.g. by triggering a s2s connection, Prosody would not handle signals, instead immediately quit on e.g. the reload (SIGHUP) signal. Likely because the libunbound thread would not have inherited the signal mask from the main Prosody thread. Thanks Menel, riau and franck-x for reporting and help narrowing down
author Kim Alvefur <zash@zash.se>
date Sat, 02 Mar 2024 13:23:24 +0100
parents 74b9e05af71e
children c673ff1075bd
comparison
equal deleted inserted replaced
13451:4a9a69659727 13452:69faf3552d52
13 if pposix._VERSION ~= want_pposix_version then 13 if pposix._VERSION ~= want_pposix_version then
14 module:log("warn", "Unknown version (%s) of binary pposix module, expected %s." 14 module:log("warn", "Unknown version (%s) of binary pposix module, expected %s."
15 .. "Perhaps you need to recompile?", tostring(pposix._VERSION), want_pposix_version); 15 .. "Perhaps you need to recompile?", tostring(pposix._VERSION), want_pposix_version);
16 end 16 end
17 17
18 local have_signal, signal = pcall(require, "prosody.util.signal");
19 if not have_signal then
20 module:log("warn", "Couldn't load signal library, won't respond to SIGTERM");
21 end
22 18
23 local lfs = require "lfs"; 19 local lfs = require "lfs";
24 local stat = lfs.attributes; 20 local stat = lfs.attributes;
25 21
26 local prosody = _G.prosody; 22 local prosody = _G.prosody;
122 -- Not going to daemonize, so write the pid of this process 118 -- Not going to daemonize, so write the pid of this process
123 write_pidfile(); 119 write_pidfile();
124 end 120 end
125 121
126 module:hook("server-stopped", remove_pidfile); 122 module:hook("server-stopped", remove_pidfile);
127
128 -- Set signal handlers
129 if have_signal then
130 module:add_timer(0, function ()
131 signal.signal("SIGTERM", function ()
132 module:log("warn", "Received SIGTERM");
133 prosody.main_thread:run(function ()
134 prosody.unlock_globals();
135 prosody.shutdown("Received SIGTERM");
136 prosody.lock_globals();
137 end);
138 end);
139
140 signal.signal("SIGHUP", function ()
141 module:log("info", "Received SIGHUP");
142 prosody.main_thread:run(function ()
143 prosody.reload_config();
144 end);
145 -- this also reloads logging
146 end);
147
148 signal.signal("SIGINT", function ()
149 module:log("info", "Received SIGINT");
150 prosody.main_thread:run(function ()
151 prosody.unlock_globals();
152 prosody.shutdown("Received SIGINT");
153 prosody.lock_globals();
154 end);
155 end);
156
157 signal.signal("SIGUSR1", function ()
158 module:log("info", "Received SIGUSR1");
159 module:fire_event("signal/SIGUSR1");
160 end);
161
162 signal.signal("SIGUSR2", function ()
163 module:log("info", "Received SIGUSR2");
164 module:fire_event("signal/SIGUSR2");
165 end);
166 end);
167 end
168
169 -- For other modules to reference
170 features = {
171 signal_events = true;
172 };