Repository navigation
Stop a GC pause from silently killing MutationObserver in tests - #12438
Conversation
happy-dom holds each observer's internal callback in a WeakRef, so a GC pause under sharded CI load permanently kills a still-connected observer. That is what made the activity portal readiness test flake: once the observer died, no amount of re-applying ready DOM could release the latch. Co-authored-by: Orca <[email protected]>
|
Warning Review limit reached
Next review available in: 18 minutes You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. How can I continue?After more reviews become available, a review can be triggered using the 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 configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (3)
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. Comment |
…tablyai#12438) happy-dom holds each observer's internal callback in a WeakRef, so a GC pause under sharded CI load permanently kills a still-connected observer. That is what made the activity portal readiness test flake: once the observer died, no amount of re-applying ready DOM could release the latch. Co-authored-by: Orca <[email protected]>
Summary
activity-portal-readiness-loop.react185.test.tsxfails intermittently across unrelated PRs withAssertionError: expected 'unavailable' to be 'ready'. It is not a product bug and it is not that test's fault.happy-dom 20.9.0 stores each
MutationObserver's internal listener callback in aWeakRef(mutation-observer/MutationObserverListener.js:callback: new WeakRef((record) => this.report(record))), read back viamutationListener.callback.deref(). That arrow closure has no strong owner, so any GC pause can collect it — and the still-connected observer then stops delivering records permanently. Real browsers keep the callback alive for the observer's lifetime.This adds a vitest setup module that pins happy-dom's internal listener callback in a
WeakMapkeyed by the observer, so it lives exactly as long as the observer does — browser semantics. It no-ops whenMutationObserveris undefined (the defaultnodeenvironment) and is idempotent via aSymbol.formarker on the prototype.ELI5
A test asks the browser "tell me when this part of the page changes." happy-dom writes that request down in pencil that the garbage collector is allowed to erase at any moment. Usually nobody erases it and the test passes. Occasionally, under load, it gets erased — and then the browser silently stops answering forever, so the test waits for a change that will never be reported and fails. This writes the request down in pen instead, which is what a real browser does.
Fix proof
Reproduced deterministically by forcing
gc()just before the assertion (scratch copy of the failing test,NODE_OPTIONS=--expose-gc):The new
happy-dom-mutation-observer-retention.test.tsasserts the retained callback is the exact object happy-dom holds weakly, that it is released ondisconnect(), and that records keep being delivered after GC.No regressions
Purely additive: 3 files, 155 insertions, 0 deletions. No production code is touched — the module lives under
config/scripts/and is wired in only viatest.setupFiles. No existing test file is modified.Trade-off: this monkeypatches
MutationObserver.prototype.observe/disconnectfor every test file that runs under happy-dom (423 of 4,021). That breadth is deliberate — the defect is in the environment, not in any one test — but it is the reason this is split out rather than bundled into an unrelated fix.Cross-platform
Test-environment only; no paths, shells, spawns, or platform-conditional code. Identical on macOS, Linux, and Windows.
Performance
One
WeakMapset perobserve()and one delete perdisconnect(). No polling, no timers, no retained DOM.Security
None. No IPC, command execution, path handling, or auth surface.
Notes
Split out of #12212 on review feedback. It was written there to green that PR's shard, but it is unrelated to #11935 and belongs on its own:
RestartPreventExitStatus=3deliberately leaves a unitfailedrather than retrying). If that PR is ever reverted, the revert would silently reintroduce a repo-wide test flake.Observed on at least #12209 and #12212 (unrelated branches, same shard); a search suggests ~7 PRs have hit it. The existing hand-written workarounds in
activity-portal-readiness-loop.react185.test.tsx(e.g. "A fixed 9-flip budget flakes when CI load drops MutationObserver") are a misdiagnosis of this same root cause — the observer does not miss one record, it dies outright. Those comments and their retry loops are now redundant but harmless, and that delicate file is deliberately left untouched here.Made with Orca 🐋