Annotate

prosody @ 11571:a8f0f87e115a

prosody: Close the state on exit (ie garbage-collect everything) This ensures __gc is called on everything that may need it, such as database connections. It was reported in the chat by Happy that SQLite3 does not close its state cleanly in WAL mode, leaving the WAL file behind. This is probably rather a bug in mod_storage_sql, but forcing a final GC sweep should also help with such things everywhere.
author Kim Alvefur <zash@zash.se>
date Tue, 18 May 2021 20:08:37 +0200
parent 10599:4f655918fef1
child 12294:81f147ddc4ab
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
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
57 local function loop()
1529
b5e4215f797d prosody: Start of refactoring of main file
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
58 -- 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
59 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
60 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
61 return "quitting";
b5e4215f797d prosody: Start of refactoring of main file
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
62 end
7876
c028555866b3 prosody: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7801
diff changeset
63
8714
e1c4bdb2cd25 prosody: Use prosody.log instead of _G.log because it looks nicer
Kim Alvefur <zash@zash.se>
parents: 8682
diff changeset
64 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
65 local traceback = debug.traceback("", 2);
b5e4215f797d prosody: Start of refactoring of main file
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
66 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
67 prosody.log("error", "%s", traceback);
1529
b5e4215f797d prosody: Start of refactoring of main file
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
68 end
7876
c028555866b3 prosody: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7801
diff changeset
69
1529
b5e4215f797d prosody: Start of refactoring of main file
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
70 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
71 end
7876
c028555866b3 prosody: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7801
diff changeset
72
6921
45fa2e554c79 prosody: Fix sleep call that relied on the no longer existing socket global
Kim Alvefur <zash@zash.se>
parents: 6808
diff changeset
73 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
74 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
75
1529
b5e4215f797d prosody: Start of refactoring of main file
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
76 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
77 sleep(0.2);
1529
b5e4215f797d prosody: Start of refactoring of main file
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
78 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
79 end
1026
e640df2e4e9b prosody: Fire events during server shutdown process
Matthew Wild <mwild1@gmail.com>
parents: 1017
diff changeset
80
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
81 local function cleanup()
8714
e1c4bdb2cd25 prosody: Use prosody.log instead of _G.log because it looks nicer
Kim Alvefur <zash@zash.se>
parents: 8682
diff changeset
82 prosody.log("info", "Shutdown status: Cleaning up");
1529
b5e4215f797d prosody: Start of refactoring of main file
Matthew Wild <mwild1@gmail.com>
parents: 1523
diff changeset
83 prosody.events.fire_event("server-cleanup");
1091
5ca2d3a33269 prosody: Define prosody_shutdown() before emitting the server-starting event
Matthew Wild <mwild1@gmail.com>
parents: 1086
diff changeset
84 end
5ca2d3a33269 prosody: Define prosody_shutdown() before emitting the server-starting event
Matthew Wild <mwild1@gmail.com>
parents: 1086
diff changeset
85
1530
0494f5e3be23 prosody: Call initialisation functions at once
Matthew Wild <mwild1@gmail.com>
parents: 1529
diff changeset
86 loop();
853
c0a40522041e prosody: Log top-level errors
Matthew Wild <mwild1@gmail.com>
parents: 843
diff changeset
87
8714
e1c4bdb2cd25 prosody: Use prosody.log instead of _G.log because it looks nicer
Kim Alvefur <zash@zash.se>
parents: 8682
diff changeset
88 prosody.log("info", "Shutting down...");
1530
0494f5e3be23 prosody: Call initialisation functions at once
Matthew Wild <mwild1@gmail.com>
parents: 1529
diff changeset
89 cleanup();
1365
864a99eff6d7 Main: Don't use empty event data objects for some global events. Some handlers don't expect it.
Waqas Hussain <waqas20@gmail.com>
parents: 1364
diff changeset
90 prosody.events.fire_event("server-stopped");
8714
e1c4bdb2cd25 prosody: Use prosody.log instead of _G.log because it looks nicer
Kim Alvefur <zash@zash.se>
parents: 8682
diff changeset
91 prosody.log("info", "Shutdown complete");
1530
0494f5e3be23 prosody: Call initialisation functions at once
Matthew Wild <mwild1@gmail.com>
parents: 1529
diff changeset
92
10041
c8f16fd2a5e3 prosody: Log shutdown reason (in past tense) as the very last thing
Kim Alvefur <zash@zash.se>
parents: 10040
diff changeset
93 prosody.log("debug", "Shutdown reason was: %s", prosody.shutdown_reason or "not specified");
10042
4e63e28ba362 prosody: Also log status code passed to exit()
Kim Alvefur <zash@zash.se>
parents: 10041
diff changeset
94 prosody.log("debug", "Exiting with status code: %d", prosody.shutdown_code or 0);
11571
a8f0f87e115a prosody: Close the state on exit (ie garbage-collect everything)
Kim Alvefur <zash@zash.se>
parents: 10599
diff changeset
95 os.exit(prosody.shutdown_code, true);