Skip to content

Conversation

@floating-dynamo
Copy link

@floating-dynamo floating-dynamo commented Dec 12, 2025

Description

  • Current Architecture: It seems the SDK creates ONE Global Isolate (a single Virtual Machine) when the server starts, and reuses it for every single request (2000+ requests).
  • The Leak: The V8 engine inside that single Isolate is accumulating "Script Compilation" data, fragmentation, or hidden caches from running 2000 different closures. Eventually, the Isolate's heap limit (usually 128MB) explodes, or it leaks into the main process.

The Strategy:

  1. Create a fresh Isolate for every single request.
  2. Run the code.
  3. Dispose (Destroy) the Isolate immediately.

This guarantees memory cleanup because the entire memory block is freed by C++.

Screenshot
If relevant, add a screenshot or two of the changes you made.


Note

Switches Node evaluate runtime to create and dispose a fresh isolated-vm per request, removing the global context and allowing configurable isolate options.

  • Node evaluate runtime (packages/sdks/src/functions/evaluate/node-runtime/node-runtime.ts):
    • Create a fresh ivm.Isolate per call in runInNode and dispose it in finally.
    • Remove global Context/singleton isolate management (setIsolateContext/getIsolateContext).
    • Update setIvm to accept optional IsolateOptions and store them in IVM_OPTIONS for per-request use.
    • Inline isolate setup (global, log, BUILDER_IVM) inside runInNode; keep state propagation via BUILDER_SET_STATE and argument proxying unchanged.

Written by Cursor Bugbot for commit 6c588e5. This will update automatically on new commits. Configure here.

…memory leaks

- Removed global IVM_CONTEXT and related functions to ensure isolates are created anew for each request.
- Updated `setIvm` to utilize options per request instead of globally.
- Enhanced `runInNode` to initialize the isolate and context directly within the function, improving memory management and cleanup.
- Added comments to clarify changes and rationale for the new approach.

This refactor aims to enhance performance and reliability by eliminating potential memory leaks associated with persistent isolates.
…roved memory management

- Added `IVM_OPTIONS` to store isolate options, defaulting to a memory limit of 128.
- Updated `setIvm` to accept and store options for each request, enhancing flexibility.
- Modified `runInNode` to utilize stored options when creating isolates, ensuring consistent memory management.
- Improved comments for clarity on the new approach and its benefits.

This refactor aims to further optimize memory usage and isolate management in the node runtime.
@changeset-bot
Copy link

changeset-bot bot commented Dec 12, 2025

⚠️ No Changeset found

Latest commit: 6c588e5

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@nx-cloud
Copy link

nx-cloud bot commented Dec 12, 2025

View your CI Pipeline Execution ↗ for commit 6c588e5

Command Status Duration Result
nx test @snippet/react ✅ Succeeded 2m 4s View ↗

☁️ Nx Cloud last updated this comment at 2025-12-12 13:23:06 UTC

- Eliminated the unused `Context` import from `isolated-vm` in the node-runtime file.
- This change simplifies the code and improves readability by removing unnecessary dependencies.

This refactor contributes to maintaining a clean and efficient codebase.
@floating-dynamo floating-dynamo self-assigned this Dec 12, 2025
@floating-dynamo floating-dynamo changed the title fix[testVueSdk]: try to not use global isolate and create fresh instance for each request fix[testVueSdk]: try to not use a global isolate instance and create fresh instance for each request Dec 12, 2025
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.

2 participants