Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 78 additions & 25 deletions reacton/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand Down Expand Up @@ -1278,6 +1297,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
Expand Down Expand Up @@ -1425,18 +1478,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.")
Expand All @@ -1446,7 +1488,7 @@ def collect(context: ComponentContext):
# 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:
Expand Down Expand Up @@ -1546,16 +1588,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? "
Expand All @@ -1564,9 +1615,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? "
Expand All @@ -1576,16 +1627,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)
Expand Down Expand Up @@ -2517,6 +2568,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:
Expand Down Expand Up @@ -3198,6 +3250,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]
Expand Down
120 changes: 119 additions & 1 deletion reacton/core_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -3267,6 +3268,123 @@ def test_no_reference_cycles_after_close(component):
gc.enable()


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()
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


@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, 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 _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, 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

@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 _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_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]] = []
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
Expand Down
Loading