chore: Guard from calling markNeedsUpdate in update (#12525) fab85a4fd5 Increasingly, users (or the agent) have been known to call markNeedsUpdate from within update in scripts. This results not only in the update cycle recursing until the max number is hit, but more seriously, memory usage can grow very quickly when this is the case, ultimately leading to the system running out of memory. This PR: Adds a guard to prevent markNeedsUpdate from adding dirt when called during the update cycle Adds some instructions for the agent and comments in the documentation to make it clear to avoid this scenario. Co-authored-by: Philip Chung <philterdesign@gmail.com>
diff --git a/.rive_head b/.rive_head index ed50efb..667c984 100644 --- a/.rive_head +++ b/.rive_head
@@ -1 +1 @@ -9faec1e36e1ffe1d92b374ca0a31a669d0625f34 +fab85a4fd560539838503c7cac18dd82e6756770
diff --git a/include/rive/scripted/scripted_object.hpp b/include/rive/scripted/scripted_object.hpp index 196574f..b7c31bb 100644 --- a/include/rive/scripted/scripted_object.hpp +++ b/include/rive/scripted/scripted_object.hpp
@@ -38,9 +38,13 @@ ScriptingVM* m_vm = nullptr; // Non-owning for runtime #endif #endif + bool inUpdatePhase() const { return m_inUpdatePhase; } + void setInUpdatePhase(bool value) { m_inUpdatePhase = value; } + private: rcp<DataContext> m_dataContext = nullptr; std::vector<ScriptedProperty*> m_trackedScriptedProperties; + bool m_inUpdatePhase = false; #ifdef WITH_RIVE_SCRIPTING bool m_userLuaInitDone = false; bool tryLuaUserInit(lua_State* L);
diff --git a/src/scripted/scripted_drawable.cpp b/src/scripted/scripted_drawable.cpp index f5f8191..99ce32b 100644 --- a/src/scripted/scripted_drawable.cpp +++ b/src/scripted/scripted_drawable.cpp
@@ -325,6 +325,10 @@ void ScriptedDrawable::markNeedsUpdate() { + if (inUpdatePhase()) + { + return; + } addScriptedDirt(ComponentDirt::ScriptUpdate); }
diff --git a/src/scripted/scripted_object.cpp b/src/scripted/scripted_object.cpp index b70cd87..2664d59 100644 --- a/src/scripted/scripted_object.cpp +++ b/src/scripted/scripted_object.cpp
@@ -222,6 +222,7 @@ { return; } + m_inUpdatePhase = true; // Stack: [] rive_lua_pushRef(L, m_self); // Stack: [self] @@ -234,6 +235,7 @@ { rive_lua_pop(L, 1); } + m_inUpdatePhase = false; } bool ScriptedObject::tryLuaUserInit(lua_State* L)
diff --git a/src/scripted/scripted_path_effect.cpp b/src/scripted/scripted_path_effect.cpp index 8f880b4..e3390b9 100644 --- a/src/scripted/scripted_path_effect.cpp +++ b/src/scripted/scripted_path_effect.cpp
@@ -45,6 +45,7 @@ { return; } + setInUpdatePhase(true); // Stack: [] rive_lua_pushRef(L, m_self); // Stack: [self] @@ -74,6 +75,7 @@ } // Stack: [self, status] or [self, outputPathData] rive_lua_pop(L, 2); + setInUpdatePhase(false); } } #else @@ -168,6 +170,10 @@ void ScriptedPathEffect::markNeedsUpdate() { + if (inUpdatePhase()) + { + return; + } addScriptedDirt(ComponentDirt::ScriptUpdate); }
diff --git a/tests/unit_tests/runtime/scripting/scripting_update_phase_guard_test.cpp b/tests/unit_tests/runtime/scripting/scripting_update_phase_guard_test.cpp new file mode 100644 index 0000000..f092c6d --- /dev/null +++ b/tests/unit_tests/runtime/scripting/scripting_update_phase_guard_test.cpp
@@ -0,0 +1,100 @@ +/* + * Copyright 2026 Rive + */ + +// Tests for the per-object update phase guard. When a ScriptedObject is inside +// its scriptUpdate() callback (m_inUpdatePhase == true), calls to +// markNeedsUpdate() are silently ignored. + +#include "catch.hpp" +#include "scripting_test_utilities.hpp" +#include "rive/lua/rive_lua_libs.hpp" +#include "rive/scripted/scripted_drawable.hpp" + +using namespace rive; + +// Extends ScriptedDrawable to inherit its real markNeedsUpdate() but stubs +// addScriptedDirt so the test doesn't need a full artboard graph. +class TestScriptedDrawable : public ScriptedDrawable +{ + int m_dirtCount = 0; + +public: + bool addScriptedDirt(ComponentDirt value, bool recurse = false) override + { + m_dirtCount++; + return true; + } + + int dirtCount() const { return m_dirtCount; } + void resetDirtCount() { m_dirtCount = 0; } + bool isInUpdatePhase() const { return inUpdatePhase(); } +}; + +TEST_CASE("markNeedsUpdate is ignored during scriptUpdate", "[scripting]") +{ + ScriptingTest vm( + R"( +type MyObj = { + _ctx: Context?, +} + +function update(self: MyObj) + if self._ctx then + self._ctx:markNeedsUpdate() + end +end + +return function(): Node<MyObj> + return { + _ctx = nil, + update = update, + } +end +)"); + + TestScriptedDrawable obj; + lua_State* L = vm.state(); + + CHECK(obj.ensureScriptInitialized(vm.vm())); + + // Attach the context to the Lua self table. + rive_lua_pushRef(L, obj.self()); + auto* ctxPtr = lua_newrive<ScriptedContext>(L, &obj); + lua_setfield(L, -2, "_ctx"); + rive_lua_pop(L, 1); + + // Mark the object as having an update method. + obj.implementedMethods(obj.implementedMethods() | (1 << 1)); + CHECK(obj.updates()); + + // scriptUpdate sets the phase flag, so markNeedsUpdate is suppressed. + obj.resetDirtCount(); + CHECK(obj.isInUpdatePhase() == false); + obj.scriptUpdate(); + CHECK(obj.isInUpdatePhase() == false); + CHECK(obj.dirtCount() == 0); + + // Outside the update phase, markNeedsUpdate adds dirt normally. + obj.resetDirtCount(); + obj.markNeedsUpdate(); + CHECK(obj.dirtCount() == 1); + + ctxPtr->clearScriptedObject(); +} + +TEST_CASE("inUpdatePhase defaults to false", "[scripting]") +{ + TestScriptedDrawable obj; + CHECK(obj.isInUpdatePhase() == false); +} + +TEST_CASE("markNeedsUpdate works outside update phase", "[scripting]") +{ + TestScriptedDrawable obj; + CHECK(obj.dirtCount() == 0); + obj.markNeedsUpdate(); + CHECK(obj.dirtCount() == 1); + obj.markNeedsUpdate(); + CHECK(obj.dirtCount() == 2); +}