[Bug] VtkLocalView: unmounting the view while it fetches arrays throws "Cannot read properties of undefined (reading 'forEach')"

Open
#127 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
1-2 days
Newbie friendliness
66/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
javascript, python
Domain
frontend, web-dev

Research direction

Start with VtkSyncView.js unmount handling and localview.js beforeDelete() and updateViewState(), then trace the pending progress callback in vtk.js SynchronizableRenderWindow. The report gives a reproducible Python script and browser steps; reproduce the error by hiding the view during a fetch, then verify that completion after unmount produces no error and does not render the deleted window.

Written by the indexing model from the issue text.

Description

Describe the bug

If a VtkLocalView is unmounted (for example by a v_if) while the browser is still fetching the arrays of a scene update, the fetch's completion callback renders the render window the view has already deleted, and the page throws an uncaught error:

TypeError: Cannot read properties of undefined (reading 'forEach')
    at t.preRender (trame-vtk.js:3924:691)
    at t.render (trame-vtk.js:3924:765)
    at Object.<anonymous> (trame-vtk.js:3927:28917)
    at Object.u [as invokeProgressDone] (trame-vtk.js:1:27315)
    at end (trame-vtk.js:3927:27928)
    at async Promise.all (index 0)

Nothing is visibly wrong (the view is gone), but an app that hides a local view while a large scene is still loading gets this uncaught error: for example, when the user switches tabs during an animation over a slow connection.

Versions: trame 4.0.0, trame-vtk 2.11.17 (bundling vue-vtk-js 3.3.5), vtk 9.7.2, Python 3.12. Also seen with trame 3.13.2, trame-vtk 2.11.15, vtk 9.6.2.

To Reproduce

The script below shows 2 million random points in a VtkLocalView inside a v_if. "Update, then hide" sends new points, then hides the view 0.3 s later (HIDE_AFTER), while the browser is still fetching the arrays.

  1. pip install trame trame-vtk vtk, then python repro_unmount.py (it opens the page).
  2. Open the browser console.
  3. Click "Update, then hide". The view disappears; a moment later the console shows the error.
  4. Click "Show" and repeat: it happens every time.

With HIDE_AFTER = 10 (the fetch completes before the view is hidden) the error does not appear. If the browser fetches the points in under 0.3 s, raise POINTS or lower HIDE_AFTER.

Driven headless with Playwright, 5 rounds each:

trame / trame-vtk / vtk HIDE_AFTER = 0.3 HIDE_AFTER = 10
4.0.0 / 2.11.17 / 9.7.2 error in 5 of 5 no error in 5
3.13.2 / 2.11.15 / 9.6.2 error in 5 of 5 no error in 5

Code

"""A VtkLocalView unmounted while it fetches arrays throws in the browser.

    pip install trame trame-vtk vtk
    python repro_unmount.py

Open the browser console and click "Update, then hide": the server sends the view a new point cloud (tens of MB of arrays), then hides the view while the browser is still fetching them. When the fetch completes, the console shows "TypeError: Cannot read properties of undefined (reading 'forEach')". Click "Show" and repeat.
"""

import asyncio

from trame.app import get_server
from trame.ui.html import DivLayout
from trame.widgets import html
from trame.widgets import vtk as vtk_widgets
from vtkmodules.vtkFiltersSources import vtkPointSource
from vtkmodules.vtkRenderingCore import (
    vtkActor,
    vtkPolyDataMapper,
    vtkRenderer,
    vtkRenderWindow,
)

POINTS = 2_000_000  # enough arrays that the browser takes a while to fetch them
HIDE_AFTER = 0.3  # seconds: the view is hidden while the fetch is still running

points = vtkPointSource(number_of_points=POINTS)  # new random points at each update
mapper = vtkPolyDataMapper(input_connection=points.output_port)
renderer = vtkRenderer()
renderer.AddActor(vtkActor(mapper=mapper))
render_window = vtkRenderWindow(off_screen_rendering=True)
render_window.AddRenderer(renderer)

server = get_server(client_type="vue3")
server.state.show = True


def update_then_hide():
    points.Modified()
    view.update()  # the browser starts fetching the new arrays
    asyncio.get_running_loop().call_later(HIDE_AFTER, hide)


def hide():
    with server.state:
        server.state.show = False  # unmounts the view mid-fetch


with DivLayout(server):
    html.Button("Update, then hide", click=update_then_hide, disabled=("!show",))
    html.Button("Show", click="show = true", disabled=("show",))
    with html.Div(v_if=("show",), style="height: 80vh"):
        view = vtk_widgets.VtkLocalView(render_window)

if __name__ == "__main__":
    server.start()

Expected behavior

Unmounting a local view drops its pending scene updates: nothing is rendered after the view is gone, and no error is thrown.

Cause

The code is vue-vtk-js 3.3.5 (identical to its master) and vtk.js:

  • localview.js updateViewState() calls this.renderWindow.synchronize(remoteState) and awaits it.
  • vtk.js's synchronize() subscribes to the synchronizer context's onProgressDone; its callback calls renderWindow.render() once the arrays have arrived.
  • On unmount, VtkSyncView.js calls beforeDelete(), which deletes the render window but leaves that subscription in place (the context outlives the view). When the fetch completes, the callback renders the deleted window:
    vtk.js's delete() cleared its model, so preRender() calls forEach on an undefined model.renderers. The promise updateViewState() awaits then never settles.

A possible fix: unsubscribe the pending progress subscription in beforeDelete(), or have synchronize()'s callback skip render() when renderWindow.isDeleted().

Platform:

Device:

  • Desktop
  • Mobile

OS:

  • Windows
  • MacOS
  • Linux
  • Android
  • iOS

Browsers Affected:

  • Chrome
  • Firefox
  • Microsoft Edge
  • Safari
  • Opera
  • Brave
  • IE 11

Tested in Chromium 156.0.8078.4 (headless, through Playwright 1.64) on Linux, and by hand in Chromium 157.0.8103.50 on Linux, with the app reached through an SSH port forward. Other browsers not tested.

Dominant language
HTML
Stars
37
Forks
21
PR merge metrics
No merged PRs in 30d

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Kitware/trame-vtk

All issues in Kitware/trame-vtk

Similar issues

More Web Dev issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.