Software / code / prosody
Annotate
plugins/mod_cron.lua @ 13652:a08065207ef0
net.server_epoll: Call :shutdown() on TLS sockets when supported
Comment from Matthew:
This fixes a potential issue where the Prosody process gets blocked on sockets
waiting for them to close. Unlike non-TLS sockets, closing a TLS socket sends
layer 7 data, and this can cause problems for sockets which are in the process
of being cleaned up.
This depends on LuaSec changes which are not yet upstream.
From Martijn's original email:
So first my analysis of luasec. in ssl.c the socket is put into blocking
mode right before calling SSL_shutdown() inside meth_destroy(). My best
guess to why this is is because meth_destroy is linked to the __close
and __gc methods, which can't exactly be called multiple times and
luasec does want to make sure that a tls session is shutdown as clean
as possible.
I can't say I disagree with this reasoning and don't want to change this
behaviour. My solution to this without changing the current behaviour is
to introduce a shutdown() method. I am aware that this overlaps in a
conflicting way with tcp's shutdown method, but it stays close to the
OpenSSL name. This method calls SSL_shutdown() in the current
(non)blocking mode of the underlying socket and returns a boolean
whether or not the shutdown is completed (matching SSL_shutdown()'s 0
or 1 return values), and returns the familiar ssl_ioerror() strings on
error with a false for completion. This error can then be used to
determine if we have wantread/wantwrite to finalize things. Once
meth_shutdown() has been called once a shutdown flag will be set, which
indicates to meth_destroy() that the SSL_shutdown() has been handled
by the application and it shouldn't be needed to set the socket to
blocking mode. I've left the SSL_shutdown() call in the
LSEC_STATE_CONNECTED to prevent TOCTOU if the application reaches a
timeout for the shutdown code, which might allow SSL_shutdown() to
clean up anyway at the last possible moment.
Another thing I've changed to luasec is the call to socket_setblocking()
right before calling close(2) in socket_destroy() in usocket.c.
According to the latest POSIX[0]:
Note that the requirement for close() on a socket to block for up to
the current linger interval is not conditional on the O_NONBLOCK
setting.
Which I read to mean that removing O_NONBLOCK on the socket before close
doesn't impact the behaviour and only causes noise in system call
tracers. I didn't touch the windows bits of this, since I don't do
windows.
For the prosody side of things I've made the TLS shutdown bits resemble
interface:onwritable(), and put it under a combined guard of self._tls
and self.conn.shutdown. The self._tls bit is there to prevent getting
stuck on this condition, and self.conn.shutdown is there to prevent the
code being called by instances where the patched luasec isn't deployed.
The destroy() method can be called from various places and is read by
me as the "we give up" error path. To accommodate for these unexpected
entrypoints I've added a single call to self.conn:shutdown() to prevent
the socket being put into blocking mode. I have no expectations that
there is any other use here. Same as previous, the self.conn.shutdown
check is there to make sure it's not called on unpatched luasec
deployments and self._tls is there to make sure we don't call shutdown()
on tcp sockets.
I wouldn't recommend logging of the conn:shutdown() error inside
close(), since a lot of clients simply close the connection before
SSL_shutdown() is done.
| author | Martijn van Duren <martijn@openbsd.org> |
|---|---|
| date | Thu, 06 Feb 2025 15:04:38 +0000 |
| parent | 13588:899453f11f50 |
| child | 13701:1aa7efabeacb |
| rev | line source |
|---|---|
|
11986
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 module:set_global(); |
|
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 |
|
12977
74b9e05af71e
plugins: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents:
12489
diff
changeset
|
3 local async = require("prosody.util.async"); |
|
11986
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 |
|
13421
92301fa7a673
mod_cron: Allow configuring various "internal" delay parameters
Matthew Wild <mwild1@gmail.com>
parents:
13371
diff
changeset
|
5 local cron_initial_delay = module:get_option_number("cron_initial_delay", 1); |
|
92301fa7a673
mod_cron: Allow configuring various "internal" delay parameters
Matthew Wild <mwild1@gmail.com>
parents:
13371
diff
changeset
|
6 local cron_check_delay = module:get_option_number("cron_check_delay", 3600); |
|
92301fa7a673
mod_cron: Allow configuring various "internal" delay parameters
Matthew Wild <mwild1@gmail.com>
parents:
13371
diff
changeset
|
7 local cron_spread_factor = module:get_option_number("cron_spread_factor", 0); |
|
92301fa7a673
mod_cron: Allow configuring various "internal" delay parameters
Matthew Wild <mwild1@gmail.com>
parents:
13371
diff
changeset
|
8 |
|
11986
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 local active_hosts = {} |
|
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 |
|
13588
899453f11f50
mod_cron: Don't run tasks if loaded inside prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
13443
diff
changeset
|
11 if prosody.process_type == "prosodyctl" then |
|
899453f11f50
mod_cron: Don't run tasks if loaded inside prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
13443
diff
changeset
|
12 return; -- Yes, it happens... |
|
899453f11f50
mod_cron: Don't run tasks if loaded inside prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
13443
diff
changeset
|
13 end |
|
899453f11f50
mod_cron: Don't run tasks if loaded inside prosodyctl
Matthew Wild <mwild1@gmail.com>
parents:
13443
diff
changeset
|
14 |
|
11986
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 function module.add_host(host_module) |
|
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 |
|
13371
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
17 local last_run_times = host_module:open_store("cron", "map"); |
|
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
18 active_hosts[host_module.host] = true; |
|
11986
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 |
|
13371
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
20 local function save_task(task, started_at) last_run_times:set(nil, task.id, started_at); end |
|
13265
6ac5ad578565
mod_cron: Load last task run time inside task runner to fix async
Kim Alvefur <zash@zash.se>
parents:
13264
diff
changeset
|
21 |
|
13371
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
22 local function restore_task(task) if task.last == nil then task.last = last_run_times:get(nil, task.id); end end |
|
11986
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 |
|
13371
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
24 local function task_added(event) |
|
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
25 local task = event.item; |
|
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
26 if task.name == nil then task.name = task.when; end |
|
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
27 if task.id == nil then task.id = event.source.name .. "/" .. task.name:gsub("%W", "_"):lower(); end |
|
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
28 task.period = host_module:get_option_period(task.id:gsub("/", "_") .. "_period", "1" .. task.when, 60, 86400 * 7 * 53); |
|
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
29 task.restore = restore_task; |
|
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
30 task.save = save_task; |
|
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
31 module:log("debug", "%s task %s added", task.when, task.id); |
|
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
32 return true |
|
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
33 end |
|
11986
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 |
|
13371
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
35 local function task_removed(event) |
|
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
36 local task = event.item; |
|
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
37 host_module:log("debug", "Task %s removed", task.id); |
|
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
38 return true |
|
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
39 end |
|
11986
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 |
|
13371
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
41 host_module:handle_items("task", task_added, task_removed, true); |
|
11986
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 |
|
13371
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
43 function host_module.unload() active_hosts[host_module.host] = nil; end |
|
11986
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
44 end |
|
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
45 |
|
13371
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
46 local function should_run(task, last) return not last or last + task.period * 0.995 <= os.time() end |
|
11986
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
47 |
|
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
48 local function run_task(task) |
|
13371
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
49 task:restore(); |
|
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
50 if not should_run(task, task.last) then return end |
|
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
51 local started_at = os.time(); |
|
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
52 task:run(started_at); |
|
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
53 task.last = started_at; |
|
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
54 task:save(started_at); |
|
11986
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
55 end |
|
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
56 |
|
13421
92301fa7a673
mod_cron: Allow configuring various "internal" delay parameters
Matthew Wild <mwild1@gmail.com>
parents:
13371
diff
changeset
|
57 local function spread(t, factor) |
|
92301fa7a673
mod_cron: Allow configuring various "internal" delay parameters
Matthew Wild <mwild1@gmail.com>
parents:
13371
diff
changeset
|
58 return t * (1 - factor + 2*factor*math.random()); |
|
92301fa7a673
mod_cron: Allow configuring various "internal" delay parameters
Matthew Wild <mwild1@gmail.com>
parents:
13371
diff
changeset
|
59 end |
|
92301fa7a673
mod_cron: Allow configuring various "internal" delay parameters
Matthew Wild <mwild1@gmail.com>
parents:
13371
diff
changeset
|
60 |
|
11986
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
61 local task_runner = async.runner(run_task); |
|
13421
92301fa7a673
mod_cron: Allow configuring various "internal" delay parameters
Matthew Wild <mwild1@gmail.com>
parents:
13371
diff
changeset
|
62 scheduled = module:add_timer(cron_initial_delay, function() |
|
13371
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
63 module:log("info", "Running periodic tasks"); |
|
13421
92301fa7a673
mod_cron: Allow configuring various "internal" delay parameters
Matthew Wild <mwild1@gmail.com>
parents:
13371
diff
changeset
|
64 local delay = spread(cron_check_delay, cron_spread_factor); |
|
13371
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
65 for host in pairs(active_hosts) do |
|
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
66 module:log("debug", "Running periodic tasks for host %s", host); |
|
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
67 for _, task in ipairs(module:context(host):get_host_items("task")) do task_runner:run(task); end |
|
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
68 end |
|
13443
98a6ec4ce140
mod_cron: Fix log format to account for float that was integer before
Kim Alvefur <zash@zash.se>
parents:
13421
diff
changeset
|
69 module:log("debug", "Wait %gs", delay); |
|
13371
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
70 return delay |
|
11986
3d5135e8a2a7
mod_cron: Initial commit of periodic task runner
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
71 end); |
|
13364
6f9b15757384
mod_cron: Add shell command to list registered cron tasks with status
Matthew Wild <mwild1@gmail.com>
parents:
13284
diff
changeset
|
72 |
|
6f9b15757384
mod_cron: Add shell command to list registered cron tasks with status
Matthew Wild <mwild1@gmail.com>
parents:
13284
diff
changeset
|
73 module:add_item("shell-command", { |
|
13371
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
74 section = "cron"; |
|
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
75 section_desc = "View and manage recurring tasks"; |
|
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
76 name = "tasks"; |
|
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
77 desc = "View registered tasks"; |
|
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
78 args = {}; |
|
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
79 handler = function(self, filter_host) |
|
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
80 local format_table = require("prosody.util.human.io").table; |
|
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
81 local it = require("util.iterators"); |
|
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
82 local row = format_table({ |
|
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
83 { title = "Host"; width = "2p" }; |
|
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
84 { title = "Task"; width = "3p" }; |
|
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
85 { title = "Desc"; width = "3p" }; |
|
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
86 { title = "When"; width = "1p" }; |
|
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
87 { title = "Last run"; width = "20" }; |
|
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
88 }, self.session.width); |
|
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
89 local print = self.session.print; |
|
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
90 print(row()); |
|
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
91 for host in it.sorted_pairs(filter_host and { [filter_host] = true } or active_hosts) do |
|
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
92 for _, task in ipairs(module:context(host):get_host_items("task")) do |
|
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
93 print(row({ host; task.id; task.name; task.when; task.last and os.date("%Y-%m-%d %R:%S", task.last) or "never" })); |
|
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
94 end |
|
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
95 end |
|
a22e3a980178
mod_cron: Rebuild with new LuaFormatter settings (tabs!)
Kim Alvefur <zash@zash.se>
parents:
13366
diff
changeset
|
96 end; |
|
13364
6f9b15757384
mod_cron: Add shell command to list registered cron tasks with status
Matthew Wild <mwild1@gmail.com>
parents:
13284
diff
changeset
|
97 }); |