Skip to content

fix(ipyvolume): raise a clear error for per-point string colors instead of hanging the app - #493

Open
cpsievert wants to merge 5 commits into
mainfrom
fix/ipyvolume-string-color-guard
Open

cpsievert wants to merge 5 commits into
mainfrom
fix/ipyvolume-string-color-guard

Conversation

@cpsievert

Copy link
Copy Markdown
Collaborator

Why

Passing a per-point string color array to an ipyvolume scatter or mesh — e.g. ipv.scatter(x, y, z, color=['tomato', 'seagreen']) — makes ipyvolume's own color_to_binary_or_json serializer spin forever ('tomato'[0]' == 't', then 't'[0] == 't', ...) the first time the widget's state is serialized. The symptom is much worse than a bad plot: the entire Shiny process hangs — every session, every request — with no error anywhere. Users have no way to connect the freeze to their color argument.

With this PR, @render_ipyvolume raises an actionable TypeError at coercion time instead:

TypeError: ipyvolume `Scatter.color` was given an array of strings (dtype=<U8, shape=(2,)),
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.

What changed

  • as_widget_ipyvolume() now walks the returned figure's scatters (traits color and color_selected) and meshes (trait color) — exactly the traits ipyvolume serializes with color_to_binary_or_json — and rejects any non-scalar array with string dtype (<U/bytes) or an object array of strings. The check runs before any state serialization, so the hang point is never reached.
  • Legitimate colors are unaffected: scalar strings (color='red', stored by ipyvolume as a 0-d array) and numeric per-point arrays (the (N, 3)/(N, 4) RGB(A) float form used throughout ipyvolume's docs) still pass, and are covered by tests.

Verification

  • Confirmed the upstream hang first: a subprocess running ipv.scatter(..., color=['tomato', 'seagreen']) infinite-loops in ipyvolume/serialize.py:327 (killed via a hard timeout).
  • New unit tests (written failing-first) assert the TypeError for Scatter.color, Scatter.color_selected, and Mesh.color string arrays (including through ipv.quickscatter()'s Container), and that scalar strings and numeric arrays still pass.
  • End-to-end check in a real session: a render that constructs the bad scatter now surfaces the TypeError and the reactive flush completes — no hang (previously the process froze in _open_shiny_comm -> get_state()).
  • make check-format and make pyright clean; test_ipyvolume.py, test_as_widget.py, test_render_widget.py, and test_render_widget_base_lifecycle.py all pass.

…ad 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.
@netlify

netlify Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for shinywidgets canceled.

Name Link
🔨 Latest commit 6b867aa
🔍 Latest deploy log https://app.netlify.com/projects/shinywidgets/deploys/6b00e23d3b2d6d5c21429a18

@github-actions

github-actions Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

@github-actions
github-actions Bot temporarily deployed to pull request November 13, 2026 23:09 Inactive
@cpsievert
cpsievert requested a balanced review from Copilot November 20, 2026 16:34

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

A critical issue remains: rejected scatter or mesh widgets retain deferred comm-opening effects that serialize invalid color arrays during a later reactive flush and hang the process.

1 open issue
What changed in this PR

This PR adds ipyvolume color validation to shinywidgets’ coercion path, aiming to replace serializer hangs with actionable errors.

Changes:

  • Rejects per-point string color arrays on figure scatters and meshes, including figures returned in containers.
  • Adds tests for rejected string arrays and accepted scalar strings and numeric colors.
  • Documents the intended fix in the changelog.
File Description
tests/​unit/​test_ipyvolume.py Tests invalid and supported color inputs.
shinywidgets/​_as_widget.py Validates ipyvolume color arrays during coercion.
CHANGELOG.md Describes the intended hang prevention and error guidance.

Review effort: Balanced

Comment thread shinywidgets/_as_widget.py Outdated
Comment on lines +250 to +251
if is_string_array:
raise TypeError(

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed — I reproduced the hang: after the coercion error, the rejected scatter's deferred _open_shiny_comm still runs get_state() at the next reactive flush and infinite-loops in color_to_binary_or_json (nothing closes widgets created during a failed render, since an errored render's context is never invalidated).

Fixed in 6b867aa by sanitizing rather than just raising: _reject_ipyvolume_string_color_arrays now resets every offending trait to None before raising the TypeError, so the pending comm opens serialize harmlessly. I considered closing render-created widgets on render error instead, but that's a new lifecycle invariant across every renderer in service of an ipyvolume edge case — and it still wouldn't cover post-render assignment.

The regression test you asked for is test_flush_after_string_color_rejection_does_not_hang: it runs the full render path, leaves the invalid value in place, and flushes — out-of-process with a hard timeout, since in-process a regression would hang the suite instead of failing. Verified it hangs against the pre-fix code and passes with the fix. The existing rejection tests also dropped their manual color resets and now assert the trait was reset to None.

Comment thread CHANGELOG.md Resolved
…wed 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).
@github-actions
github-actions Bot temporarily deployed to pull request November 21, 2026 00:17 Inactive

This branch was previously deployed

1 inactive deployment
pull request — 6b867aa0 Deployed Nov 21, 2026 by github-actions[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants