primefaces: Charts: deferred renderer sometimes called multiple times, causing errors

Describe the bug

In the ChartJS components (LineChart, BarChart etc.) there is a problem, that when you refresh the component more than once, while it is invisible (e.g. in another TabView tab), there is an error during rendering, when the component is finally visible again.

E.g.:

Uncaught Error: Canvas is already in use. Chart with ID '1' must be destroyed before the canvas with ID 'frmTest:tabView:chart_canvas' can be reused.
    bn http://localhost:8080/primefaces-test/jakarta.faces.resource/chartjs/chartjs.js.xhtml?ln=primefaces&v=14.0.0-SNAPSHOT:13
    _render http://localhost:8080/primefaces-test/jakarta.faces.resource/chartjs/chartjs.js.xhtml?ln=primefaces&v=14.0.0-SNAPSHOT:96
    render http://localhost:8080/primefaces-test/jakarta.faces.resource/core.js.xhtml?ln=primefaces&v=14.0.0-SNAPSHOT:5810
    renderDeferred http://localhost:8080/primefaces-test/jakarta.faces.resource/core.js.xhtml?ln=primefaces&v=14.0.0-SNAPSHOT:5794
    invokeDeferredRenders http://localhost:8080/primefaces-test/jakarta.faces.resource/core.js.xhtml?ln=primefaces&v=14.0.0-SNAPSHOT:1413
    postTabShow http://localhost:8080/primefaces-test/jakarta.faces.resource/components.js.xhtml?ln=primefaces&v=14.0.0-SNAPSHOT:30309
    show http://localhost:8080/primefaces-test/jakarta.faces.resource/components.js.xhtml?ln=primefaces&v=14.0.0-SNAPSHOT:30092
    oncomplete http://localhost:8080/primefaces-test/jakarta.faces.resource/components.js.xhtml?ln=primefaces&v=14.0.0-SNAPSHOT:30137

I tried to deduce the root cause of this problem, and apparently the ChartJs components are implemented in JavaScript as DeferredWidget. In its current implementation, a DeferredWidget won’t call a direct rendering when an invisible widget is initialized or refreshed, but instead add a callback to render, after the widget gets visible again. However, if you refresh a DeferredWidget twice (or more), while it is invisible, the DeferredWidget will “stack” the deferred renderings, so when component get visible again, the _render() function will be called twice (or more often) in a row. In case of the ChartJs widget this will cause an error, because the first _render() call initializes a ChartJs instance on the canvas, while the second _render() call can’t do that as the canvas is already in use.

Generally, I’m not sure if a DeferredWidget should plan multiple deferred renderings while invisible anyways. In my opinion the DeferredWidget should detect whether there is already a deferred rendering planned, and then not plan another one. But in case this is how you want the DeferredWidget to behave, then I think the rendering of the ChartJs widgets should be made idempotent, so when two (or more) _render() calls are executed in a row, the widget does not produce an error and still show a functioning ChartJs graph.

This problem exists in the latest master version (14.0.0-SNAPSHOT), as well as in the 13.x branch.

Reproducer

I created a reproducer in the forked PrimeFaces-Test repository: https://github.com/metaxmx/primefaces-test

How to Reproduce:

  1. Start Test-App and open test.xhtml
  2. Open Developer Console
  3. Switch Tab, so the Chart is invisible
  4. Press the “Refresh Chart” button multiple times (which will update the chart per AJAX)
  5. Switch back to Chart tab, so it is visible again
  6. An error from ChartJS will be displayed on the javascript console

Expected behavior

When the chart gets visibile again, it should initialize only one ChartJS instance, and not try to create multiple, overlapping charts

PrimeFaces edition

Community

PrimeFaces version

14.0.0-SNAPSHOT

Theme

No response

JSF implementation

Mojarra

JSF version

4.0

Java version

20

Browser(s)

Firefox

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 16 (12 by maintainers)

Commits related to this issue

Most upvoted comments

Good debugging @metaxmx ! I am wondering if this method.

addDeferredRender: function(widgetId, containerId, fn) {
     this.deferredRenders.push({widget: widgetId, container: containerId, callback: fn});
}

Shouldn’t blindly push the widget onto the array but should remove an existing one with that widgetId first before add so it would be the “last” version of that widget pushed onto the stack?

Seems to work fine the patch of the javascript. So the fix i apply is two fold. On the one hand, yes I optimize the logic that is deciding what elements of the page to refresh to take into account if the popup is open or not and if the popup is currently not open I no longer re-render the internal contents. But this is a fix that uses for the specific leak i encountered but not applicable application wide.

On the other hand i got to the top level xhtml page template inherited by most other pages. <script type="text/javascript" src="#{resource['script:primefaces/TRK35672_addDeferredRender_memory_leak.js']}"/>

Put in there this little snippet. And sneak into that javascript the following:

/**
 * PrimeFaces versions 6.2 and higher have a serious bug causing a memory leak
 * in the PrimeFaces.deferredRenders array.
 * See the bug report: hidden URL 
 *
 * The issue occurs when a page refreshes areas that are not currently visible
 * (e.g., refreshing the content of a hidden popup). PrimeFaces defers the rendering
 * of the widget's content, but it blindly adds new deferred rendering logic to an array
 * without checking for existing entries.
 *
 * This snippet of code is adapted from:
 * https://github.com/primefaces/primefaces/blob/15.0.0/primefaces/src/main/resources/META-INF/resources/primefaces/core/core.widget.js
 *
 * The crucial step here is the "remove existing" logic that was missing.
 *
 */
if (PrimeFaces && PrimeFaces.addDeferredRender) {
    PrimeFaces.addDeferredRender = function (widgetId, containerId, fn) {
        // Remove existing deferred renders with the same widgetId and containerId
        this.deferredRenders = this.deferredRenders.filter(deferredRender => {
            return !(deferredRender.widget === widgetId && deferredRender.container === containerId);
        });

        // Add new deferred render
        this.deferredRenders.push({widget: widgetId, container: containerId, callback: fn});
    };
}

Hi, my gut feeling would teel me, that we still might run into problem with this new implementation, if a DeferredWidget gets refreshed, while having multiple layers of invisible containers, where each container will trigger a addDeferredRender () with their container id.

However, I tried to reproduce the bug with your changed code, and even after wrapping the Chart into multiple layers of tabs, and refreshing it while different tabviews were currently making the chart invisible, I could not reproduce the original problem and everything worked fine.

So probably your revised fix does still solve the bug, and I’m happy if we keep it in the new form, instead of reverting the bugfix alltogether.

Thank you @melloware for the quick response and fix! Now everything works fine