Skip to content

fix(perf):excessive memory consumption in v3.4.x#8198

Closed
abhishekp-bruno wants to merge 5 commits into
usebruno:mainfrom
abhishekp-bruno:fix/excessive-memory-consumption
Closed

fix(perf):excessive memory consumption in v3.4.x#8198
abhishekp-bruno wants to merge 5 commits into
usebruno:mainfrom
abhishekp-bruno:fix/excessive-memory-consumption

Conversation

@abhishekp-bruno

@abhishekp-bruno abhishekp-bruno commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

JIRA Link - https://usebruno.atlassian.net/browse/BRU-3509

Description

Contribution Checklist:

  • I've used AI significantly to create this pull request
  • The pull request only addresses one issue or adds one feature.
  • The pull request does not introduce any breaking changes
  • I have added screenshots or gifs to help explain the change if applicable.
  • I have read the contribution guidelines.
  • Create an issue and link to the pull request.

Note: Keeping the PR small and focused helps make it easier to review and merge. If you have multiple changes you want to make, please consider submitting them as separate pull requests.

Publishing to New Package Managers

Please see here for more information.

Summary by CodeRabbit

  • Bug Fixes

    • Fixed QuickJS sandbox memory leaks by ensuring VM resources and temporary handles are cleaned up.
  • New Features

    • Added a memory-profiling CLI and an npm script to run deterministic memory benchmarks.
    • CI now runs a dedicated runner memory benchmark step.
  • Tests

    • Added automated memory benchmark scenarios that fail the run if WASM memory thresholds are exceeded.

@coderabbitai

coderabbitai Bot commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 56649036-16bb-45da-b96a-53bd378e196b

📥 Commits

Reviewing files that changed from the base of the PR and between 3c0caba and 729e94d.

📒 Files selected for processing (16)
  • .github/actions/tests/run-benchmark-tests/action.yml
  • package.json
  • packages/bruno-js/src/sandbox/quickjs/index.js
  • packages/bruno-js/src/sandbox/quickjs/shims/bru.js
  • packages/bruno-js/src/sandbox/quickjs/shims/bruno-request.js
  • packages/bruno-js/src/sandbox/quickjs/shims/bruno-response.js
  • packages/bruno-js/src/sandbox/quickjs/shims/lib/axios.js
  • packages/bruno-js/src/sandbox/quickjs/shims/lib/crypto-utils.js
  • packages/bruno-js/src/sandbox/quickjs/shims/lib/jwt.js
  • packages/bruno-js/src/sandbox/quickjs/shims/lib/nanoid.js
  • packages/bruno-js/src/sandbox/quickjs/shims/lib/path.js
  • packages/bruno-js/src/sandbox/quickjs/shims/lib/uuid.js
  • packages/bruno-js/src/sandbox/quickjs/shims/test.js
  • packages/bruno-js/src/sandbox/quickjs/utils/index.js
  • scripts/memory-profile.js
  • tests/benchmarks/memory/runner-memory.js
🚧 Files skipped from review as they are similar to previous changes (15)
  • package.json
  • packages/bruno-js/src/sandbox/quickjs/shims/bruno-request.js
  • packages/bruno-js/src/sandbox/quickjs/shims/lib/nanoid.js
  • packages/bruno-js/src/sandbox/quickjs/index.js
  • packages/bruno-js/src/sandbox/quickjs/utils/index.js
  • packages/bruno-js/src/sandbox/quickjs/shims/lib/crypto-utils.js
  • packages/bruno-js/src/sandbox/quickjs/shims/bruno-response.js
  • packages/bruno-js/src/sandbox/quickjs/shims/lib/path.js
  • .github/actions/tests/run-benchmark-tests/action.yml
  • packages/bruno-js/src/sandbox/quickjs/shims/lib/jwt.js
  • packages/bruno-js/src/sandbox/quickjs/shims/lib/axios.js
  • tests/benchmarks/memory/runner-memory.js
  • packages/bruno-js/src/sandbox/quickjs/shims/test.js
  • packages/bruno-js/src/sandbox/quickjs/shims/bru.js
  • scripts/memory-profile.js

Walkthrough

Dispose QuickJS VM contexts, marshalled handles, and eval-result handles; add evalCodeAndDispose; update shims and execution paths to free QuickJS-managed handles; add a memory benchmark runner, CI step, and a profiling CLI to detect WASM/external memory growth.

Changes

QuickJS Memory Leak Prevention and Benchmarking

