From 1ed07bc5b93533ec47ee6d2f25ba2f43f19c5853 Mon Sep 17 00:00:00 2001 From: Carson Date: Fri, 13 Nov 2026 16:32:47 -0600 Subject: [PATCH 1/4] fix(ipyvolume): raise a clear error for per-point string colors instead of hanging A per-point array of color strings (e.g. ipv.scatter(x, y, z, color=['tomato', 'seagreen'])) makes ipyvolume's color serializer infinite-loop the first time the widget's state is serialized, freezing the whole Shiny process with no error. Reject string-dtype (and str-valued object) arrays on Scatter.color, Scatter.color_selected, and Mesh.color during coercion with a TypeError that names the trait and points to the working alternatives (a single color, or numeric data with a colormap). Scalar string colors like color='red' are unaffected. --- shinywidgets/_as_widget.py | 40 +++++++++++++++++- tests/unit/test_ipyvolume.py | 82 ++++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+), 1 deletion(-) diff --git a/shinywidgets/_as_widget.py b/shinywidgets/_as_widget.py index 5ec7fa348..2e7f2b4eb 100644 --- a/shinywidgets/_as_widget.py +++ b/shinywidgets/_as_widget.py @@ -24,6 +24,7 @@ from bokeh.models import LayoutDOM from ipympl.backend_nbagg import Canvas from ipysigma import Sigma + from ipyvolume.widgets import Figure as IpyvolumeFigure from jupyter_bokeh import BokehModel from matplotlib.figure import Figure from panel.viewable import Viewable @@ -204,11 +205,12 @@ def as_widget_ipyvolume(x: object) -> Widget: # ipyvolume is guaranteed to be installed here since this function is only # called when x comes from the ipyvolume package from ipyvolume.ui import Container + from ipyvolume.widgets import Figure # quickscatter() and friends return the Container (figure plus control # widgets); the Figure is the renderable part if isinstance(x, Container): - return x.figure + x = x.figure if not isinstance(x, DOMWidget): raise TypeError( @@ -217,9 +219,45 @@ def as_widget_ipyvolume(x: object) -> Widget: "`ipv.gcf()` after drawing, or `ipv.figure()`)." ) + if isinstance(x, Figure): + _reject_ipyvolume_string_color_arrays(x) + return x +def _reject_ipyvolume_string_color_arrays(fig: IpyvolumeFigure) -> None: + import numpy as np + + # Scatter.color/color_selected and Mesh.color serialize through ipyvolume's + # color_to_binary_or_json(), which walks value[0][0]... until indexing + # raises; indexing into a string never raises ("tomato"[0] == "t", then + # "t"[0] == "t", ...), so a per-point string array infinite-loops and hangs + # the whole process the first time the widget's state is serialized + color_traits: list[Tuple[Widget, str]] = [ + *[(scatter, "color") for scatter in fig.scatters], + *[(scatter, "color_selected") for scatter in fig.scatters], + *[(mesh, "color") for mesh in fig.meshes], + ] + for mark, trait in color_traits: + value: object = getattr(mark, trait) + if not (isinstance(value, np.ndarray) and value.ndim > 0): + continue + is_string_array = value.dtype.kind in ("U", "S") or ( + value.dtype.kind == "O" + and value.size > 0 + and isinstance(value.flat[0], str) + ) + if is_string_array: + raise TypeError( + f"ipyvolume `{type(mark).__name__}.{trait}` was given an array " + f"of strings (dtype={value.dtype}, shape={value.shape}), which " + "ipyvolume's serializer infinite-loops on, hanging the entire " + "Shiny app. Pass a single color (e.g., color='red') or " + "per-point numeric data (e.g., values to map through a " + "colormap, or an (N, 3) RGB float array) instead." + ) + + def as_widget_lonboard(x: object) -> Widget: # lonboard is guaranteed to be installed here since this function is only # called when x comes from the lonboard package diff --git a/tests/unit/test_ipyvolume.py b/tests/unit/test_ipyvolume.py index 2064921cd..ecae956a9 100644 --- a/tests/unit/test_ipyvolume.py +++ b/tests/unit/test_ipyvolume.py @@ -63,6 +63,88 @@ def test_as_widget_errors_helpfully_for_a_bare_scene_object( as_widget(scatter) +def test_as_widget_rejects_per_point_string_colors(widget_session: Session) -> None: + ipv = pytest.importorskip("ipyvolume") + + with session_context(widget_session): + fig = ipv.figure() + scatter = ipv.scatter( + [0.0, 1.0], [0.0, 1.0], [0.0, 1.0], color=["tomato", "seagreen"] + ) + with pytest.raises(TypeError, match=r"Scatter\.color.*array of strings"): + as_widget(fig) + # A string array left in place would hang a later flush in ipyvolume's + # serializer + scatter.color = "red" + + +def test_as_widget_rejects_string_colors_from_the_quickscatter_container( + widget_session: Session, +) -> None: + ipv = pytest.importorskip("ipyvolume") + + with session_context(widget_session): + container = ipv.quickscatter( + [0.0, 1.0], [0.0, 1.0], [0.0, 1.0], color=["tomato", "seagreen"] + ) + with pytest.raises(TypeError, match=r"Scatter\.color.*array of strings"): + as_widget(container) + container.figure.scatters[0].color = "red" + + +def test_as_widget_rejects_string_color_selected(widget_session: Session) -> None: + ipv = pytest.importorskip("ipyvolume") + + with session_context(widget_session): + fig = ipv.figure() + scatter = ipv.scatter([0.0, 1.0], [0.0, 1.0], [0.0, 1.0]) + scatter.color_selected = ["tomato", "seagreen"] + with pytest.raises( + TypeError, match=r"Scatter\.color_selected.*array of strings" + ): + as_widget(fig) + scatter.color_selected = "green" + + +def test_as_widget_rejects_mesh_string_colors(widget_session: Session) -> None: + ipv = pytest.importorskip("ipyvolume") + import numpy as np + + with session_context(widget_session): + mesh = ipv.widgets.Mesh( + x=np.zeros((2, 2)), y=np.zeros((2, 2)), z=np.zeros((2, 2)), color="red" + ) + mesh.color = ["tomato", "seagreen"] + fig = ipv.Figure(meshes=[mesh]) + with pytest.raises(TypeError, match=r"Mesh\.color.*array of strings"): + as_widget(fig) + mesh.color = "red" + + +def test_as_widget_accepts_a_scalar_string_color(widget_session: Session) -> None: + ipv = pytest.importorskip("ipyvolume") + + with session_context(widget_session): + fig = ipv.figure() + ipv.scatter([0.0, 1.0], [0.0, 1.0], [0.0, 1.0], color="tomato") + assert as_widget(fig) is fig + + +def test_as_widget_accepts_numeric_per_point_colors(widget_session: Session) -> None: + ipv = pytest.importorskip("ipyvolume") + import numpy as np + + with session_context(widget_session): + fig = ipv.figure() + ipv.scatter( + [0.0, 1.0], + [0.0, 1.0], + [0.0, 1.0], + color=np.array([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]), + ) + assert as_widget(fig) is fig + + def test_vuetify_widget_open_serves_jupyter_vue( widget_session: Session, monkeypatch: pytest.MonkeyPatch ) -> None: From 168ad4b83ae61230c28c83a8cd659ee732f7df21 Mon Sep 17 00:00:00 2001 From: Carson Date: Fri, 13 Nov 2026 16:33:17 -0600 Subject: [PATCH 2/4] changelog: ipyvolume string-color guard (#493) --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f6755dac..96f8475de 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -90,6 +90,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Bug fixes +* An ipyvolume scatter or mesh given a per-point array of color strings (e.g., `ipv.scatter(x, y, z, color=['tomato', 'seagreen'])`) now raises a clear `TypeError` pointing to the working alternatives (a single color, or per-point numeric data with a colormap), instead of hanging the entire Shiny app with no error. (#493) * Widget-to-server messages larger than the websocket limit are now dropped client-side instead of disconnecting the whole session: the user sees a sticky error notification (once per widget), and the console error names the widget and the `--ws-max-size` workaround. Set `SHINYWIDGETS_WS_MAX_SIZE` if you run with a raised limit. (#459) * A widget that fails after its render function returns (e.g., an Altair/VegaFusion chart without `vl-convert-python` installed) now shows the user an error notification instead of leaving the output blank. (#320) * Fixed many issues with widgets nested inside other widgets and with widget outputs inserted dynamically (e.g., `output_widget()` inside `@render.ui`). With ipywidgets 8, these either didn't render at all (e.g., a Plotly `FigureWidget` or a styled `HTML` widget inside an ipyleaflet popup) or rendered while logging errors like `Class HTMLStyleModel not found` (e.g., ipyleaflet popups, `WidgetControl` children, and leafmap legends). (#75, #101, #137, #157, #194, #237) From 2040c5fdb0a5dbc0509851acfdba53d0a0b80755 Mon Sep 17 00:00:00 2001 From: Carson Sievert Date: Fri, 20 Nov 2026 17:57:34 -0600 Subject: [PATCH 3/4] Apply suggestion from @cpsievert --- CHANGELOG.md | 1 - 1 file changed, 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 96f8475de..2f6755dac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -90,7 +90,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Bug fixes -* An ipyvolume scatter or mesh given a per-point array of color strings (e.g., `ipv.scatter(x, y, z, color=['tomato', 'seagreen'])`) now raises a clear `TypeError` pointing to the working alternatives (a single color, or per-point numeric data with a colormap), instead of hanging the entire Shiny app with no error. (#493) * Widget-to-server messages larger than the websocket limit are now dropped client-side instead of disconnecting the whole session: the user sees a sticky error notification (once per widget), and the console error names the widget and the `--ws-max-size` workaround. Set `SHINYWIDGETS_WS_MAX_SIZE` if you run with a raised limit. (#459) * A widget that fails after its render function returns (e.g., an Altair/VegaFusion chart without `vl-convert-python` installed) now shows the user an error notification instead of leaving the output blank. (#320) * Fixed many issues with widgets nested inside other widgets and with widget outputs inserted dynamically (e.g., `output_widget()` inside `@render.ui`). With ipywidgets 8, these either didn't render at all (e.g., a Plotly `FigureWidget` or a styled `HTML` widget inside an ipyleaflet popup) or rendered while logging errors like `Class HTMLStyleModel not found` (e.g., ipyleaflet popups, `WidgetControl` children, and leafmap legends). (#75, #101, #137, #157, #194, #237) From 6b867aa0fe23234735d05b085e1ae363023f6517 Mon Sep 17 00:00:00 2001 From: Carson Date: Fri, 20 Nov 2026 18:12:15 -0600 Subject: [PATCH 4/4] fix(ipyvolume): reset rejected string colors so the error isn't followed by a hang The TypeError alone didn't prevent the hang: the rejected marks' comm opens were deferred at construction and still serialize state at the next reactive flush (nothing closes widgets created during a failed render), infinite-looping in ipyvolume's serializer. Reset the offending traits to None before raising, and cover it with an out-of-process flush-after-error regression test (in-process it would hang the suite on regression). --- CHANGELOG.md | 1 + shinywidgets/_as_widget.py | 33 +++++++++++----- tests/unit/test_ipyvolume.py | 75 +++++++++++++++++++++++++++++++++--- 3 files changed, 94 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f6755dac..9a616b124 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -90,6 +90,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Bug fixes +* An ipyvolume scatter or mesh given a per-point array of color strings (e.g., `ipv.scatter(x, y, z, color=['tomato', 'seagreen'])`) no longer hangs the entire Shiny app with no error: the invalid value is reset to `None` and a clear `TypeError` points to the working alternatives (a single color, or per-point numeric data with a colormap). (#493) * Widget-to-server messages larger than the websocket limit are now dropped client-side instead of disconnecting the whole session: the user sees a sticky error notification (once per widget), and the console error names the widget and the `--ws-max-size` workaround. Set `SHINYWIDGETS_WS_MAX_SIZE` if you run with a raised limit. (#459) * A widget that fails after its render function returns (e.g., an Altair/VegaFusion chart without `vl-convert-python` installed) now shows the user an error notification instead of leaving the output blank. (#320) * Fixed many issues with widgets nested inside other widgets and with widget outputs inserted dynamically (e.g., `output_widget()` inside `@render.ui`). With ipywidgets 8, these either didn't render at all (e.g., a Plotly `FigureWidget` or a styled `HTML` widget inside an ipyleaflet popup) or rendered while logging errors like `Class HTMLStyleModel not found` (e.g., ipyleaflet popups, `WidgetControl` children, and leafmap legends). (#75, #101, #137, #157, #194, #237) diff --git a/shinywidgets/_as_widget.py b/shinywidgets/_as_widget.py index 2e7f2b4eb..bc7ad0783 100644 --- a/shinywidgets/_as_widget.py +++ b/shinywidgets/_as_widget.py @@ -233,11 +233,13 @@ def _reject_ipyvolume_string_color_arrays(fig: IpyvolumeFigure) -> None: # raises; indexing into a string never raises ("tomato"[0] == "t", then # "t"[0] == "t", ...), so a per-point string array infinite-loops and hangs # the whole process the first time the widget's state is serialized + # (https://github.com/widgetti/ipyvolume/issues/371) color_traits: list[Tuple[Widget, str]] = [ *[(scatter, "color") for scatter in fig.scatters], *[(scatter, "color_selected") for scatter in fig.scatters], *[(mesh, "color") for mesh in fig.meshes], ] + first_bad: Optional[Tuple[Widget, str, np.ndarray]] = None for mark, trait in color_traits: value: object = getattr(mark, trait) if not (isinstance(value, np.ndarray) and value.ndim > 0): @@ -247,15 +249,28 @@ def _reject_ipyvolume_string_color_arrays(fig: IpyvolumeFigure) -> None: and value.size > 0 and isinstance(value.flat[0], str) ) - if is_string_array: - raise TypeError( - f"ipyvolume `{type(mark).__name__}.{trait}` was given an array " - f"of strings (dtype={value.dtype}, shape={value.shape}), which " - "ipyvolume's serializer infinite-loops on, hanging the entire " - "Shiny app. Pass a single color (e.g., color='red') or " - "per-point numeric data (e.g., values to map through a " - "colormap, or an (N, 3) RGB float array) instead." - ) + if not is_string_array: + continue + if first_bad is None: + first_bad = (mark, trait, value) + # Reset every offending trait, not just the one reported below: the + # marks' comm opens were deferred at construction and still serialize + # state at the next reactive flush (nothing closes widgets created + # during a failed render), so a string array left in place would hang + # the process despite the error + setattr(mark, trait, None) + + if first_bad is not None: + mark, trait, value = first_bad + raise TypeError( + f"ipyvolume `{type(mark).__name__}.{trait}` was given an array " + f"of strings (dtype={value.dtype}, shape={value.shape}), which " + "ipyvolume's serializer infinite-loops on, hanging the entire " + "Shiny app. Pass a single color (e.g., color='red') or " + "per-point numeric data (e.g., values to map through a " + "colormap, or an (N, 3) RGB float array) instead. " + "The invalid value was reset to None to keep the app running." + ) def as_widget_lonboard(x: object) -> Widget: diff --git a/tests/unit/test_ipyvolume.py b/tests/unit/test_ipyvolume.py index ecae956a9..db069f5be 100644 --- a/tests/unit/test_ipyvolume.py +++ b/tests/unit/test_ipyvolume.py @@ -73,9 +73,9 @@ def test_as_widget_rejects_per_point_string_colors(widget_session: Session) -> N ) with pytest.raises(TypeError, match=r"Scatter\.color.*array of strings"): as_widget(fig) - # A string array left in place would hang a later flush in ipyvolume's - # serializer - scatter.color = "red" + # The invalid value is sanitized away: a string array left in place + # would hang the next flush in ipyvolume's serializer + assert scatter.color is None def test_as_widget_rejects_string_colors_from_the_quickscatter_container( @@ -89,7 +89,7 @@ def test_as_widget_rejects_string_colors_from_the_quickscatter_container( ) with pytest.raises(TypeError, match=r"Scatter\.color.*array of strings"): as_widget(container) - container.figure.scatters[0].color = "red" + assert container.figure.scatters[0].color is None def test_as_widget_rejects_string_color_selected(widget_session: Session) -> None: @@ -103,7 +103,7 @@ def test_as_widget_rejects_string_color_selected(widget_session: Session) -> Non TypeError, match=r"Scatter\.color_selected.*array of strings" ): as_widget(fig) - scatter.color_selected = "green" + assert scatter.color_selected is None def test_as_widget_rejects_mesh_string_colors(widget_session: Session) -> None: @@ -118,7 +118,70 @@ def test_as_widget_rejects_mesh_string_colors(widget_session: Session) -> None: fig = ipv.Figure(meshes=[mesh]) with pytest.raises(TypeError, match=r"Mesh\.color.*array of strings"): as_widget(fig) - mesh.color = "red" + assert mesh.color is None + + +def test_flush_after_string_color_rejection_does_not_hang() -> None: + pytest.importorskip("ipyvolume") + + # The coercion error must sanitize the invalid trait: the scatter's + # deferred comm open still runs at the next flush (nothing closes widgets + # created during a failed render) and would infinite-loop in ipyvolume's + # serializer on a leftover string array. Run out-of-process so a + # regression fails here instead of hanging the test suite. + code = """ +import asyncio + +import shinywidgets._shinywidgets as sw +import ipyvolume as ipv +from shiny import App, reactive, ui +from shiny._connection import MockConnection +from shiny.reactive._reactives import Context +from shiny.session import session_context +from shinywidgets import render_ipyvolume + + +async def main(): + app = App(ui.page_fluid(), lambda input, output, session: None) + session = app._create_session(MockConnection()) + sw.SHINYWIDGETS_CDN_ONLY = True + + r = render_ipyvolume() + + @r + def bad(): + ipv.scatter( + [0.0, 1.0], [0.0, 1.0], [0.0, 1.0], color=["tomato", "seagreen"] + ) + return ipv.gcf() + + with session_context(session): + ctx = Context() + with ctx(): + try: + await r.render() + except TypeError: + pass + await reactive.flush() + print("FLUSH OK", flush=True) + + +asyncio.run(main()) +""" + try: + proc = subprocess.run( + [sys.executable, "-c", code], + capture_output=True, + text=True, + check=False, + timeout=60, + ) + except subprocess.TimeoutExpired: + pytest.fail( + "The flush after a rejected string color array hung -- the " + "rejected widget's deferred comm open serialized the invalid value" + ) + assert "FLUSH OK" in proc.stdout, proc.stderr def test_as_widget_accepts_a_scalar_string_color(widget_session: Session) -> None: