Skip to content

test: make sourcetextmodule-leak actually detect leaks and run 5x faster - #35917

Closed
robobun wants to merge 1 commit into
mainfrom
farm/20cf650b/sourcetextmodule-leak-faster
Closed

robobun wants to merge 1 commit into
mainfrom
farm/20cf650b/sourcetextmodule-leak-faster

Conversation

@robobun

@robobun robobun commented Jul 26, 2026 •

Copy link
Copy Markdown
Collaborator

What does this PR do?

test/js/node/vm/sourcetextmodule-leak.test.ts takes ~23 s on the linux x64-asan lane and ~8 s everywhere else, and in its current form cannot detect the leak it was written for.

The existing test never fails on a leak. With every one of the 50 000 modules artificially retained, release RSS lands at ~2738 MB versus ~2672 MB with nothing retained; both sit under the 3000 MB threshold. The loop never GCs, so the allocator's high-water mark dominates and retained vs freed pages end up in the same resident set. The debug/ASAN branch is the same story (130 MB retained vs 76 MB freed, threshold 1500 MB).

This change:

  • forces a full GC after every batch so no-leak RSS is bounded to roughly one batch of outstanding work (~120 MB release, ~30 MB debug) instead of tracking total allocations,
  • cuts iterations 50 000 → 8 000 (release) / 2 000 → 400 (debug) now that the per-iteration leak signal (~55 KB) is visible,
  • adds an exact NodeVMSourceTextModule object-count assertion via expectMaxObjectTypeCount, which is immune to ASAN's ~256 MB quarantine that otherwise masks RSS deltas on sanitized lanes,
  • asserts the last module's status and an exported namespace.result so the loop is verified to have linked and evaluated, not just allocated,
  • tightens the RSS threshold from 3000/3500 MB to 256/600 MB.

How did you verify your code works?

Timed locally, five runs each:

build before after
release 9.8 s 1.7–2.0 s
debug+ASAN (bun bd) 11.5 s 2.8–3.2 s

Leak-sensitivity check: a copy of each variant with leaked.push(mod) injected into the loop.

build old test new test
release passes (2738 MB < 3000 MB) fails (447.62 MB ≥ 256 MB)
debug+ASAN passes (130 MB < 1500 MB) fails (400 NodeVMSourceTextModule > 20)

#33704 also reduces this file's release iteration count (50k → 20k) as part of a broader sweep; this change supersedes that hunk with a redesign that fixes the detection gap as well as the runtime.


[stamp-90s] gate passed · iteration 0 · 1 files touched

passes on PR (with fix)
Test-only change.

Debug/ASAN (expected pass):
$ bun bd test 'test/js/node/vm/sourcetextmodule-leak.test.ts'
$ BUN_DEBUG_QUIET_LOGS=1 bun scripts/build.ts --profile=debug --quiet test test/js/node/vm/sourcetextmodule-leak.test.ts
bun test v1.4.0 (3a5993309)

test/js/node/vm/sourcetextmodule-leak.test.ts:
(pass) vm.SourceTextModule > shouldn't leak memory [3133.28ms]

 1 pass
 0 fail
 3 expect() calls
Ran 1 test across 1 file. [5.73s]
Exit: 0
diff hotspot
test/js/node/vm/sourcetextmodule-leak.test.ts | 38 +++++++++++++++++++++------
 1 file changed, 30 insertions(+), 8 deletions(-)

gate history · 1 passed · 0 rejected · iteration 0

evidence per changed file
file                                           reads  edits  tests
test/js/node/vm/sourcetextmodule-leak.test.ts      1      1      0

self-review · no surviving concerns

32 concerns were raised and did not survive verification.

The existing test could not detect its target leak: with every module retained,
release RSS landed at ~2738 MB vs ~2672 MB no-leak, both under the 3000 MB
threshold. Without periodic GC the allocator high-water mark dominates and
retained vs freed pages are indistinguishable.

Force a full GC after every batch so no-leak RSS stays bounded (~120 MB
release), cut iterations 50k -> 8k (2k -> 400 debug), add an exact
NodeVMSourceTextModule object-count assertion that also holds under ASAN
quarantine, and assert the last module actually evaluated.
@coderabbitai

coderabbitai Bot commented Jul 26, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

@robobun, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 15 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: bc425e75-fd93-476c-9d18-fae837cbf24d

📥 Commits

Reviewing files that changed from the base of the PR and between 44f6469 and 3a59933.

📒 Files selected for processing (1)
  • test/js/node/vm/sourcetextmodule-leak.test.ts

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

@robobun

robobun commented Jul 26, 2026 •

Copy link
Copy Markdown
Collaborator Author
Updated 8:20 AM PT - Jul 26th, 2026

