Diff

plugins/mod_c2s.lua @ 12301:4f1fe6eb1ddb

mod_c2s,mod_s2s: Wait for sessions to close before proceeding with shutdown steps Ensures unavailable presence and other outgoing stanzas are sent. Waiting for c2s sessions to close first before proceeding to disable and close s2s ensures that unavailable presence can go out, even if it requires dialback to complete first.
author Kim Alvefur <zash@zash.se>
date Thu, 17 Feb 2022 03:49:47 +0100
parent 12300:fb74ff16620c
child 12302:6a8c680b8677
line wrap: on
line diff
--- a/plugins/mod_c2s.lua	Fri Feb 18 14:25:22 2022 +0100
+++ b/plugins/mod_c2s.lua	Thu Feb 17 03:49:47 2022 +0100
@@ -16,7 +16,8 @@
 local st = require "util.stanza";
 local sm_new_session, sm_destroy_session = sessionmanager.new_session, sessionmanager.destroy_session;
 local uuid_generate = require "util.uuid".generate;
-local runner = require "util.async".runner;
+local async = require "util.async";
+local runner = async.runner;
 
 local tostring, type = tostring, type;
 
@@ -382,6 +383,7 @@
 		session.conn = nil;
 		sessions[conn]  = nil;
 	end
+	module:fire_event("c2s-closed", { session = session; conn = conn });
 end
 
 function listener.onreadtimeout(conn)
@@ -431,11 +433,24 @@
 end, -80);
 
 module:hook("server-stopping", function(event)
+	local wait, done = async.waiter();
+	module:hook("c2s-closed", function ()
+		if next(sessions) == nil then done(); end
+	end)
+
 	-- Close sessions
 	local reason = event.reason;
 	for _, session in pairs(sessions) do
 		session:close{ condition = "system-shutdown", text = reason };
 	end
+
+	-- Wait for them to close properly if they haven't already
+	if next(sessions) ~= nil then
+		add_task(stream_close_timeout+1, done);
+		module:log("info", "Waiting for sessions to close");
+		wait();
+	end
+
 end, -100);