diff mod_firewall/mod_firewall.lua @ 6534:5aeefc2251d4

mod_firewall: Load a script completely or not at all Previously it was possible that some chains would load, but others would not. This can be surprising (that a "half-loaded" state is possible) and hard to debug. This change makes it so that if some chains don't compile, the whole script will be treated as not loaded.
author Matthew Wild <mwild1@gmail.com>
date Wed, 06 May 2026 16:15:38 +0100
parents 0f61d13ebbde
children 82b6a03b9dfa
line wrap: on
line diff
--- a/mod_firewall/mod_firewall.lua	Wed May 06 15:41:56 2026 +0100
+++ b/mod_firewall/mod_firewall.lua	Wed May 06 16:15:38 2026 +0100
@@ -688,6 +688,8 @@
 		return;
 	end
 
+	local errs;
+
 	-- Loop through the chains in the script, and for each chain attach the compiled code to the
 	-- relevant events, keeping track in events_hooked so we can cleanly unload later
 	local events_hooked = {};
@@ -695,6 +697,8 @@
 		local new_handler, err = compile_handler(handler_code, "mod_firewall::"..chain);
 		if not new_handler then
 			module:log("error", "Compilation error for %s: %s", script, err);
+			errs = true;
+			break;
 		else
 			local chain_definition = chains[chain];
 			if chain_definition and chain_definition.type == "event" then
@@ -711,6 +715,13 @@
 			module:hook(event_name, handler);
 		end
 	end
+	if errs then
+		for event_name, event_handler in pairs(events_hooked) do
+			module:unhook(event_name, event_handler);
+			events_hooked[event_name] = nil;
+		end
+		return;
+	end
 	loaded_scripts[script] = { last_modified = last_modified, events_hooked = events_hooked };
 	module:log("debug", "Loaded %s", script);
 	return true;