Skip to content

Stop a GC pause from silently killing MutationObserver in tests - #12438

Merged
nwparker merged 1 commit into
mainfrom
nwparker/happy-dom-mutation-observer-retention
Aug 4, 2026
Merged

nwparker merged 1 commit into
mainfrom
nwparker/happy-dom-mutation-observer-retention

Conversation

@nwparker

@nwparker nwparker commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary

activity-portal-readiness-loop.react185.test.tsx fails intermittently across unrelated PRs with AssertionError: 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 a WeakRef (mutation-observer/MutationObserverListener.js: callback: new WeakRef((record) => this.report(record))), read back via mutationListener.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 WeakMap keyed by the observer, so it lives exactly as long as the observer does — browser semantics. It no-ops when MutationObserver is undefined (the default node environment) and is idempotent via a Symbol.for marker 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):

# WITHOUT this setup file
FAIL  Activity portal pane switching > releases a latched readiness once the terminal attaches
AssertionError: expected 'unavailable' to be 'ready'

# WITH this setup file
Test Files  1 passed (1)

The new happy-dom-mutation-observer-retention.test.ts asserts the retained callback is the exact object happy-dom holds weakly, that it is released on disconnect(), and that records keep being delivered after GC.

No regressions

npx vitest run --config config/vitest.config.ts \
  config/scripts/happy-dom-mutation-observer-retention.test.ts \
  src/renderer/src/components/activity/activity-portal-readiness-loop.react185.test.tsx

Test Files  2 passed (2)
     Tests  6 passed (6)

Purely additive: 3 files, 155 insertions, 0 deletions. No production code is touched — the module lives under config/scripts/ and is wired in only via test.setupFiles. No existing test file is modified.

Trade-off: this monkeypatches MutationObserver.prototype.observe/disconnect for 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 WeakMap set per observe() and one delete per disconnect(). 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:

  • Revert coupling — Stop a duplicate headless orca serve from crash-looping and exhausting AppImage FUSE mounts #12212 carries a real availability trade-off (RestartPreventExitStatus=3 deliberately leaves a unit failed rather than retrying). If that PR is ever reverted, the revert would silently reintroduce a repo-wide test flake.
  • Blast radius mismatch — a suite-wide prototype patch and a single startup gate want different reviewers.
  • It benefits every open PR, so coupling it to an unrelated review only delays it.
  • Bisect hygiene — this is exactly the kind of change you want isolated in history when a strange DOM failure shows up months later.

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 🐋

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]>
@coderabbitai

coderabbitai Bot commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

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

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 @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: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 68fa3289-ec50-494d-ab51-596cccaca81f

📥 Commits

Reviewing files that changed from the base of the PR and between dbbeca0 and eea28f8.

📒 Files selected for processing (3)
  • config/scripts/happy-dom-mutation-observer-retention.test.ts
  • config/scripts/happy-dom-mutation-observer-retention.ts
  • config/vitest.config.ts

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.

❤️ Share

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

@nwparker
nwparker merged commit e8d4818 into main Aug 4, 2026
43 checks passed
@nwparker
nwparker deleted the nwparker/happy-dom-mutation-observer-retention branch August 4, 2026 06:18
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…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]>
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.

1 participant