Repository navigation
fix: [#2264] MutationObserver callback being GC'd due to orphaned WeakRef - #2272
Conversation
|
Comparing this fix to the approach suggested in #2264: This PR: keeps the Suggested in #2264: point the 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 |
|
Thanks for the detailed comparison @marciomazza! The approach suggested in #2264 (pointing the WeakRef at Would you prefer I refactor this PR to use the #2264 approach? Happy to make that change. |
capricorn86
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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.
|
Awesome. That allows me to remove my local patch. Thanks to you both. |
With this issue solved, the patch is not needed anymore: capricorn86/happy-dom#2264 by the PR: capricorn86/happy-dom#2272
…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]>
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
AI assisted: Claude Code