Layer / File(s) Summary
Core handle disposal utilities
packages/bruno-js/src/sandbox/quickjs/utils/index.js, packages/bruno-js/src/sandbox/quickjs/shims/*
marshallToVm disposes each child handle after assignment; new evalCodeAndDispose(vm, code, filename?) evaluates code and disposes the returned handle; many shims import and use the helper.
VM execution context cleanup
packages/bruno-js/src/sandbox/quickjs/index.js
executeQuickJsVm and executeQuickJsVmAsync capture/dispose intermediate eval handles and always dispose the VM context in finally blocks.
Direct shim eval result disposal
packages/bruno-js/src/sandbox/quickjs/shims/bru.js, bruno-request.js, bruno-response.js
Shims that previously ignored vm.evalCode results now capture and dispose the returned error/value; bru.js also disposes its runner object after wiring.
Library and test shims using evalCodeAndDispose
packages/bruno-js/src/sandbox/quickjs/shims/lib/*, packages/bruno-js/src/sandbox/quickjs/shims/test.js
Library/test shims import and use evalCodeAndDispose instead of calling vm.evalCode directly, ensuring eval handles are disposed after injection.
Memory regression benchmarking suite
package.json, .github/actions/tests/run-benchmark-tests/action.yml, tests/benchmarks/memory/runner-memory.js
Adds test:benchmark:memory npm script, CI step to run it, and runner-memory.js which measures external (WASM) and RSS growth across scenarios, writes tests/benchmarks/results/memory.json, and fails CI on threshold breaches.
Memory profiling diagnostic tool
scripts/memory-profile.js
New CLI that samples process memory across iterations, detects near-WASM-limit or OOM-like errors, forces GC, prints delta summaries, and supports early stopping for analysis.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • usebruno/bruno#7692: Also changes QuickJS execution context handling to avoid leaks; related to sandbox lifecycle fixes.
  • usebruno/bruno#7915: Introduced benchmark framework and CI pieces that this PR extends.
  • usebruno/bruno#6662: Related Bruno shim edits in the QuickJS shim surface area.

Suggested reviewers

  • helloanoop
  • lohit-bruno
  • naman-bruno
  • bijin-bruno

Poem

QuickJS handles let go at last,
Benchmarks spy the memory cast.
GC rings twice, the snapshots sing,
CI watches what fixes bring.
Sandboxed leaks now breathe their past.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 6.25% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the primary change: fixing excessive memory consumption in v3.4.x. It's concise, specific, and accurately reflects the changeset's focus on memory-leak fixes and QuickJS sandbox optimizations.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@scripts/memory-profile.js`:
- Around line 23-24: ITERATIONS and SAMPLE_EVERY are parsed from env but never
validated, which can produce NaN/Infinity during loop/modulo/division
operations; add a validation guard after their parsing to ensure both are finite
integers > 0 (fallback to sane defaults or exit with an error), and apply the
same validation where these values are used later (references: ITERATIONS,
SAMPLE_EVERY, and any logic computing sampling/reporting in the memory profiling
loop and summary code); if validation fails, either set safe defaults or
throw/exit with a clear message so subsequent modulo/division (sampling) logic
cannot operate on invalid numbers.
- Line 55: Replace the hardcoded pathname '/tmp/req.bru' with an OS-agnostic
temp path by using os.tmpdir() and path.join; import/require the os and path
modules if not already present and change the object's pathname property to
path.join(os.tmpdir(), 'req.bru') so the code works on Windows and POSIX
systems.

In `@tests/benchmarks/memory/runner-memory.js`:
- Around line 46-49: The comment describing threshold scaling is inconsistent
with the SCENARIOS array (which uses 100 and 500 iterations)—update the comment
in runner-memory.js to reference the actual iteration counts (100 and 500)
instead of 300, e.g. change "300-run limits (~25 MB at 300 -> 40 MB at 500)" to
something like "100-run limits (~25 MB at 100 -> 40 MB at 500)" so the prose
matches the SCENARIOS definition.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 6f477778-eee4-446e-a7c9-db08b4704578

📥 Commits

Reviewing files that changed from the base of the PR and between 913214e and 338de87.

📒 Files selected for processing (16)
  • .github/actions/tests/run-benchmark-tests/action.yml
  • package.json
  • packages/bruno-js/src/sandbox/quickjs/index.js
  • packages/bruno-js/src/sandbox/quickjs/shims/bru.js
  • packages/bruno-js/src/sandbox/quickjs/shims/bruno-request.js
  • packages/bruno-js/src/sandbox/quickjs/shims/bruno-response.js
  • packages/bruno-js/src/sandbox/quickjs/shims/lib/axios.js
  • packages/bruno-js/src/sandbox/quickjs/shims/lib/crypto-utils.js
  • packages/bruno-js/src/sandbox/quickjs/shims/lib/jwt.js
  • packages/bruno-js/src/sandbox/quickjs/shims/lib/nanoid.js
  • packages/bruno-js/src/sandbox/quickjs/shims/lib/path.js
  • packages/bruno-js/src/sandbox/quickjs/shims/lib/uuid.js
  • packages/bruno-js/src/sandbox/quickjs/shims/test.js
  • packages/bruno-js/src/sandbox/quickjs/utils/index.js
  • scripts/memory-profile.js
  • tests/benchmarks/memory/runner-memory.js

Comment thread scripts/memory-profile.js
Comment thread scripts/memory-profile.js Outdated
Comment thread tests/benchmarks/memory/runner-memory.js Outdated
@abhishekp-bruno abhishekp-bruno force-pushed the fix/excessive-memory-consumption branch from 3c0caba to 729e94d Compare June 9, 2026 12:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant