diff options
author | Kim Alvefur <zash@zash.se> | 2024-03-02 13:23:24 +0100 |
---|---|---|
committer | Kim Alvefur <zash@zash.se> | 2024-03-02 13:23:24 +0100 |
commit | 3036a689fa42a1830eb5e8181f3e53aba52cee39 (patch) | |
tree | d2406c0a02cc66634e44b35bda12da4658d05ebf /util/startup.lua | |
parent | ed0188ce815b61bac484d406371e68e4b2e52843 (diff) | |
download | prosody-3036a689fa42a1830eb5e8181f3e53aba52cee39.tar.gz prosody-3036a689fa42a1830eb5e8181f3e53aba52cee39.zip |
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
Diffstat (limited to 'util/startup.lua')
-rw-r--r-- | util/startup.lua | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/util/startup.lua b/util/startup.lua index ba2a7cf6..37f9359f 100644 --- a/util/startup.lua +++ b/util/startup.lua @@ -671,6 +671,48 @@ function startup.make_dummy_hosts() end end +function startup.hook_posix_signals() + if prosody.platform ~= "posix" then return end + local have_signal, signal = pcall(require, "prosody.util.signal"); + if not have_signal then + log("warn", "Couldn't load signal library, won't respond to SIGTERM"); + return + end + signal.signal("SIGTERM", function() + log("warn", "Received SIGTERM"); + prosody.main_thread:run(function() + prosody.unlock_globals(); + prosody.shutdown("Received SIGTERM"); + prosody.lock_globals(); + end); + end); + + signal.signal("SIGHUP", function() + log("info", "Received SIGHUP"); + prosody.main_thread:run(function() prosody.reload_config(); end); + -- this also reloads logging + end); + + signal.signal("SIGINT", function() + log("info", "Received SIGINT"); + prosody.main_thread:run(function() + prosody.unlock_globals(); + prosody.shutdown("Received SIGINT"); + prosody.lock_globals(); + end); + end); + + signal.signal("SIGUSR1", function() + log("info", "Received SIGUSR1"); + fire_event("signal/SIGUSR1"); + end); + + signal.signal("SIGUSR2", function() + log("info", "Received SIGUSR2"); + fire_event("signal/SIGUSR2"); + end); +end + function startup.cleanup() prosody.log("info", "Shutdown status: Cleaning up"); prosody.events.fire_event("server-cleanup"); @@ -748,6 +790,7 @@ function startup.prosody() startup.init_http_client(); startup.init_data_store(); startup.init_global_protection(); + startup.hook_posix_signals(); startup.prepare_to_start(); startup.notify_started(); end |