annotate util/helpers.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
parents d10957394a3c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1964
101a8df23b29 util.helpers: Add copyright header
Matthew Wild <mwild1@gmail.com>
parents: 1959
diff changeset
1 -- Prosody IM
2923
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 1964
diff changeset
2 -- Copyright (C) 2008-2010 Matthew Wild
b7049746bd29 Update copyright headers for 2010
Matthew Wild <mwild1@gmail.com>
parents: 1964
diff changeset
3 -- Copyright (C) 2008-2010 Waqas Hussain
5776
bd0ff8ae98a8 Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents: 5415
diff changeset
4 --
1964
101a8df23b29 util.helpers: Add copyright header
Matthew Wild <mwild1@gmail.com>
parents: 1959
diff changeset
5 -- This project is MIT/X11 licensed. Please see the
101a8df23b29 util.helpers: Add copyright header
Matthew Wild <mwild1@gmail.com>
parents: 1959
diff changeset
6 -- COPYING file in the source package for more information.
101a8df23b29 util.helpers: Add copyright header
Matthew Wild <mwild1@gmail.com>
parents: 1959
diff changeset
7 --
1531
21051377f11b util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8
12975
d10957394a3c util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 11059
diff changeset
9 local debug = require "prosody.util.debug";
4681
3299223bbed5 util.helpers: Add show_events(), to show the events and handlers in a util.events object
Matthew Wild <mwild1@gmail.com>
parents: 4536
diff changeset
10
1531
21051377f11b util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 -- Helper functions for debugging
21051377f11b util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12
12975
d10957394a3c util: Prefix module imports with prosody namespace
Kim Alvefur <zash@zash.se>
parents: 11059
diff changeset
13 local log = require "prosody.util.logger".init("util.debug");
1531
21051377f11b util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14
6777
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
15 local function log_events(events, name, logger)
1531
21051377f11b util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 local f = events.fire_event;
21051377f11b util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 if not f then
21051377f11b util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 error("Object does not appear to be a util.events object");
21051377f11b util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 end
21051377f11b util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 logger = logger or log;
21051377f11b util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 name = name or tostring(events);
21051377f11b util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 function events.fire_event(event, ...)
21051377f11b util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 logger("debug", "%s firing event: %s", name, event);
1795
0e933d6f2c31 util.helpers: It would be a good idea to fire an event when we say we are
Matthew Wild <mwild1@gmail.com>
parents: 1531
diff changeset
24 return f(event, ...);
1531
21051377f11b util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 end
11059
ad89e3cc67b6 util.helpers: when logging events, log individual handler calls
Matthew Wild <mwild1@gmail.com>
parents: 8411
diff changeset
26
ad89e3cc67b6 util.helpers: when logging events, log individual handler calls
Matthew Wild <mwild1@gmail.com>
parents: 8411
diff changeset
27 local function event_handler_hook(handler, event_name, event_data)
ad89e3cc67b6 util.helpers: when logging events, log individual handler calls
Matthew Wild <mwild1@gmail.com>
parents: 8411
diff changeset
28 logger("debug", "calling handler for %s: %s", event_name, handler);
ad89e3cc67b6 util.helpers: when logging events, log individual handler calls
Matthew Wild <mwild1@gmail.com>
parents: 8411
diff changeset
29 local ok, ret = pcall(handler, event_data);
ad89e3cc67b6 util.helpers: when logging events, log individual handler calls
Matthew Wild <mwild1@gmail.com>
parents: 8411
diff changeset
30 if not ok then
ad89e3cc67b6 util.helpers: when logging events, log individual handler calls
Matthew Wild <mwild1@gmail.com>
parents: 8411
diff changeset
31 logger("error", "error in event handler %s: %s", handler, ret);
ad89e3cc67b6 util.helpers: when logging events, log individual handler calls
Matthew Wild <mwild1@gmail.com>
parents: 8411
diff changeset
32 error(ret);
ad89e3cc67b6 util.helpers: when logging events, log individual handler calls
Matthew Wild <mwild1@gmail.com>
parents: 8411
diff changeset
33 end
ad89e3cc67b6 util.helpers: when logging events, log individual handler calls
Matthew Wild <mwild1@gmail.com>
parents: 8411
diff changeset
34 if ret ~= nil then
ad89e3cc67b6 util.helpers: when logging events, log individual handler calls
Matthew Wild <mwild1@gmail.com>
parents: 8411
diff changeset
35 logger("debug", "event chain ended for %s by %s with result: %s", event_name, handler, ret);
ad89e3cc67b6 util.helpers: when logging events, log individual handler calls
Matthew Wild <mwild1@gmail.com>
parents: 8411
diff changeset
36 end
ad89e3cc67b6 util.helpers: when logging events, log individual handler calls
Matthew Wild <mwild1@gmail.com>
parents: 8411
diff changeset
37 return ret;
ad89e3cc67b6 util.helpers: when logging events, log individual handler calls
Matthew Wild <mwild1@gmail.com>
parents: 8411
diff changeset
38 end
ad89e3cc67b6 util.helpers: when logging events, log individual handler calls
Matthew Wild <mwild1@gmail.com>
parents: 8411
diff changeset
39 events.set_debug_hook(event_handler_hook);
1531
21051377f11b util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 events[events.fire_event] = f;
21051377f11b util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 return events;
21051377f11b util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 end
21051377f11b util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43
6777
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
44 local function revert_log_events(events)
4536
285450536ec0 util.helpers: After nearly 'fixing' this code, I conclude it instead only deserves a bigger smile
Matthew Wild <mwild1@gmail.com>
parents: 2923
diff changeset
45 events.fire_event, events[events.fire_event] = events[events.fire_event], nil; -- :))
11059
ad89e3cc67b6 util.helpers: when logging events, log individual handler calls
Matthew Wild <mwild1@gmail.com>
parents: 8411
diff changeset
46 events.set_debug_hook(nil);
1531
21051377f11b util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 end
21051377f11b util.helpers: New util library to aid with debugging, etc.
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48
6783
cd44427c7295 util.helpers: Fix order of functions using each other [fixes 00412b36166f]
Kim Alvefur <zash@zash.se>
parents: 6777
diff changeset
49 local function log_host_events(host)
cd44427c7295 util.helpers: Fix order of functions using each other [fixes 00412b36166f]
Kim Alvefur <zash@zash.se>
parents: 6777
diff changeset
50 return log_events(prosody.hosts[host].events, host);
cd44427c7295 util.helpers: Fix order of functions using each other [fixes 00412b36166f]
Kim Alvefur <zash@zash.se>
parents: 6777
diff changeset
51 end
cd44427c7295 util.helpers: Fix order of functions using each other [fixes 00412b36166f]
Kim Alvefur <zash@zash.se>
parents: 6777
diff changeset
52
cd44427c7295 util.helpers: Fix order of functions using each other [fixes 00412b36166f]
Kim Alvefur <zash@zash.se>
parents: 6777
diff changeset
53 local function revert_log_host_events(host)
cd44427c7295 util.helpers: Fix order of functions using each other [fixes 00412b36166f]
Kim Alvefur <zash@zash.se>
parents: 6777
diff changeset
54 return revert_log_events(prosody.hosts[host].events);
cd44427c7295 util.helpers: Fix order of functions using each other [fixes 00412b36166f]
Kim Alvefur <zash@zash.se>
parents: 6777
diff changeset
55 end
cd44427c7295 util.helpers: Fix order of functions using each other [fixes 00412b36166f]
Kim Alvefur <zash@zash.se>
parents: 6777
diff changeset
56
6777
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
57 local function show_events(events, specific_event)
4681
3299223bbed5 util.helpers: Add show_events(), to show the events and handlers in a util.events object
Matthew Wild <mwild1@gmail.com>
parents: 4536
diff changeset
58 local event_handlers = events._handlers;
3299223bbed5 util.helpers: Add show_events(), to show the events and handlers in a util.events object
Matthew Wild <mwild1@gmail.com>
parents: 4536
diff changeset
59 local events_array = {};
3299223bbed5 util.helpers: Add show_events(), to show the events and handlers in a util.events object
Matthew Wild <mwild1@gmail.com>
parents: 4536
diff changeset
60 local event_handler_arrays = {};
7720
7166750fb963 util.helpers: List event priorities instead of useless array index
Kim Alvefur <zash@zash.se>
parents: 6783
diff changeset
61 for event, priorities in pairs(events._event_map) do
4681
3299223bbed5 util.helpers: Add show_events(), to show the events and handlers in a util.events object
Matthew Wild <mwild1@gmail.com>
parents: 4536
diff changeset
62 local handlers = event_handlers[event];
4705
447f5a94792d util.helpers: show_events(): Make more robust, and allow filtering results to a specific event
Matthew Wild <mwild1@gmail.com>
parents: 4681
diff changeset
63 if handlers and (event == specific_event or not specific_event) then
447f5a94792d util.helpers: show_events(): Make more robust, and allow filtering results to a specific event
Matthew Wild <mwild1@gmail.com>
parents: 4681
diff changeset
64 table.insert(events_array, event);
447f5a94792d util.helpers: show_events(): Make more robust, and allow filtering results to a specific event
Matthew Wild <mwild1@gmail.com>
parents: 4681
diff changeset
65 local handler_strings = {};
447f5a94792d util.helpers: show_events(): Make more robust, and allow filtering results to a specific event
Matthew Wild <mwild1@gmail.com>
parents: 4681
diff changeset
66 for i, handler in ipairs(handlers) do
447f5a94792d util.helpers: show_events(): Make more robust, and allow filtering results to a specific event
Matthew Wild <mwild1@gmail.com>
parents: 4681
diff changeset
67 local upvals = debug.string_from_var_table(debug.get_upvalues_table(handler));
8411
a9e8523a5e73 util.helpers: Handle missing priorities, happens due to wildcard magic in net.http.server (fixes #1044)
Kim Alvefur <zash@zash.se>
parents: 7720
diff changeset
68 handler_strings[i] = " "..(priorities[handler] or "?")..": "..tostring(handler)..(upvals and ("\n "..upvals) or "");
4705
447f5a94792d util.helpers: show_events(): Make more robust, and allow filtering results to a specific event
Matthew Wild <mwild1@gmail.com>
parents: 4681
diff changeset
69 end
447f5a94792d util.helpers: show_events(): Make more robust, and allow filtering results to a specific event
Matthew Wild <mwild1@gmail.com>
parents: 4681
diff changeset
70 event_handler_arrays[event] = handler_strings;
4681
3299223bbed5 util.helpers: Add show_events(), to show the events and handlers in a util.events object
Matthew Wild <mwild1@gmail.com>
parents: 4536
diff changeset
71 end
3299223bbed5 util.helpers: Add show_events(), to show the events and handlers in a util.events object
Matthew Wild <mwild1@gmail.com>
parents: 4536
diff changeset
72 end
3299223bbed5 util.helpers: Add show_events(), to show the events and handlers in a util.events object
Matthew Wild <mwild1@gmail.com>
parents: 4536
diff changeset
73 table.sort(events_array);
3299223bbed5 util.helpers: Add show_events(), to show the events and handlers in a util.events object
Matthew Wild <mwild1@gmail.com>
parents: 4536
diff changeset
74 local i = 1;
4705
447f5a94792d util.helpers: show_events(): Make more robust, and allow filtering results to a specific event
Matthew Wild <mwild1@gmail.com>
parents: 4681
diff changeset
75 while i <= #events_array do
4681
3299223bbed5 util.helpers: Add show_events(), to show the events and handlers in a util.events object
Matthew Wild <mwild1@gmail.com>
parents: 4536
diff changeset
76 local handlers = event_handler_arrays[events_array[i]];
3299223bbed5 util.helpers: Add show_events(), to show the events and handlers in a util.events object
Matthew Wild <mwild1@gmail.com>
parents: 4536
diff changeset
77 for j=#handlers, 1, -1 do
3299223bbed5 util.helpers: Add show_events(), to show the events and handlers in a util.events object
Matthew Wild <mwild1@gmail.com>
parents: 4536
diff changeset
78 table.insert(events_array, i+1, handlers[j]);
3299223bbed5 util.helpers: Add show_events(), to show the events and handlers in a util.events object
Matthew Wild <mwild1@gmail.com>
parents: 4536
diff changeset
79 end
3299223bbed5 util.helpers: Add show_events(), to show the events and handlers in a util.events object
Matthew Wild <mwild1@gmail.com>
parents: 4536
diff changeset
80 if i > 1 then events_array[i] = "\n"..events_array[i]; end
3299223bbed5 util.helpers: Add show_events(), to show the events and handlers in a util.events object
Matthew Wild <mwild1@gmail.com>
parents: 4536
diff changeset
81 i = i + #handlers + 1
4705
447f5a94792d util.helpers: show_events(): Make more robust, and allow filtering results to a specific event
Matthew Wild <mwild1@gmail.com>
parents: 4681
diff changeset
82 end
4681
3299223bbed5 util.helpers: Add show_events(), to show the events and handlers in a util.events object
Matthew Wild <mwild1@gmail.com>
parents: 4536
diff changeset
83 return table.concat(events_array, "\n");
3299223bbed5 util.helpers: Add show_events(), to show the events and handlers in a util.events object
Matthew Wild <mwild1@gmail.com>
parents: 4536
diff changeset
84 end
3299223bbed5 util.helpers: Add show_events(), to show the events and handlers in a util.events object
Matthew Wild <mwild1@gmail.com>
parents: 4536
diff changeset
85
6777
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
86 local function get_upvalue(f, get_name)
1959
f56670ce64de util.helpers: Add get_upvalue(function, name) helper
Matthew Wild <mwild1@gmail.com>
parents: 1795
diff changeset
87 local i, name, value = 0;
f56670ce64de util.helpers: Add get_upvalue(function, name) helper
Matthew Wild <mwild1@gmail.com>
parents: 1795
diff changeset
88 repeat
f56670ce64de util.helpers: Add get_upvalue(function, name) helper
Matthew Wild <mwild1@gmail.com>
parents: 1795
diff changeset
89 i = i + 1;
f56670ce64de util.helpers: Add get_upvalue(function, name) helper
Matthew Wild <mwild1@gmail.com>
parents: 1795
diff changeset
90 name, value = debug.getupvalue(f, i);
f56670ce64de util.helpers: Add get_upvalue(function, name) helper
Matthew Wild <mwild1@gmail.com>
parents: 1795
diff changeset
91 until name == get_name or name == nil;
f56670ce64de util.helpers: Add get_upvalue(function, name) helper
Matthew Wild <mwild1@gmail.com>
parents: 1795
diff changeset
92 return value;
f56670ce64de util.helpers: Add get_upvalue(function, name) helper
Matthew Wild <mwild1@gmail.com>
parents: 1795
diff changeset
93 end
f56670ce64de util.helpers: Add get_upvalue(function, name) helper
Matthew Wild <mwild1@gmail.com>
parents: 1795
diff changeset
94
6777
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
95 return {
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
96 log_host_events = log_host_events;
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
97 revert_log_host_events = revert_log_host_events;
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
98 log_events = log_events;
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
99 revert_log_events = revert_log_events;
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
100 show_events = show_events;
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
101 get_upvalue = get_upvalue;
5de6b93d0190 util.*: Remove use of module() function, make all module functions local and return them in a table at the end
Kim Alvefur <zash@zash.se>
parents: 5776
diff changeset
102 };