A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
fix: check python id before deleting UI element (#1439)
Akshay Agrawal6ea69d703cda
diff --git a/marimo/_plugins/ui/_core/registry.py b/marimo/_plugins/ui/_core/registry.pyindex 3053c1ec7b..841c46b0cc 100644--- a/marimo/_plugins/ui/_core/registry.py+++ b/marimo/_plugins/ui/_core/registry.py@@ -151,8 +151,10 @@ class UIElementRegistry:returnui_element = self._objects[object_id]()- del self._objects[object_id]-+ # We guard against UIElement's destructor racing against+ # registration of another element when a cell re-runs by checking+ # the Python object id. This isn't perfect because python ids can+ # be reused ...registered_python_id = (id(ui_element) if ui_element is not None else None)@@ -160,8 +162,6 @@ class UIElementRegistry:registered_python_id is not Noneand registered_python_id != python_id):- # guards against UIElement's destructor racing against- # registration of another element when a cell re-runsreturntry:@@ -175,3 +175,4 @@ class UIElementRegistry:del self._bindings[object_id]if object_id in self._constructing_cells:del self._constructing_cells[object_id]+ del self._objects[object_id]diff --git a/tests/_plugins/ui/_core/test_registry.py b/tests/_plugins/ui/_core/test_registry.pyindex 864baf20d1..d58c6bf74e 100644--- a/tests/_plugins/ui/_core/test_registry.py+++ b/tests/_plugins/ui/_core/test_registry.py@@ -168,3 +168,25 @@ async def test_parent_bound_to_view(array = k.globals["array"]registry = get_context().ui_element_registryassert registry.bound_names(array._id) == set(["array", "child"])+++async def test_dont_delete_element_with_wrong_python_id(+ k: Kernel, exec_req: ExecReqProvider+) -> None:+ await k.run(+ [+ exec_req.get("import marimo as mo"),+ exec_req.get(+ """+ s = mo.ui.slider(1, 10)+ """+ ),+ ]+ )+ # Make sure that the slider is registered+ s = k.globals["s"]+ assert get_context().ui_element_registry.get_object(s._id) == s++ # If the Python id doesn't match, don't delete the object.+ get_context().ui_element_registry.delete(s._id, -1)+ assert get_context().ui_element_registry.get_object(s._id) == s