[PR #5800] [MERGED] fix: prevent memory leaks in experimental scripting sandbox #5357

Closed
opened 2026-03-17 02:48:48 +03:00 by kerem · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/hoppscotch/hoppscotch/pull/5800
Author: @jamesgeorge007
Created: 1/23/2026
Status: Merged
Merged: 1/27/2026
Merged by: @jamesgeorge007

Base: nextHead: fix/sandbox-memory-leaks


📝 Commits (1)

  • b19f85f fix: prevent memory leaks in experimental scripting sandbox

📊 Changes

8 files changed (+110 additions, -57 deletions)

View changed files

📝 packages/hoppscotch-common/src/components/MonacoScriptEditor.vue (+2 -0)
📝 packages/hoppscotch-common/src/services/inspection/index.ts (+72 -46)
📝 packages/hoppscotch-common/src/services/inspection/inspectors/environment.inspector.ts (+2 -3)
📝 packages/hoppscotch-js-sandbox/src/node/pre-request/experimental.ts (+2 -2)
📝 packages/hoppscotch-js-sandbox/src/node/test-runner/experimental.ts (+2 -2)
packages/hoppscotch-js-sandbox/src/utils/cage.ts (+26 -0)
📝 packages/hoppscotch-js-sandbox/src/web/pre-request/index.ts (+2 -2)
📝 packages/hoppscotch-js-sandbox/src/web/test-runner/index.ts (+2 -2)

📄 Description

Closes #5760
Closes FE-1116

Memory usage with the experimental scripting sandbox spirals out of control during collection runs. Profiling revealed ~30GB allocations for just 5 requests, which eventually caused the tab to crash. This PR addresses the root causes.

What's changed

  • Cached WASM cage: Reuse a single FaradayCage instance via acquireCage() to avoid repeated WASM module allocations. Tests use fresh cages to avoid QuickJS GC corruption.
  • InspectionService reactive cleanup: Uses effectScope to properly dispose computed refs, debounced watchers, and inner watchers on re-initialisation. The previous nested-watcher structure created new watchers on every tab switch without cleanup, causing watcher and timer accumulation.
  • Environment inspector optimization: Switched from Array.includes() (O(n)) to Set.has() (O(1)) for variable existence checks — noticeable when you have 100+ environment variables.
  • Monaco editor model disposal: Added editorModel.dispose() on unmount to clean up script editor instances.

Notes to reviewers

Use this sample collection for profiling, which has 5 identical requests with a relatively heavy pre-request script.

Profiling showed a memory drop from ~30GB to ~4GB for the above collection run (86% reduction). The tab no longer crashes.


Summary by cubic

Fixes runaway memory usage in the experimental scripting sandbox by caching and serialising access to the WASM cage, and cleaning up reactive watchers and editor models. Memory drops from ~30GB to ~4GB for a 5-request run, preventing tab crashes and meeting FE-1116 and #5760.

  • Bug Fixes
    • Cache and reuse a single FaradayCage instance with serialised access; use a fresh cage in tests to avoid QuickJS GC issues.
    • Dispose and reinitialise the InspectionService watcher correctly; create debounced refs once to stop timer leaks.
    • Use Set.has for environment variable checks in the inspector to avoid costly scans.
    • Dispose Monaco editor models on unmount to free editor memory.

Written for commit b19f85f2c5. Summary will update on new commits.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/hoppscotch/hoppscotch/pull/5800 **Author:** [@jamesgeorge007](https://github.com/jamesgeorge007) **Created:** 1/23/2026 **Status:** ✅ Merged **Merged:** 1/27/2026 **Merged by:** [@jamesgeorge007](https://github.com/jamesgeorge007) **Base:** `next` ← **Head:** `fix/sandbox-memory-leaks` --- ### 📝 Commits (1) - [`b19f85f`](https://github.com/hoppscotch/hoppscotch/commit/b19f85f2c51a203a1f82c57daa21996bf2107afe) fix: prevent memory leaks in experimental scripting sandbox ### 📊 Changes **8 files changed** (+110 additions, -57 deletions) <details> <summary>View changed files</summary> 📝 `packages/hoppscotch-common/src/components/MonacoScriptEditor.vue` (+2 -0) 📝 `packages/hoppscotch-common/src/services/inspection/index.ts` (+72 -46) 📝 `packages/hoppscotch-common/src/services/inspection/inspectors/environment.inspector.ts` (+2 -3) 📝 `packages/hoppscotch-js-sandbox/src/node/pre-request/experimental.ts` (+2 -2) 📝 `packages/hoppscotch-js-sandbox/src/node/test-runner/experimental.ts` (+2 -2) ➕ `packages/hoppscotch-js-sandbox/src/utils/cage.ts` (+26 -0) 📝 `packages/hoppscotch-js-sandbox/src/web/pre-request/index.ts` (+2 -2) 📝 `packages/hoppscotch-js-sandbox/src/web/test-runner/index.ts` (+2 -2) </details> ### 📄 Description Closes #5760 Closes FE-1116 Memory usage with the experimental scripting sandbox spirals out of control during collection runs. Profiling revealed ~30GB allocations for just 5 requests, which eventually caused the tab to crash. This PR addresses the root causes. ### What's changed - **Cached WASM cage**: Reuse a single `FaradayCage` instance via `acquireCage()` to avoid repeated WASM module allocations. Tests use fresh cages to avoid QuickJS GC corruption. - **InspectionService reactive cleanup**: Uses `effectScope` to properly dispose computed refs, debounced watchers, and inner watchers on re-initialisation. The previous nested-watcher structure created new watchers on every tab switch without cleanup, causing watcher and timer accumulation. - **Environment inspector optimization**: Switched from `Array.includes()` (O(n)) to `Set.has()` (O(1)) for variable existence checks — noticeable when you have 100+ environment variables. - **Monaco editor model disposal**: Added `editorModel.dispose()` on unmount to clean up script editor instances. ### Notes to reviewers Use this [sample collection](https://github.com/user-attachments/files/24823182/perf-validate-col.json) for profiling, which has 5 identical requests with a relatively heavy pre-request script. Profiling showed a memory drop from ~30GB to ~4GB for the above collection run (86% reduction). The tab no longer crashes. <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Fixes runaway memory usage in the experimental scripting sandbox by caching and serialising access to the WASM cage, and cleaning up reactive watchers and editor models. Memory drops from ~30GB to ~4GB for a 5-request run, preventing tab crashes and meeting FE-1116 and #5760. - **Bug Fixes** - Cache and reuse a single FaradayCage instance with serialised access; use a fresh cage in tests to avoid QuickJS GC issues. - Dispose and reinitialise the InspectionService watcher correctly; create debounced refs once to stop timer leaks. - Use `Set.has` for environment variable checks in the inspector to avoid costly scans. - Dispose Monaco editor models on unmount to free editor memory. <sup>Written for commit b19f85f2c51a203a1f82c57daa21996bf2107afe. Summary will update on new commits.</sup> <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
kerem 2026-03-17 02:48:48 +03:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/hoppscotch#5357
No description provided.