# HG changeset patch
# User Kim Alvefur <zash@zash.se>
# Date 1730567425 -3600
# Node ID 01d2abc3d17c5c1f7223781688700e701b78dd66
# Parent  c228f1ffb7b3a59ed33a73fe60140d80cd6b47f2
mod_smacks: Destroy timed out session in async context (fixes #1884)

Prevents ASYNC-01 due to storage interactions in a timer.

Also considered modifying mod_c2s to allow passing arbitrary closures
into its runner thread but this seems like a big step away from the
current code for just this module.

Also considered creating a dedicated runner in mod_smacks, but ensuring
continuity across module reloads might be tricky.

We could further improve this in the next major version.

diff -r c228f1ffb7b3 -r 01d2abc3d17c plugins/mod_smacks.lua
--- a/plugins/mod_smacks.lua	Sun Sep 29 18:09:17 2024 -0700
+++ b/plugins/mod_smacks.lua	Sat Nov 02 18:10:25 2024 +0100
@@ -494,14 +494,16 @@
 			return
 		end
 
-		session.log("debug", "Destroying session for hibernating too long");
-		session_registry[jid.join(session.username, session.host, session.resumption_token)] = nil;
-		old_session_registry:set(session.username, session.resumption_token,
-			{ h = session.handled_stanza_count; t = os.time() });
-		session.resumption_token = nil;
-		session.resending_unacked = true; -- stop outgoing_stanza_filter from re-queueing anything anymore
-		sessionmanager.destroy_session(session, "Hibernating too long");
-		sessions_expired(1);
+		prosody.main_thread:run(function ()
+			session.log("debug", "Destroying session for hibernating too long");
+			session_registry[jid.join(session.username, session.host, session.resumption_token)] = nil;
+			old_session_registry:set(session.username, session.resumption_token,
+				{ h = session.handled_stanza_count; t = os.time() });
+			session.resumption_token = nil;
+			session.resending_unacked = true; -- stop outgoing_stanza_filter from re-queueing anything anymore
+			sessionmanager.destroy_session(session, "Hibernating too long");
+			sessions_expired(1);
+		end);
 	end);
 	if session.conn then
 		local conn = session.conn;