Skip to content

🔒 [security fix] Fix JSONP memory leak with hybrid cleanup strategy#221

Draft
deflis wants to merge 2 commits intomasterfrom
fix-jsonp-memory-leak-8630123667505666188
Draft

🔒 [security fix] Fix JSONP memory leak with hybrid cleanup strategy#221
deflis wants to merge 2 commits intomasterfrom
fix-jsonp-memory-leak-8630123667505666188

Conversation

@deflis
Copy link
Copy Markdown
Owner

@deflis deflis commented Apr 10, 2026

🎯 What: Fixed a memory leak in the jsonp utility function using a hybrid cleanup strategy.
⚠️ Risk: Continuous use of JSONP would lead to an accumulation of properties on the global window object, potentially causing memory issues in long-running applications.
🛡️ Solution:

  • Successful requests now trigger a delete window[id] to fully reclaim memory.
  • Timed-out requests replace the callback with a noop function to suppress errors from late-loading scripts.
  • Added and updated tests to verify these two distinct cleanup paths.

PR created automatically by Jules for task 8630123667505666188 started by @deflis

This commit addresses a memory leak in the JSONP utility function where
callback properties were accumulating on the global `window` object.

Based on user feedback, a hybrid approach has been implemented:
1. On success: The callback property is completely deleted from the
   `window` object (with a fallback to `undefined`) to ensure no memory
   leak occurs for the most common case.
2. On timeout: The callback property is replaced with a `noop` function.
   This prevents console `TypeError` if a late-arriving response script
   attempts to call the callback after the timeout.

Unit tests in `test/util/jsonp.test.ts` have been updated to verify
this behavior, including a robust check for property removal on success.

Co-authored-by: deflis <206113+deflis@users.noreply.github.com>
@google-labs-jules
Copy link
Copy Markdown

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the jsonp utility to improve memory management and error handling. The cleanup function now distinguishes between successful completions and timeouts; on success, the callback is deleted from the global window object to prevent memory leaks, while on timeout, it is replaced with a noop function to suppress console errors from delayed responses. Corresponding tests have been added to verify these behaviors. I have no feedback to provide.

This commit addresses a memory leak in the JSONP utility function where
callback properties were accumulating on the global `window` object.

A hybrid approach has been implemented based on user requirements:
- On success: The callback property is deleted from `window` to reclaim memory.
- On timeout: The callback is replaced with a `noop` function to suppress
  errors from late-loading scripts.

Additionally, a lint error was fixed by using optional catch binding
(`catch { ... }`) for the `delete` fallback logic.

Tests have been updated to verify property deletion on success and
`noop` replacement on timeout.

Co-authored-by: deflis <206113+deflis@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant