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
- master-Branch: Commit 48f771add1e7430f201432e9635d5e5a4588bbe3
- jakarta-Branch: Commit d97a0eedb03f797bd2829d3a427c3e1e02d78ce4
How to Reproduce:
- Start Test-App and open test.xhtml
- Open Developer Console
- Switch Tab, so the Chart is invisible
- Press the “Refresh Chart” button multiple times (which will update the chart per AJAX)
- Switch back to Chart tab, so it is visible again
- 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
- Fix #10839: DeferredRender list unique by widgetId — committed to melloware/primefaces by melloware 3 years ago
- Fix #10839: 13.0.3 DeferredRender list unique by widgetId — committed to melloware/primefaces by melloware 3 years ago
- Fix #10839: DeferredRender list unique by widgetId (#10840) — committed to primefaces/primefaces by melloware 3 years ago
- Fix #10839: 13.0.3 DeferredRender list unique by widgetId (#10842) — committed to primefaces/primefaces by melloware 3 years ago
- Fix #10839: Improved deferred widget handling — committed to melloware/primefaces by melloware 3 years ago
- Fix #10839: Improved deferred widget handling (#10906) — committed to primefaces/primefaces by melloware 3 years ago
- Fix #10839: 13.0.3 Improved deferred widget handling — committed to melloware/primefaces by melloware 3 years ago
- Fix #10839: 13.0.3 Improved deferred widget handling (#10910) — committed to primefaces/primefaces by melloware 3 years ago
Good debugging @metaxmx ! I am wondering if this method.
Shouldn’t blindly push the widget onto the array but should remove an existing one with that
widgetIdfirst 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:
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