From 330e5487992c6ddb68f3b3a20bb9516d78130409 Mon Sep 17 00:00:00 2001 From: Maarten Breddels Date: Sun, 8 Nov 2026 13:03:15 +0100 Subject: [PATCH 1/2] fix: free removed and closed component contexts without the gc A load test of a solara server found that about a third of all cyclic garbage is reacton's: ComponentContext, Element, Effect and use_state setters. #52 empties every component context in close(), but two cycles were left: - A component that goes away while the render context stays open was never emptied. Its setters in its hook state reference its context, so each unmount in a live page made a cycle for the gc. - close() found the contexts with a recursive local function. That function references itself through its closure cell, a cycle that kept every hollow context and its empty containers alive until a gc. The #52 test only checked that the render context is freed, so it missed this. _remove_element now empties the context it removes (both renderers), and close() finds the contexts with a loop. A setter of a removed component does nothing, like React: it reads the state containers once, so a teardown from another thread cannot make it fail halfway. Co-Authored-By: Claude Opus 5.5 --- reacton/core.py | 82 ++++++++++++++++++++++---------- reacton/core_test.py | 108 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 165 insertions(+), 25 deletions(-) diff --git a/reacton/core.py b/reacton/core.py index 2246753..0a7be8b 100644 --- a/reacton/core.py +++ b/reacton/core.py @@ -1278,6 +1278,40 @@ def _teardown_component_context(context: ComponentContext): context.fast_stale_effects_cleaned = set() +def _collect_component_contexts(root: ComponentContext) -> Dict[int, ComponentContext]: + """Return root and every context below it, in children and children_next, by id. + + By id: after a failed render, a context can be in both children and children_next, + and a visit per path to it is 2**depth visits. A loop, not a recursive local + function: that function would reference itself through its closure cell, a cycle + that keeps everything it found alive until a gc. + """ + found: Dict[int, ComponentContext] = {} + stack = [root] + while stack: + context = stack.pop() + if id(context) in found: + continue + found[id(context)] = context + stack.extend(context.children.values()) + stack.extend(context.children_next.values()) + return found + + +def _teardown_removed_component_context(parent: ComponentContext, key: str, removed: ComponentContext): + """Empty a component context that _remove_element took out of a render context that stays open. + + Without this, every component that goes away during a render is a reference cycle + (its use_state setters in its hook state reference it), that only a gc can free. + Leftover contexts in its children_next (after a failed render) go with it. + """ + if parent.children_next.get(key) is removed: + # the new tree still uses this context (defensive: it should not happen) + return + for context in _collect_component_contexts(removed).values(): + _teardown_component_context(context) + + @dataclass class RerenderReason: reason: str @@ -1425,18 +1459,7 @@ def close(self): # snapshot the component contexts before _remove_element detaches them from # their parents: detached contexts would escape the teardown below while the # setter/handler closures in their state still reference them and us - # by id: after a failed render, a context can be in both children and children_next, and - # a visit per path to it is 2**depth visits - all_contexts: Dict[int, ComponentContext] = {} - - def collect(context: ComponentContext): - if id(context) in all_contexts: - return - all_contexts[id(context)] = context - for child in list(context.children.values()) + list(context.children_next.values()): - collect(child) - - collect(self.context_root) + all_contexts = _collect_component_contexts(self.context_root) logger.info("Removing elements...") self._remove_element(self.element, default_key="/", parent_key=ROOT_KEY) logger.info("Removing elements done.") @@ -1546,16 +1569,25 @@ def set_(value): # torn down: there is nothing to update. This check must come first: # after close, context.state is empty and reading it would fail. return + # read the containers once: when this component goes away while the render + # context stays open, _teardown_removed_component_context replaces them + # (maybe from another thread, while we run); the old ones stay intact + state = context.state + state_metadata = context.state_metadata + if key not in state: + # the component was removed, there is nothing to update (like React, + # setting the state of an unmounted component does nothing) + return if callable(value): - value = value(context.state[key]) - logger.info("Set state = %r for key %r (previous value was %r) (%r)", value, key, context.state[key], id(self.context)) + value = value(state[key]) + logger.info("Set state = %r for key %r (previous value was %r) (%r)", value, key, state[key], id(self.context)) should_update = False new_metadata = None if eq is None: - if context.state[key] is value and isinstance(value, (list, dict, set)): + if state[key] is value and isinstance(value, (list, dict, set)): new_metadata = len(value) - if context.state_metadata[key] != new_metadata: + if state_metadata[key] != new_metadata: warn( "You are setting the state with the same object, this will usually not trigger a rerender. " f"The length of {value} changed compared to the previous time it was set. Are you mutating an existing state object? " @@ -1564,9 +1596,9 @@ def set_(value): stacklevel=2, ) should_update = True - if context.state[key] is value and utils.isinstance_lazy(value, "pandas.DataFrame"): + if state[key] is value and utils.isinstance_lazy(value, "pandas.DataFrame"): new_metadata = utils.dataframe_fingerprint(value) - if context.state_metadata[key] != new_metadata: + if state_metadata[key] != new_metadata: warn( "You are setting the state with the dataframe, this will usually not trigger a rerender. " "We noticed the ids of the dataframe series are changed. Are you mutating an dataframe? " @@ -1576,16 +1608,16 @@ def set_(value): ) should_update = True equals = eq or utils.equals - should_update = not equals(context.state[key], value) or should_update + should_update = not equals(state[key], value) or should_update if should_update: - prev_value = context.state[key] - context.state[key] = value - if context.state[key] is value and isinstance(value, (list, dict, set)) and new_metadata is None: + prev_value = state[key] + state[key] = value + if state[key] is value and isinstance(value, (list, dict, set)) and new_metadata is None: new_metadata = len(value) - if context.state[key] is value and utils.isinstance_lazy(value, "pandas.DataFrame") and new_metadata is None: + if state[key] is value and utils.isinstance_lazy(value, "pandas.DataFrame") and new_metadata is None: new_metadata = utils.dataframe_fingerprint(value) - context.state_metadata[key] = new_metadata + state_metadata[key] = new_metadata # TODO: enable context.needs_render = True _mark_needs_render_ancestors(context) @@ -2517,6 +2549,7 @@ def _remove_element(self, el: Element, default_key: str, parent_key): context.exceptions_children.extend(child_context.exceptions_self) context.exceptions_children.extend(child_context.exceptions_children) del context.children[key] + _teardown_removed_component_context(context, key, child_context) else: self._visit_children(el, key, parent_key, self._remove_element) if el.is_shared: @@ -3198,6 +3231,7 @@ def _remove_element(self, el: Element, default_key: str, parent_key): context.exceptions_children.extend(child_context.exceptions_self) context.exceptions_children.extend(child_context.exceptions_children) del context.children[key] + _teardown_removed_component_context(context, key, child_context) else: self._visit_children(el, key, parent_key, self._remove_element) widget = self._shared_widgets[el] if el.is_shared else context.widgets[key] diff --git a/reacton/core_test.py b/reacton/core_test.py index 2a98167..c8ad498 100644 --- a/reacton/core_test.py +++ b/reacton/core_test.py @@ -6,7 +6,7 @@ import unittest.mock import weakref from concurrent.futures import ThreadPoolExecutor -from typing import Callable, Generic, List, Optional, Tuple, TypeVar, cast +from typing import Any, Callable, Generic, List, Optional, Tuple, TypeVar, cast import ipyvuetify import ipywidgets @@ -3267,6 +3267,112 @@ def test_no_reference_cycles_after_close(component): gc.enable() +def _reacton_cyclic_garbage() -> List[str]: + """Collect, and describe the reacton objects that only a gc could free. + + Widgets have cycles of their own (a Button keeps a bound method of itself in its + message callbacks), so this only looks at what reacton owns: component contexts, + elements, effects, render contexts, and closures defined in reacton. + """ + gc.set_debug(gc.DEBUG_SAVEALL) + try: + gc.collect() + return sorted( + getattr(obj, "__qualname__", type(obj).__name__) + for obj in gc.garbage + if isinstance(obj, (core.ComponentContext, core.Element, core.Effect, core._RenderContext)) + or (isinstance(obj, type(_reacton_cyclic_garbage)) and obj.__module__.startswith("reacton")) + ) + finally: + gc.set_debug(0) + gc.garbage.clear() + + +@pytest.mark.parametrize("component", [w.Button, ButtonComponentFunction, UseMemoComponent, UseStateComponent]) +def test_no_cyclic_garbage_after_close(component): + # Stricter than test_no_reference_cycles_after_close: that test only checks the + # render context itself. The hollow component contexts that close() leaves must + # be freed by refcounting too, otherwise every closed kernel still feeds the gc. + gc.collect() + gc.disable() + try: + box, rc = react.render(component(), handle_error=False) + rc.close() + del rc, box + assert _reacton_cyclic_garbage() == [] + finally: + gc.enable() + + +def test_no_cyclic_garbage_after_unmount(): + # A component that goes away during a render (the render context stays open) + # must be freed by refcounting: its setter closures in its hook state reference + # its component context, so a removed context that is not emptied is a cycle. + # On a server every unmount in a live page would feed the gc. + set_show: Callable[[bool], None] = lambda x: None # noqa + + @react.component + def Child(i): + value, set_value = react.use_state(0) + react.use_effect(lambda: None, []) + return w.Button(description=f"{i} {value}", on_click=lambda: set_value(value + 1)) + + @react.component + def Parent(): + nonlocal set_show + show, set_show = react.use_state(True) + with w.VBox() as main: + if show: + for i in range(3): + Child(i).key(f"child-{i}") + return main + + gc.collect() + gc.disable() + box, rc = react.render(Parent(), handle_error=False) + try: + set_show(False) + assert rc.find(widgets.Button).widgets == [] + assert _reacton_cyclic_garbage() == [] + # the parent still works after its children went away + set_show(True) + assert len(rc.find(widgets.Button).widgets) == 3 + finally: + rc.close() + gc.enable() + + +def test_set_state_after_unmount_does_nothing(): + # a task or a thread can still hold the setter of a component that went away + child_setters: List[Callable[[Any], None]] = [] + set_show: Callable[[bool], None] = lambda x: None # noqa + renders = 0 + + @react.component + def Child(): + value, set_value = react.use_state(0) + child_setters.append(set_value) + return w.Button(description=str(value)) + + @react.component + def Parent(): + nonlocal set_show, renders + renders += 1 + show, set_show = react.use_state(True) + return Child() if show else w.Label(value="gone") + + box, rc = react.render(Parent(), handle_error=False) + try: + set_show(False) + renders_before = renders + child_setters[-1](42) + child_setters[-1](lambda value: value + 1) + assert renders == renders_before + rc.find(widgets.Label, value="gone").assert_single() + finally: + rc.close() + + def test_state_changes_do_not_keep_old_values_alive(): # every state change used to append a RerenderReason with the previous and next # value to the render context, so a long-lived page kept every old state value From 5ae49ad98724d13bd999df76f00819c0cba57665 Mon Sep 17 00:00:00 2001 From: Maarten Breddels Date: Sun, 8 Nov 2026 13:10:18 +0100 Subject: [PATCH 2/2] fix: free closed widgets without the gc After the component contexts, the rest of the cyclic garbage of an unmount or a close was the closed widgets themselves: ipywidgets' Button and ipyvue's Events (so every ipyvue/ipyvuetify widget) register a bound method of the widget with on_msg, and ipyvue's VueWidget keeps a ClassList that points back to the widget. Each closed widget was about 20 objects for the gc, and in solara almost every widget is an ipyvue widget. close_widget now clears the message callbacks (a closed widget has no comm, so no message can arrive) and points class_list at a weak proxy, so class_list keeps working for whoever still holds the widget. close() closes its container through close_widget too. The gc tests now check all cyclic garbage, not only reacton objects, and cover an ipyvuetify component with use_event. Co-Authored-By: Claude Opus 5.5 --- reacton/core.py | 21 +++++++++++++++++- reacton/core_test.py | 52 +++++++++++++++++++++++++++----------------- 2 files changed, 52 insertions(+), 21 deletions(-) diff --git a/reacton/core.py b/reacton/core.py index 0a7be8b..75804de 100644 --- a/reacton/core.py +++ b/reacton/core.py @@ -182,10 +182,29 @@ def close_widget(widget: widgets.Widget): # this happens for v.Chip, which has a close trait if callable(widget.close): widget.close() + _break_widget_self_references(widget) else: logger.warning("Widget %r does not have a close method, possibly a close trait was added", widget) +def _break_widget_self_references(widget: widgets.Widget): + """Let a closed widget be freed by refcounting, not only by a gc. + + Some widgets reference themselves, which makes each closed widget a cycle: + ipywidgets' Button and ipyvue's Events (every ipyvue/ipyvuetify widget) register a + bound method of the widget with on_msg, and ipyvue's VueWidget keeps a ClassList + whose obj is the widget. + """ + # a closed widget has no comm, so no message can arrive for these callbacks + callbacks = getattr(getattr(widget, "_msg_callbacks", None), "callbacks", None) + if isinstance(callbacks, list): + callbacks.clear() + class_list = widget.__dict__.get("class_list") + if class_list is not None and getattr(class_list, "obj", None) is widget: + # keep class_list working for whoever still holds the widget + class_list.obj = weakref.proxy(widget) + + def _is_shared_ipyvue_template(widget: widgets.Widget) -> bool: """Is this widget an ipyvue Template that is shared between VueTemplate instances? @@ -1469,7 +1488,7 @@ def close(self): # find self.context None mid-render (AssertionError in # Element.__exit__, seen in production behind a prompt kernel close) if self.container: - self.container.close() + close_widget(self.container) if isinstance(self.container, widgets.DOMWidget) and self.container.layout is not None: self.container.layout.close() if self._shared_elements: diff --git a/reacton/core_test.py b/reacton/core_test.py index c8ad498..25c27b6 100644 --- a/reacton/core_test.py +++ b/reacton/core_test.py @@ -19,6 +19,7 @@ import reacton as react import reacton.core +import reacton.ipyvue from reacton.core import component, use_effect from . import bqplot # noqa: F401 @@ -3267,47 +3268,51 @@ def test_no_reference_cycles_after_close(component): gc.enable() -def _reacton_cyclic_garbage() -> List[str]: - """Collect, and describe the reacton objects that only a gc could free. - - Widgets have cycles of their own (a Button keeps a bound method of itself in its - message callbacks), so this only looks at what reacton owns: component contexts, - elements, effects, render contexts, and closures defined in reacton. - """ +def _cyclic_garbage() -> List[str]: + """Collect, and return the type names of everything that only a gc could free.""" gc.set_debug(gc.DEBUG_SAVEALL) try: gc.collect() - return sorted( - getattr(obj, "__qualname__", type(obj).__name__) - for obj in gc.garbage - if isinstance(obj, (core.ComponentContext, core.Element, core.Effect, core._RenderContext)) - or (isinstance(obj, type(_reacton_cyclic_garbage)) and obj.__module__.startswith("reacton")) - ) + names = sorted(type(obj).__name__ for obj in gc.garbage) finally: gc.set_debug(0) gc.garbage.clear() + # free the cycles we found, also when gc is disabled, so the next check does not see them + gc.collect() + return names -@pytest.mark.parametrize("component", [w.Button, ButtonComponentFunction, UseMemoComponent, UseStateComponent]) +@reacton.component +def VuetifyButtonComponent(): + value, set_value = reacton.use_state(0) + with v.Html(tag="div") as main: + btn = v.Btn(children=[str(value)]) + reacton.ipyvue.use_event(btn, "click", lambda *_: set_value(value + 1)) + return main + + +@pytest.mark.parametrize("component", [w.Button, ButtonComponentFunction, UseMemoComponent, UseStateComponent, VuetifyButtonComponent]) def test_no_cyclic_garbage_after_close(component): # Stricter than test_no_reference_cycles_after_close: that test only checks the - # render context itself. The hollow component contexts that close() leaves must - # be freed by refcounting too, otherwise every closed kernel still feeds the gc. + # render context itself. The hollow component contexts that close() leaves, and the + # closed widgets, must be freed by refcounting too, otherwise every closed kernel + # still feeds the gc. gc.collect() gc.disable() try: box, rc = react.render(component(), handle_error=False) rc.close() del rc, box - assert _reacton_cyclic_garbage() == [] + assert _cyclic_garbage() == [] finally: gc.enable() def test_no_cyclic_garbage_after_unmount(): # A component that goes away during a render (the render context stays open) - # must be freed by refcounting: its setter closures in its hook state reference - # its component context, so a removed context that is not emptied is a cycle. + # must be freed by refcounting, with its widgets: its setter closures in its hook + # state reference its component context, so a removed context that is not emptied + # is a cycle, and a Button keeps a bound method of itself. # On a server every unmount in a live page would feed the gc. set_show: Callable[[bool], None] = lambda x: None # noqa @@ -3333,7 +3338,7 @@ def Parent(): try: set_show(False) assert rc.find(widgets.Button).widgets == [] - assert _reacton_cyclic_garbage() == [] + assert _cyclic_garbage() == [] # the parent still works after its children went away set_show(True) assert len(rc.find(widgets.Button).widgets) == 3 @@ -3342,6 +3347,13 @@ def Parent(): gc.enable() +def test_closed_widget_class_list_still_works(): + btn = ipyvuetify.Btn(children=["x"]) + reacton.core.close_widget(btn) + btn.class_list.add("d-none") + assert btn.class_ == "d-none" + + def test_set_state_after_unmount_does_nothing(): # a task or a thread can still hold the setter of a component that went away child_setters: List[Callable[[Any], None]] = []