changeset 14134:041c7ff18f76 13.0

mod_cloud_notify: Fix leaking iq response handlers by using send_iq()
author Matthew Wild <mwild1@gmail.com>
date Thu, 16 Apr 2026 18:55:53 +0100
parents 4087c5b600b1
children 836a70deb3c9
files plugins/mod_cloud_notify.lua
diffstat 1 files changed, 7 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/plugins/mod_cloud_notify.lua	Thu Apr 16 18:54:56 2026 +0100
+++ b/plugins/mod_cloud_notify.lua	Thu Apr 16 18:55:53 2026 +0100
@@ -74,8 +74,7 @@
 -- Forward declarations, as both functions need to reference each other
 local handle_push_success, handle_push_error;
 
-function handle_push_error(event)
-	local stanza = event.stanza;
+function handle_push_error(stanza)
 	local error_type, condition, error_text = stanza:get_error();
 	local node = id2node[stanza.attr.id];
 	local identifier = id2identifier[stanza.attr.id];
@@ -117,11 +116,6 @@
 					changed = true;
 					user_push_services[push_identifier] = nil
 					push_errors[push_identifier] = nil;
-					-- unhook iq handlers for this identifier (if possible)
-					module:unhook("iq-error/host/"..stanza.attr.id, handle_push_error);
-					module:unhook("iq-result/host/"..stanza.attr.id, handle_push_success);
-					id2node[stanza.attr.id] = nil;
-					id2identifier[stanza.attr.id] = nil;
 				end
 			elseif user_push_services[push_identifier] and user_push_services[push_identifier].jid == from and error_type == "wait" then
 				module:log("debug", "Got error <%s:%s:%s> for identifier '%s': "
@@ -141,8 +135,7 @@
 	return true;
 end
 
-function handle_push_success(event)
-	local stanza = event.stanza;
+function handle_push_success(stanza)
 	local node = id2node[stanza.attr.id];
 	local identifier = id2identifier[stanza.attr.id];
 	if node == nil then return false; end		-- unknown stanza? Ignore for now!
@@ -153,11 +146,6 @@
 		if push_identifier == identifier then
 			if user_push_services[push_identifier] and user_push_services[push_identifier].jid == from and push_errors[push_identifier] > 0 then
 				push_errors[push_identifier] = 0;
-				-- unhook iq handlers for this identifier (if possible)
-				module:unhook("iq-error/host/"..stanza.attr.id, handle_push_error);
-				module:unhook("iq-result/host/"..stanza.attr.id, handle_push_success);
-				id2node[stanza.attr.id] = nil;
-				id2identifier[stanza.attr.id] = nil;
 				module:log("debug", "Push succeeded, error count for identifier '%s' is now at %s again",
 					push_identifier, tostring(push_errors[push_identifier])
 				);
@@ -257,14 +245,6 @@
 			end
 			user_push_services[key] = nil;
 			push_errors[key] = nil;
-			for stanza_id, identifier in pairs(id2identifier) do
-				if identifier == key then
-					module:unhook("iq-error/host/"..stanza_id, handle_push_error);
-					module:unhook("iq-result/host/"..stanza_id, handle_push_success);
-					id2node[stanza_id] = nil;
-					id2identifier[stanza_id] = nil;
-				end
-			end
 		end
 	end
 	local ok = push_store:flush_to_disk(origin.username);
@@ -413,11 +393,13 @@
 				if push_errors[push_identifier] == nil then
 					push_errors[push_identifier] = 0;
 				end
-				module:hook("iq-error/host/"..stanza_id, handle_push_error);
-				module:hook("iq-result/host/"..stanza_id, handle_push_success);
 				id2node[stanza_id] = node;
 				id2identifier[stanza_id] = push_identifier;
-				module:send(push_publish);
+				module:send_iq(push_publish):next(handle_push_success, handle_push_error):finally(function ()
+					-- unhook iq handlers for this identifier (if possible)
+					id2node[stanza.attr.id] = nil;
+					id2identifier[stanza.attr.id] = nil;
+				end);
 				pushes = pushes + 1;
 			end
 		end