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


##########
superset-embedded-sdk/src/index.ts:
##########
@@ -277,7 +281,10 @@ export async function embedDashboard({
 
   function unmount() {
     log('unmounting');
-    //@ts-ignore
+
+    clearTimeout(refreshTokenTimerId);
+    refreshTokenTimerId = 'UNMOUNTED';
+
     mountPoint.replaceChildren();

Review Comment:
   **Suggestion:** `unmount()` clears the refresh timer but does not stop or 
close the Switchboard/message port, so the message channel and any defined 
methods/listeners can remain active after unmount, causing memory leaks and 
continued message handling. Stop the switchboard and close its underlying port 
(if present) during unmount. [resource leak]
   
   <details>
   <summary><b>Severity Level:</b> Critical 🚨</summary>
   
   ```mdx
   - ❌ Message ports/listeners may leak after unmount.
   - ⚠️ Long-running host pages accumulate memory leaks.
   - ⚠️ Embedded comms may still receive messages unexpectedly.
   ```
   </details>
   
   ```suggestion
       // Stop the switchboard and close the underlying MessagePort if 
available to avoid
       // leaving listeners/ports open after unmount.
       try {
         (ourPort as any).stop?.();
       } catch (e) {
         // ignore stop errors
       }
       try {
         (ourPort as any).port?.close?.();
       } catch (e) {
         // ignore close errors
       }
   
   ```
   <details>
   <summary><b>Steps of Reproduction ✅ </b></summary>
   
   ```mdx
   1. Call embedDashboard() (superset-embedded-sdk/src/index.ts:118) which 
eventually calls
   ourPort.start() at line 266 to begin iframe comms.
   
   2. Interact with the embedded dashboard; ourPort.defineMethod(...) is used 
(lines 267-279)
   and communication channel remains active.
   
   3. Call unmount() (lines 282-289). Current unmount clears the refresh timer 
(lines
   285-286) and removes iframe (line 288) but does not call any Switchboard 
stop/port close
   methods.
   
   4. Because ourPort.was started at line 266 and no stop/close is invoked on 
unmount, the
   underlying MessageChannel/handlers defined by Switchboard can remain active, 
causing
   retained listeners/ports and potential memory leaks.
   ```
   </details>
   <details>
   <summary><b>Prompt for AI Agent 🤖 </b></summary>
   
   ```mdx
   This is a comment left during a code review.
   
   **Path:** superset-embedded-sdk/src/index.ts
   **Line:** 288:288
   **Comment:**
        *Resource Leak: `unmount()` clears the refresh timer but does not stop 
or close the Switchboard/message port, so the message channel and any defined 
methods/listeners can remain active after unmount, causing memory leaks and 
continued message handling. Stop the switchboard and close its underlying port 
(if present) during unmount.
   
   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.
   ```
   </details>



-- 
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