FIX: Prevent infinite loop of automations triggering each other (#26814)

It's currently possible to setup multiple automation rules that trigger each other resulting in an infinite loop. To prevent that, this commit adds a global "circuit breaker" that prevents all automations from triggering while an automation rule is executing.

Internal topic: t/124365.
This commit is contained in:
Osama Sayegh
2024-04-30 20:13:29 +03:00
committed by GitHub
parent ff8e1f4ed6
commit 0e44072b2b
4 changed files with 72 additions and 15 deletions

View File

@ -134,11 +134,23 @@ module DiscourseAutomation
def trigger!(context = {})
if enabled
if scriptable.background && !running_in_background
trigger_in_background!(context)
else
triggerable&.on_call&.call(self, serialized_fields)
scriptable.script.call(context, serialized_fields, self)
if active_id = DiscourseAutomation.get_active_automation
Rails.logger.warn(<<~TEXT.strip)
[automation] potential automations infinite loop detected: skipping automation #{self.id} because automation #{active_id} is still executing.")
TEXT
return
end
begin
DiscourseAutomation.set_active_automation(self.id)
if scriptable.background && !running_in_background
trigger_in_background!(context)
else
triggerable&.on_call&.call(self, serialized_fields)
scriptable.script.call(context, serialized_fields, self)
end
ensure
DiscourseAutomation.set_active_automation(nil)
end
end
end