Skip to content

[lexical-headless] Bug Fix: Use window.happyDOM.close() to ensure proper cleanup#8274

Merged
etrepum merged 2 commits intofacebook:mainfrom
etrepum:happydom-close
Apr 1, 2026
Merged

[lexical-headless] Bug Fix: Use window.happyDOM.close() to ensure proper cleanup#8274
etrepum merged 2 commits intofacebook:mainfrom
etrepum:happydom-close

Conversation

@etrepum
Copy link
Copy Markdown
Collaborator

@etrepum etrepum commented Apr 1, 2026

Description

HappyDOM's Window API uses a strange convention for proper cleanup that the original withDOM implementation missed. Had Claude do the research to track this one down.

Closes #8272

Claude's research

That's the leak. Line 16:

private static [PropertySymbol.browserFrames]: Map<number, IBrowserFrame> = new Map();

This is a static Map on the WindowBrowserContext class — a GC root. Every window created gets added to it at line 105:

browserFrames.set(window[PropertySymbol.internalId], browserFrame);

The only place it gets removed is line 115, inside removeWindowBrowserFrameRelation, which is only called from BrowserWindow[PropertySymbol.destroy](). And destroy() is only called from BrowserFrameFactory.destroyFrame(), which is only reached via happyDOM.close().

So the reference chain that prevents GC is:

WindowBrowserContext (static class, GC root)
  └─ browserFrames Map
       └─ IBrowserFrame (the frame)
            └─ .window → BrowserWindow (the window)
            └─ .page → DetachedBrowserPage
                 └─ .context → DetachedBrowserContext
                      └─ .browser → DetachedBrowser

Every Window ever created stays in this static Map forever unless happyDOM.close() is called. window.close() is a no-op for directly-constructed windows (it only works for window.open() popups), so it never triggers the cleanup.

Test plan

I think it'd be fragile to write tests for this happy-dom implementation detail 🤷 I've also submitted a PR to fix the upstream root cause capricorn86/happy-dom#2121 - if that lands upstream this fix would be unobservable since GC would work as-is even with calling the no-op close

@vercel
Copy link
Copy Markdown

vercel bot commented Apr 1, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
lexical Ready Ready Preview, Comment Apr 1, 2026 4:50am
lexical-playground Ready Ready Preview, Comment Apr 1, 2026 4:50am

Request Review

@meta-cla meta-cla bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Apr 1, 2026
@etrepum
Copy link
Copy Markdown
Collaborator Author

etrepum commented Apr 1, 2026

Note that the RSS will go up for a while especially if you create a lot of garbage quickly, but it should eventually plateau. That's just how GC languages work (at least when the object graphs are non-trivial).

@etrepum etrepum added this pull request to the merge queue Apr 1, 2026
Merged via the queue into facebook:main with commit c7a0c30 Apr 1, 2026
40 checks passed
@etrepum etrepum mentioned this pull request Apr 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. extended-tests Run extended e2e tests on a PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Memory leak in withDOM

2 participants