Annotate

prosody @ 12553:cc0ec0277813 0.12

util.startup: Fix async waiting for last shutdown steps Observed problem: When shutting down prosody would immediately exit after waiting for s2s connections to close, skipping the last cleanup events and reporting the exit reason and code. This happens because prosody.main_thread is in a waiting state and queuing startup.shutdown is dispatched trough the main loop via nexttick, but since the main loop was no longer running at that point it proceeded to the end of the prosody script and exited there.
author Kim Alvefur <zash@zash.se>
date Tue, 14 Jun 2022 16:28:49 +0200
parent 12296:49ebac8a5260
child 12578:10bb58ad5583
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
455
2eeae9c314b0 main.lua -> prosody
Matthew Wild <mwild1@gmail.com>
parents: 452
diff changeset
1 #!/usr/bin/env lua
1523
841d61be198f Remove version number from copyright headers
Matthew Wild <mwild1@gmail.com>
parents: 1493
diff changeset
2 -- Prosody IM
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2877
diff changeset
3 -- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 2877
diff changeset
4 -- Copyright (C) 2008-2010 Waqas Hussain
7876
c028555866b3 prosody: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7801
diff changeset
5 --
761
67ec69001fd7 Update main prosody file, since it doesn't match *.lua pattern, and sed -i treats symlinks badly
Matthew Wild <mwild1@gmail.com>
parents: 755
diff changeset
6 -- This project is MIT/X11 licensed. Please see the
67ec69001fd7 Update main prosody file, since it doesn't match *.lua pattern, and sed -i treats symlinks badly
Matthew Wild <mwild1@gmail.com>
parents: 755
diff changeset
7 -- COPYING file in the source package for more information.
519
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 502
diff changeset
8 --
cccd610a0ef9 Insert copyright/license headers
Matthew Wild <mwild1@gmail.com>
parents: 502
diff changeset
9
3740
69f95537e9e4 prosody: Added a comment, to match prosodyctl.
Waqas Hussain <waqas20@gmail.com>
parents: 3713
diff changeset
10 -- prosody - main executable for Prosody XMPP server
69f95537e9e4 prosody: Added a comment, to match prosodyctl.
Waqas Hussain <waqas20@gmail.com>
parents: 3713
diff changeset
11
843
1d2dab41b0db prosody: Protect main loop. Dare I say crashing finally becomes impossible.
Matthew Wild <mwild1@gmail.com>
parents: 793
diff changeset
12 -- Will be modified by configure script if run --
455
2eeae9c314b0 main.lua -> prosody
Matthew Wild <mwild1@gmail.com>
parents: 452
diff changeset
13
7297
b34a42a10c9f prosody, prosodyctl: Allow setting CFG_* variables via Lua interpreter before loading Prosody. Fixes #308.
Matthew Wild <mwild1@gmail.com>
parents: 7081
diff changeset
14 CFG_SOURCEDIR=CFG_SOURCEDIR or os.getenv("PROSODY_SRCDIR");
b34a42a10c9f prosody, prosodyctl: Allow setting CFG_* variables via Lua interpreter before loading Prosody. Fixes #308.
Matthew Wild <mwild1@gmail.com>
parents: 7081
diff changeset
15 CFG_CONFIGDIR=CFG_CONFIGDIR or os.getenv("PROSODY_CFGDIR");
b34a42a10c9f prosody, prosodyctl: Allow setting CFG_* variables via Lua interpreter before loading Prosody. Fixes #308.
Matthew Wild <mwild1@gmail.com>
parents: 7081
diff changeset
16 CFG_PLUGINDIR=CFG_PLUGINDIR or os.getenv("PROSODY_PLUGINDIR");
b34a42a10c9f prosody, prosodyctl: Allow setting CFG_* variables via Lua interpreter before loading Prosody. Fixes #308.
Matthew Wild <mwild1@gmail.com>
parents: 7081
diff changeset
17 CFG_DATADIR=CFG_DATADIR or os.getenv("PROSODY_DATADIR");
455
2eeae9c314b0 main.lua -> prosody
Matthew Wild <mwild1@gmail.com>
parents: 452
diff changeset
18
2147
119323e35c32 Mainfile: Fixed some comments.
Waqas Hussain <waqas20@gmail.com>
parents: 2087
diff changeset
19 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
455
2eeae9c314b0 main.lua -> prosody
Matthew Wild <mwild1@gmail.com>
parents: 452
diff changeset
20
3999
58c0de7c6da0 prosody, prosodyctl: Filter out relative paths from package.(c)path when installed
Matthew Wild <mwild1@gmail.com>
parents: 3998
diff changeset
21 local function is_relative(path)
58c0de7c6da0 prosody, prosodyctl: Filter out relative paths from package.(c)path when installed
Matthew Wild <mwild1@gmail.com>
parents: 3998
diff changeset
22 local path_sep = package.config:sub(1,1);
8264
23aee8ccfe9b prosody: Tiny whitespace fix
Kim Alvefur <zash@zash.se>
parents: 8202
diff changeset
23 return ((path_sep == "/" and path:sub(1,1) ~= "/")
23aee8ccfe9b prosody: Tiny whitespace fix
Kim Alvefur <zash@zash.se>
parents: 8202
diff changeset
24 or (path_sep == "\\" and (path:sub(1,1) ~= "/" and path:sub(2,3) ~= ":\\")))
3999
58c0de7c6da0 prosody, prosodyctl: Filter out relative paths from package.(c)path when installed
Matthew Wild <mwild1@gmail.com>
parents: 3998
diff changeset
25 end
58c0de7c6da0 prosody, prosodyctl: Filter out relative paths from package.(c)path when installed
Matthew Wild <mwild1@gmail.com>
parents: 3998
diff changeset
26
2337
9eb20b3f3bbb prosody: Clarify and add some comments to describe what we're doing when and why
Matthew Wild <mwild1@gmail.com>
parents: 2330
diff changeset
27 -- Tell Lua where to find our libraries
455
2eeae9c314b0 main.lua -> prosody
Matthew Wild <mwild1@gmail.com>
parents: 452
diff changeset
28 if CFG_SOURCEDIR then
3999
58c0de7c6da0 prosody, prosodyctl: Filter out relative paths from package.(c)path when installed
Matthew Wild <mwild1@gmail.com>
parents: 3998
diff changeset
29 local function filter_relative_paths(path)
58c0de7c6da0 prosody, prosodyctl: Filter out relative paths from package.(c)path when installed
Matthew Wild <mwild1@gmail.com>
parents: 3998
diff changeset
30 if is_relative(path) then return ""; end
58c0de7c6da0 prosody, prosodyctl: Filter out relative paths from package.(c)path when installed
Matthew Wild <mwild1@gmail.com>
parents: 3998
diff changeset
31 end
58c0de7c6da0 prosody, prosodyctl: Filter out relative paths from package.(c)path when installed
Matthew Wild <mwild1@gmail.com>
parents: 3998
diff changeset
32 local function sanitise_paths(paths)
58c0de7c6da0 prosody, prosodyctl: Filter out relative paths from package.(c)path when installed
Matthew Wild <mwild1@gmail.com>
parents: 3998
diff changeset
33 return (paths:gsub("[^;]+;?", filter_relative_paths):gsub(";;+", ";"));
58c0de7c6da0 prosody, prosodyctl: Filter out relative paths from package.(c)path when installed
Matthew Wild <mwild1@gmail.com>
parents: 3998
diff changeset
34 end
58c0de7c6da0 prosody, prosodyctl: Filter out relative paths from package.(c)path when installed
Matthew Wild <mwild1@gmail.com>
parents: 3998
diff changeset
35 package.path = sanitise_paths(CFG_SOURCEDIR.."/?.lua;"..package.path);
58c0de7c6da0 prosody, prosodyctl: Filter out relative paths from package.(c)path when installed
Matthew Wild <mwild1@gmail.com>
parents: 3998
diff changeset
36 package.cpath = sanitise_paths(CFG_SOURCEDIR.."/?.so;"..package.cpath);
455
2eeae9c314b0 main.lua -> prosody
Matthew Wild <mwild1@gmail.com>
parents: 452
diff changeset
37 end
2eeae9c314b0 main.lua -> prosody
Matthew Wild <mwild1@gmail.com>
parents: 452
diff changeset
38
2337
9eb20b3f3bbb prosody: Clarify and add some comments to describe what we're doing when and why
Matthew Wild <mwild1@gmail.com>
parents: 2330
diff changeset
39 -- Substitute ~ with path to home directory in data path
502
21dc299387a6 Installation improvements (auto-creation of data directories)
Matthew Wild <mwild1@gmail.com>
parents: 500
diff changeset
40 if CFG_DATADIR then
467
66f145f5c932 Update Makefile to now pass config paths to prosody. Update prosody, modulemanager and connectionlisteners to obey these paths.
Matthew Wild <mwild1@gmail.com>
parents: 455
diff changeset
41 if os.getenv("HOME") then
502
21dc299387a6 Installation improvements (auto-creation of data directories)
Matthew Wild <mwild1@gmail.com>
parents: 500
diff changeset
42 CFG_DATADIR = CFG_DATADIR:gsub("^~", os.getenv("HOME"));
467
66f145f5c932 Update Makefile to now pass config paths to prosody. Update prosody, modulemanager and connectionlisteners to obey these paths.
Matthew Wild <mwild1@gmail.com>
parents: 455
diff changeset
43 end
502
21dc299387a6 Installation improvements (auto-creation of data directories)
Matthew Wild <mwild1@gmail.com>
parents: 500
diff changeset
44 end
467
66f145f5c932 Update Makefile to now pass config paths to prosody. Update prosody, modulemanager and connectionlisteners to obey these paths.
Matthew Wild <mwild1@gmail.com>
parents: 455
diff changeset
45
10597
25a3c8134b0a prosody/util.startup: Switch to parse_args() for --root and --config
Matthew Wild <mwild1@gmail.com>
parents: 8726
diff changeset
46
6985
a2e1f5ebdb53 prosody: Don't silently ignore unknown command-line options
Matthew Wild <mwild1@gmail.com>
parents: 6921
diff changeset
47
8635
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents: 8285
diff changeset
48 local startup = require "util.startup";
8682
151ecd18d624 prosody, util.startup: Switch from async.once() to long-lived thread, to avoid GC
Matthew Wild <mwild1@gmail.com>
parents: 8635
diff changeset
49 local async = require "util.async";
2587
c37f971f0fe6 prosody, prosodyctl: Re-jiggle load order again, fixes logging config not being obeyed (thanks darkrain)
Matthew Wild <mwild1@gmail.com>
parents: 2567
diff changeset
50
8682
151ecd18d624 prosody, util.startup: Switch from async.once() to long-lived thread, to avoid GC
Matthew Wild <mwild1@gmail.com>
parents: 8635
diff changeset
51 -- Note: it's important that this thread is not GC'd, as some C libraries
151ecd18d624 prosody, util.startup: Switch from async.once() to long-lived thread, to avoid GC
Matthew Wild <mwild1@gmail.com>
parents: 8635
diff changeset
52 -- that are initialized here store a pointer to it ( :/ ).
151ecd18d624 prosody, util.startup: Switch from async.once() to long-lived thread, to avoid GC
Matthew Wild <mwild1@gmail.com>
parents: 8635
diff changeset
53 local thread = async.runner();
151ecd18d624 prosody, util.startup: Switch from async.once() to long-lived thread, to avoid GC
Matthew Wild <mwild1@gmail.com>
parents: 8635
diff changeset
54
151ecd18d624 prosody, util.startup: Switch from async.once() to long-lived thread, to avoid GC
Matthew Wild <mwild1@gmail.com>
parents: 8635
diff changeset
55 thread:run(startup.prosody);
1530
0494f5e3be23 prosody: Call initialisation functions at once
Matthew Wild <mwild1@gmail.com>
parents: 1529
diff changeset
56
12296
49ebac8a5260 prosody: Expose main thread on the 'prosody' global
Kim Alvefur <zash@zash.se>
parents: 12295
diff changeset
57 prosody.main_thread = thread;
49ebac8a5260 prosody: Expose main thread on the 'prosody' global
Kim Alvefur <zash@zash.se>
parents: 12295
diff changeset
58
8635
47e3b8b6f17a prosody, prosodyctl, util.startup: Finally factor out startup-related and common code into a separate module
Matthew Wild <mwild1@gmail.com>
parents: 8285
diff changeset
59 local function loop()
1529
b5e4215f797d prosody: Start of refactoring of main file
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
60 -- Error handler for errors that make it this far
b5e4215f797d prosody: Start of refactoring of main file
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
61 local function catch_uncaught_error(err)
2769
826f6fb7036d prosody: Less strict matching for the magic 'interrupted' error
Matthew Wild <mwild1@gmail.com>
parents: 2321
diff changeset
62 if type(err) == "string" and err:match("interrupted!$") then
1529
b5e4215f797d prosody: Start of refactoring of main file
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
63 return "quitting";
b5e4215f797d prosody: Start of refactoring of main file
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
64 end
7876
c028555866b3 prosody: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7801
diff changeset
65
8714
e1c4bdb2cd25 prosody: Use prosody.log instead of _G.log because it looks nicer
Kim Alvefur <zash@zash.se>
parents: 8682
diff changeset
66 prosody.log("error", "Top-level error, please report:\n%s", tostring(err));
1529
b5e4215f797d prosody: Start of refactoring of main file
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
67 local traceback = debug.traceback("", 2);
b5e4215f797d prosody: Start of refactoring of main file
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
68 if traceback then
8714
e1c4bdb2cd25 prosody: Use prosody.log instead of _G.log because it looks nicer
Kim Alvefur <zash@zash.se>
parents: 8682
diff changeset
69 prosody.log("error", "%s", traceback);
1529
b5e4215f797d prosody: Start of refactoring of main file
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
70 end
7876
c028555866b3 prosody: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7801
diff changeset
71
1529
b5e4215f797d prosody: Start of refactoring of main file
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
72 prosody.events.fire_event("very-bad-error", {error = err, traceback = traceback});
992
3153eff6dae2 prosody: Add prosody_shutdown() function to initiate a server shutdown, add code to gracefully close connections before stopping
Matthew Wild <mwild1@gmail.com>
parents: 978
diff changeset
73 end
7876
c028555866b3 prosody: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7801
diff changeset
74
6921
45fa2e554c79 prosody: Fix sleep call that relied on the no longer existing socket global
Kim Alvefur <zash@zash.se>
parents: 6808
diff changeset
75 local sleep = require"socket".sleep;
8726
fbb6751dbe85 prosody: Get net.server via requre to avoid relying on globals
Kim Alvefur <zash@zash.se>
parents: 8714
diff changeset
76 local server = require "net.server";
6921
45fa2e554c79 prosody: Fix sleep call that relied on the no longer existing socket global
Kim Alvefur <zash@zash.se>
parents: 6808
diff changeset
77
1529
b5e4215f797d prosody: Start of refactoring of main file
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
78 while select(2, xpcall(server.loop, catch_uncaught_error)) ~= "quitting" do
6921
45fa2e554c79 prosody: Fix sleep call that relied on the no longer existing socket global
Kim Alvefur <zash@zash.se>
parents: 6808
diff changeset
79 sleep(0.2);
1529
b5e4215f797d prosody: Start of refactoring of main file
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
80 end
992
3153eff6dae2 prosody: Add prosody_shutdown() function to initiate a server shutdown, add code to gracefully close connections before stopping
Matthew Wild <mwild1@gmail.com>
parents: 978
diff changeset
81 end
1026
e640df2e4e9b prosody: Fire events during server shutdown process
Matthew Wild <mwild1@gmail.com>
parents: 1017
diff changeset
82
1530
0494f5e3be23 prosody: Call initialisation functions at once
Matthew Wild <mwild1@gmail.com>
parents: 1529
diff changeset
83 loop();
853
c0a40522041e prosody: Log top-level errors
Matthew Wild <mwild1@gmail.com>
parents: 843
diff changeset
84
12553
cc0ec0277813 util.startup: Fix async waiting for last shutdown steps
Kim Alvefur <zash@zash.se>
parents: 12296
diff changeset
85 startup.exit();