codeant-ai-for-open-source[bot] commented on code in PR #41111:
URL: https://github.com/apache/superset/pull/41111#discussion_r3419034395


##########
superset-frontend/plugins/plugin-chart-echarts/src/components/Echart.tsx:
##########
@@ -201,6 +202,11 @@ function Echart(
         chartRef.current?.on(name, handler);
       });
 
+      (queryEventHandlers || []).forEach(({ name, query, handler }) => {
+        chartRef.current?.off(name, handler);
+        chartRef.current?.on(name, query, handler);
+      });

Review Comment:
   **Suggestion:** Query-based handlers are deregistered using the current 
function reference only, so when a handler is replaced or removed from 
`queryEventHandlers`, the previous listener can remain attached and continue 
firing. This causes stale callbacks and duplicate event execution over time. 
Track and explicitly clean up prior query listeners (or clear by event name 
before re-registering) so removed/updated handlers are actually detached. 
[stale reference]
   
   <details>
   <summary><b>Severity Level:</b> Critical 🚨</summary>
   
   ```mdx
   - ❌ X-axis label clicks dispatch duplicate setDataMask cross-filters.
   - ⚠️ Timeseries dashboards accumulate ECharts listeners during interactions.
   ```
   </details>
   <details>
   <summary><b>Steps of Reproduction ✅ </b></summary>
   
   ```mdx
   1. Open a dashboard (or Explore view) that uses an ECharts timeseries-based 
chart (e.g.
   timeseries bar/line/area/scatter) so that `EchartsTimeseries` is rendered as 
the chart
   component
   
(`superset-frontend/plugins/plugin-chart-echarts/src/Timeseries/EchartsTimeseries.tsx:41-21`).
   
   2. Ensure the chart is configured with no groupby dimensions and a 
categorical x-axis so
   `canCrossFilterByXAxis` is true and the X-axis cross-filter path is active
   (`EchartsTimeseries.tsx:76-78`), and `emitCrossFilters` is true so 
`handleXAxisChange`
   actually calls `setDataMask` (`EchartsTimeseries.tsx:65-73`).
   
   3. Click an x-axis label multiple times to toggle the cross-filter 
selection; each click
   runs `handleXAxisChange` which calls
   `setDataMask(getXAxisCrossFilterDataMask(xAxisValue).dataMask)`
   (`EchartsTimeseries.tsx:65-72`), updating `selectedValues` and causing
   `getXAxisCrossFilterDataMask` to be recreated because it depends on 
`selectedValues`
   (`EchartsTimeseries.tsx:117-53); this in turn recreates `handleXAxisChange`,
   `handleXAxisLabelClick` (`EchartsTimeseries.tsx:12-18`), and the 
`queryEventHandlers`
   array (`EchartsTimeseries.tsx:21-29`).
   
   4. On each such re-render, `Echart`'s effect (`Echart.tsx:198-208, 86-94`) 
runs and
   registers query-based listeners by calling `chartRef.current?.off(name, 
handler)` and then
   `chartRef.current?.on(name, query, handler)` for the *new* handler 
(`Echart.tsx:205-207);
   because the previous handler reference is not used when calling `off`, 
ECharts does not
   remove the old listener, so multiple `click` handlers for `xAxis.category` 
accumulate;
   subsequent clicks on x-axis labels trigger `handleXAxisLabelClick` and 
`handleXAxisChange`
   multiple times, causing duplicate `setDataMask` calls and a growing number 
of attached
   listeners over the lifetime of the chart instance.
   ```
   </details>
   
   [![Fix in 
Cursor](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-cursor-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=82bbe349a2844697b02700d4f74fea2a&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
 [![Fix in VSCode 
Claude](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-vscode-claude-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=82bbe349a2844697b02700d4f74fea2a&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
   
   *(Use Cmd/Ctrl + Click for best experience)*
   <details>
   <summary><b>Prompt for AI Agent 🤖 </b></summary>
   
   ```mdx
   This is a comment left during a code review.
   
   **Path:** 
superset-frontend/plugins/plugin-chart-echarts/src/components/Echart.tsx
   **Line:** 205:208
   **Comment:**
        *Stale Reference: Query-based handlers are deregistered using the 
current function reference only, so when a handler is replaced or removed from 
`queryEventHandlers`, the previous listener can remain attached and continue 
firing. This causes stale callbacks and duplicate event execution over time. 
Track and explicitly clean up prior query listeners (or clear by event name 
before re-registering) so removed/updated handlers are actually detached.
   
   Validate the correctness of the flagged issue. If correct, How can I resolve 
this? If you propose a fix, implement it and please make it concise.
   Once fix is implemented, also check other comments on the same PR, and ask 
user if the user wants to fix the rest of the comments as well. if said yes, 
then fetch all the comments validate the correctness and implement a minimal fix
   ```
   </details>
   <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41111&comment_hash=cb751328396f881c57813bd1c033c82f3e282465d8ef213de4c87e5da48fdb9a&reaction=like'>👍</a>
 | <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41111&comment_hash=cb751328396f881c57813bd1c033c82f3e282465d8ef213de4c87e5da48fdb9a&reaction=dislike'>👎</a>



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to