❌ @robobun, your commit 3a59933 has 1 failures in Build #82350 (All Failures):

  • 📦 Binary size — 12 over 0.50 MB
  • targetthis build canary: main #79916
    sizeΔ
    ❌ bun-darwin-aarch6458.13 MB57.58 MB+564.9 KB
    ❌ bun-darwin-x6463.48 MB62.95 MB+544.5 KB
    ❌ bun-linux-aarch6470.98 MB70.42 MB+576.0 KB
    ❌ bun-linux-x6472.47 MB71.95 MB+528.0 KB
    ❌ bun-linux-aarch64-musl64.88 MB64.32 MB+576.0 KB
    ❌ bun-linux-x64-musl66.98 MB66.45 MB+544.0 KB
    ❌ bun-linux-aarch64-android78.47 MB77.97 MB+512.0 KB
    ❌ bun-linux-x64-android80.62 MB80.10 MB+529.2 KB
    ❌ bun-freebsd-x6483.07 MB82.56 MB+528.0 KB
    ❌ bun-freebsd-aarch6484.84 MB84.31 MB+544.0 KB
    ❌ bun-windows-x6480.26 MB79.70 MB+570.5 KB
    ❌ bun-windows-aarch6470.86 MB70.34 MB+533.0 KB

    Add [skip size check] to the commit message if this increase is intentional.


🧪   To try this PR locally:

bunx bun-pr 35917

That installs a local version of the PR into your bun-35917 executable, so you can run:

bun-35917 --bun

@claude claude 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.

LGTM — test-only change that turns a vacuous leak test into one that actually fails on regression, and cuts runtime ~5×.

What was reviewed:

  • expectMaxObjectTypeCount signature/semantics in test/harness.ts and that NodeVMSourceTextModule is a real IsoSubspace-backed type name that appears in heapStats().objectTypeCounts.
  • last is cleared before the final GC/object-count check so the retained reference doesn't self-fail the assertion; Buffer.alloc(10, 'hello').toString() does yield "hellohello".
  • RSS threshold branches on isASAN and the object-count check is the authoritative signal, per the repo's leak-test guidance; timeouts (15s/30s) remain well above the measured 2s/3s runtimes.
Extended reasoning...

Overview

Single-file, test-only change to test/js/node/vm/sourcetextmodule-leak.test.ts. The rewrite adds per-batch Bun.gc(true), drops iteration counts (50k→8k release, 2k→400 debug), tightens the RSS threshold (3000/3500 MB → 256/600 MB), adds an exact NodeVMSourceTextModule object-count assertion via the existing expectMaxObjectTypeCount harness helper, and asserts last.status/last.namespace.result so the loop is proven to have linked+evaluated rather than just allocated.

Security risks

None. No production code, no external I/O, no new dependencies. Pure in-process test logic.

Level of scrutiny

Low-to-medium. This is a leak regression test being tightened, not loosened — the PR description empirically shows the old thresholds were vacuous (a forced leak still passed at 2738 MB < 3000 MB) and the new test fails on the same forced leak. The change follows the repo's stated leak-test conventions exactly: Bun.gc(true) + heapStats objectTypeCounts as the authoritative signal, RSS threshold branched on isASAN, bound well below what the unfixed leak produces. The object-count check is the pattern already used in sourcetextmodule-link-gc.test.ts and vm-script-fetcher-leak.test.ts for the same class.

Other factors

I verified expectMaxObjectTypeCount in test/harness.ts:297 polls with GC and uses toBeLessThanOrEqual, so baseline + 20 is a ceiling, and confirmed NodeVMSourceTextModule has its own IsoSubspace (DOMIsoSubspaces.h) so it shows up under that exact key in objectTypeCounts. The last = undefined before the final GC releases the one intentionally-held reference so it can't trip the ≤20 check. Buffer.alloc(10, 'hello').toString() → "hellohello" matches the assertion. Reduced timeouts (30s→15s release, 60s→30s debug) still leave ~7–10× headroom over the measured runtimes. No prior human reviews or outstanding comments on the thread.

@github-actions

Copy link
Copy Markdown
Contributor

This PR may be a duplicate of:

  1. make more tests faster #33704 - Also modifies sourcetextmodule-leak.test.ts with overlapping changes to ITERATIONS and THRESHOLD_MB constants; test: make sourcetextmodule-leak actually detect leaks and run 5x faster #35917 explicitly supersedes make more tests faster #33704's hunk for this file

🤖 Generated with Claude Code

@robobun

robobun commented Jul 26, 2026 •

Copy link
Copy Markdown
Collaborator Author

CI (build #82350, complete): the test passes on every test lane, including debian-13-x64-asan (the 23 s lane this targets) and all three darwin lanes. The only hard failure is package-binary-size, which is a stale canary comparison on main (same failure on #35918 and #35919; this diff is test-only so the binary is byte-identical to main). Six flaky tests in unrelated areas passed on retry.

Ready for review.

@robobun

robobun commented Sep 6, 2026

Copy link
Copy Markdown
Collaborator Author

Closing: superseded by #41606, which carries the same design on top of the harness rss() helper the file now uses.

@robobun robobun closed this Sep 6, 2026
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