Skip to content

fix: [#2264] MutationObserver callback being GC'd due to orphaned WeakRef - #2272

Merged
capricorn86 merged 1 commit into
capricorn86:masterfrom
mixelburg:fix/mutation-observer-weakref-gc
Aug 7, 2026
Merged

capricorn86 merged 1 commit into
capricorn86:masterfrom
mixelburg:fix/mutation-observer-weakref-gc

Conversation

@mixelburg

Copy link
Copy Markdown
Contributor

What

Fixes #2264 — MutationObserverListener wraps the mutation delivery callback in a WeakRef without a strong reference, allowing V8 to GC the callback at any time. When collected, deref() returns undefined permanently and mutation delivery silently stops.

How

Added a #listenerCallback private field to hold a strong reference to the arrow function. The strong reference is cleared in destroy(). This maintains the original weak semantics between listener and node while preventing premature GC.

Changes

  • Store listener callback as private field alongside WeakRef
  • Clear in destroy()
  • Test: verifies mutations are delivered when observer is externally dereferenced

AI assisted: Claude Code

@mixelburg
mixelburg requested a review from capricorn86 as a code owner August 5, 2026 22:11
@marciomazza

Copy link
Copy Markdown

Comparing this fix to the approach suggested in #2264:

This PR: keeps the WeakRef pointed at the callback closure, and adds a new private field #listenerCallback to hold a separate strong reference to that same closure (cleared in destroy()). Node.ts's dispatch site is unchanged — it still does callback(record) after deref.

Suggested in #2264: point the WeakRef at this (the MutationObserverListener instance) instead of at the closure, and call listener.report(record) at the dispatch site. No extra field, no extra closure — relies on MutationObserver#listeners already keeping the instance alive, the same pattern already used elsewhere in this file.

Both fix the GC bug. The difference is where the strong reference lives: a new field pointing at the closure (this PR) vs. removing the closure and pointing the WeakRef at the already-alive instance (#2264's suggestion).

@mixelburg

Copy link
Copy Markdown
Contributor Author

Thanks for the detailed comparison @marciomazza!

The approach suggested in #2264 (pointing the WeakRef at this and calling listener.report(record)) is indeed cleaner — it removes the extra closure and relies on the already-existing strong reference from MutationObserver#listeners, which is the same pattern already used elsewhere in the file. No extra #listenerCallback field needed.

Would you prefer I refactor this PR to use the #2264 approach? Happy to make that change.

@capricorn86 capricorn86 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thank you for your contribution @mixelburg!

@marciomazza I think I prefer the fix with using the report callback as strong reference, as using a reference to itself as a strong reference opens up for more scenarios.

@capricorn86 capricorn86 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thank you for your contribution @mixelburg!

@marciomazza I think I prefer the fix with using the report callback as strong reference, as using a reference to itself as a strong reference opens up for more scenarios.

@capricorn86
capricorn86 merged commit 576a8bc into capricorn86:master Aug 7, 2026
4 checks passed
@marciomazza

Copy link
Copy Markdown

Awesome. That allows me to remove my local patch. Thanks to you both.

marciomazza added a commit to marciomazza/miniclient that referenced this pull request Aug 8, 2026
With this issue solved, the patch is not needed anymore:
capricorn86/happy-dom#2264
by the PR:
capricorn86/happy-dom#2272
ChristopherChudzicki added a commit to mitodl/mit-learn that referenced this pull request Oct 23, 2026
…s to settle (#4148)

* fix(test): bump happy-dom to 20.14.6 so MutationObservers survive GC

happy-dom 20.3.4 holds each MutationObserver's callback only through a
WeakRef (MutationObserverListener), with no strong reference anywhere.
A garbage collection frees it and Node.reportMutation silently drops the
observer while it is still connected. ProseMirror observes the editor
DOM this way, so text typed after a GC lands in the DOM but never
reaches editor state.

That is the "PATCH never sent" failure of `ArticleEditor autosave › an
article that has never been saved is created once, then updated`: no
edit, no autosave, `Number of calls: 1`. It only shows under memory
pressure (CI runs with coverage and parallel workers), which is why it
appears on unrelated PRs. Forcing one gc() after the first save fails
the test every time on 20.3.4 and passes on 20.14.6.

Fixed upstream in capricorn86/happy-dom#2272, released in 20.11.2. The
`^20.1.0` range already admits it, so the lockfile alone kept 20.3.4.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* fix(test): wait for autosaves to settle, not just to be sent

The `act(...)` failure of `ArticleEditor autosave › an article that has
never been saved is created once, then updated` comes from a save
landing just after a `waitFor` returns. The tests waited for the request
to be recorded; the response settles a moment later, re-rendering the
autosave indicator and Publish. React Testing Library turns the act
environment back on as a `waitFor` returns, so the toolbar's
MutationObserver, reacting to that re-render, sets state outside `act`
and jest-fail-on-console fails the test.

A second `waitFor` for "Saved" does not help: the gap sits between the
two waits (5 of 64 CI-like runs still failed). `waitForSaveToSettle`
checks the request, the "Saved" indicator and an enabled Publish in a
single `waitFor`, so the save has nothing left to land when it returns.
With it, 64 of 64 CI-like runs passed.

It replaces the request-sent waits in the three autosave tests that
ended on one, and the "Saving..." check that could pass before
"Saving..." had rendered.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

MutationObserver callbacks can be silently GC'd (orphaned WeakRef)

3 participants