Skip to content

fix: KALEIDO_RENDER_TIMEOUT#433

Open
bitmaybewise wants to merge 1 commit intoplotly:masterfrom
bitmaybewise:fix/render-timeout
Open

fix: KALEIDO_RENDER_TIMEOUT#433
bitmaybewise wants to merge 1 commit intoplotly:masterfrom
bitmaybewise:fix/render-timeout

Conversation

@bitmaybewise
Copy link

I managed to reproduce this issue #419 locally using this script:

"""Repro for plotly/Kaleido issue #419 (write_image hangs with many points)."""

import argparse
import time

import numpy as np
import plotly.graph_objects as go


def build_traces(num_points: int, lines: int, dtype: str):
    if lines < 1:
        raise ValueError("lines must be >= 1")

    points_per_line = num_points // lines
    remainder = num_points % lines
    numpy_dtype = np.float64 if dtype == "float" else np.int64
    traces = []

    for i in range(lines):
        count = points_per_line + (1 if i < remainder else 0)
        x = np.full(count, 1, dtype=numpy_dtype)
        y = np.full(count, 0.5, dtype=numpy_dtype)
        traces.append(go.Scatter(x=x, y=y))

    return traces


def main():
    parser = argparse.ArgumentParser(
        description="Reproduce Kaleido write_image hang with many datapoints."
    )
    parser.add_argument(
        "--points",
        type=int,
        default=4_914_834,
        help="Total number of datapoints (default matches issue #419).",
    )
    parser.add_argument(
        "--lines",
        type=int,
        default=1,
        help="Number of Scatter traces to split points across.",
    )
    parser.add_argument(
        "--dtype",
        choices=("float", "int"),
        default="float",
        help="Array dtype for x/y values.",
    )
    parser.add_argument(
        "--output",
        default="output.png",
        help="Output image path.",
    )
    args = parser.parse_args()

    traces = build_traces(args.points, args.lines, args.dtype)
    fig = go.Figure(data=traces)

    print(
        f"Writing image with {args.points} points across {args.lines} trace(s) "
        f"using {args.dtype} arrays..."
    )
    start = time.perf_counter()
    fig.write_image(args.output)
    elapsed = time.perf_counter() - start
    print(f"Done in {elapsed:.2f}s -> {args.output}")


if __name__ == "__main__":
    main()

This hangs:

uv run integration_tests/repro_issue_419.py
Writing image with 4914834 points across 1 trace(s) using float arrays...

Although I don't know how to fix the underlying issue making the process to hang, I think it's a sane to be able to define a timeout threshold, which is what I introduced in this pull request.

With the timeout:

KALEIDO_RENDER_TIMEOUT=10 uv run --project src/py integration_tests/repro_issue_419.py
Writing image with 4914834 points across 1 trace(s) using float arrays...
Traceback (most recent call last):
  File "integration_tests/repro_issue_419.py", line 71, in <module>
    main()
  File "integration_tests/repro_issue_419.py", line 65, in main
    fig.write_image(args.output)
  File "/Users/hercules/BitMaybeWise/github/Kaleido/src/py/.venv/lib/python3.8/site-packages/plotly/basedatatypes.py", line 3895, in write_image
    return pio.write_image(self, *args, **kwargs)
  File "/Users/hercules/BitMaybeWise/github/Kaleido/src/py/.venv/lib/python3.8/site-packages/plotly/io/_kaleido.py", line 510, in write_image
Wait expired, Browser is being closed by watchdog.
    img_data = to_image(
  File "/Users/hercules/BitMaybeWise/github/Kaleido/src/py/.venv/lib/python3.8/site-packages/plotly/io/_kaleido.py", line 380, in to_image
    img_bytes = kaleido.calc_fig_sync(
  File "/Users/hercules/BitMaybeWise/github/Kaleido/src/py/kaleido/__init__.py", line 191, in calc_fig_sync
    return _sync_server.oneshot_async_run(
  File "/Users/hercules/BitMaybeWise/github/Kaleido/src/py/kaleido/_sync_server.py", line 243, in oneshot_async_run
    raise TimeoutError(
TimeoutError: Kaleido sync call exceeded the timeout; Chrome termination attempted.

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.

1 participant