Repository navigation
fix(resize-observer): measure the first observation from a real layout - #2810
lanyue-llk wants to merge 4 commits into
Conversation
lanyue-llk
left a comment
There was a problem hiding this comment.
Independent review of 1e1c460
Changes needed (1)
- A child-frame target evicts the page's layout and turns later reads into republishes.
moli-renderer-v8/src/native_bridge/context_host/layout.rs:287-312(newensure_initial_layout_for_document), called fromcontext_bootstrap/resize_observer_runtime.rs:395-396.- The layout cache holds one recursive snapshot (
layout_snapshot.rs:10-41,LatestLayoutTreeCache,publishreplaceslatest).latest_layout(document)matches only when the slot's own document isdocument. A child frame's Document is never the slot document, even when the page's recursive tree already contains that frame, which is howwith_latest_layout_tree_for_document(layout.rs:~527, viaget_for_root) answers it on main today. - Failure scenario:
r2-c/ro/www/childrealm.htmlunder--layout. The parent observer runs, so the page layout is published. The child observer then observesw.document.getElementById('target').latest_layout(child_doc)isNone, so the code builds a child-only pass and publishes it underchild_doc, which drops the page snapshot. The next main-document read (getBoundingClientRect,offsetHeight, a click) callsensure_initial_layout(), sees no main snapshot and runs a fresh full layout. That read sees refreshed geometry, which is an implicit republish and breaks the #1080 on-demand contract and the body's claim that "an existing snapshot is reused, never refreshed". - Cost:
queue_mutation_recordscallsqueue_resize_observer_checkson every DOM mutation (observer_runtime/dom_access.rs:154). With observed targets in both documents, every mutation-driven flush doesensure(main), which rebuilds the page, thenensure(child), which rebuilds the child. That is two full layout passes per mutation batch, instead of the "at most one layout pass per Document". Before this PR, main measured that child target from the existing recursive snapshot with no pass at all. - This is the case the existing doc comment rules out: "Missing nodes or newly navigated frames in an existing tree must not refresh the whole page" (
layout.rs:279-280). - Smallest fix: drop the new function and call the page's first-demand layout, i.e. use
observable_geometry_batch(ensure_initial_layout()+published_geometry_batch,geometry/provider.rs:53-60) inbuild_resize_observer_entries. The recursive page tree already covers same-process frames. A frame that is missing from it hitsNoLayoutSnapshot, which this PR already skips without recording a size. Add oneOnDemandtest with an observer on an iframe target after a main snapshot exists: assert the real size and that the page snapshot or layout pass count is unchanged.
- The layout cache holds one recursive snapshot (
Not blocking, but outside this PR's scope:
- A target inserted after an earlier snapshot (
t1593/www/ro5.html: an earlygetBoundingClientRect, then a newvpis observed) is absent from the frozen tree, so it is still answered and recorded as 0x0. Chrome reportsro=400x200. Under #1080 this needs #1823 or #2795, but it is the same visible symptom as #2759 in a common ordering. The body could say so.
Checks:
- Main-document path: verified only once per Document and only when an observer has pending element targets. Pages without observers never reach it.
- New test: fails on main (
NoLayoutSnapshot, then a default 0x0). - CI: Format, Node, Python and Windows pass. Nextest, Clippy, CDP smoke and Build release are still pending.
- Picture: pending (body has only the
<!-- picture -->placeholder).
lanyue-llk
left a comment
There was a problem hiding this comment.
Independent review of 78f05c8
Changes needed (2)
The round-1 regression is fixed. The observation now goes through observable_geometry_batch → ensure_initial_layout() (native_bridge/context_host/layout.rs:281). That publishes only the main document's recursive tree, and only when no tree exists yet, so it neither republishes nor runs an extra pass for child frames. ondemand_ro_measures_child_frame_targets_from_the_page_snapshot guards that. resize_observer_measures_real_layout_before_any_snapshot_exists fails on main, where it gets 0x0. The fixture's publishLayout hook already exists on main: it is installed by the Rust test through vm.eval (script_vm/tests/resize_observer_entries.rs:16-20) and is already used at fixture line 190. It is test-only and not visible to pages or CDP.
-
The skip branch changes late-frame targets from a fake 0x0 to no callback at all. The issue does not need this change, and the Limit section does not disclose it. (
context_bootstrap/resize_observer_runtime.rs:432-436)
Afterensure_initial_layout(), the main document always has a tree. SoNoLayoutSnapshot→continuecan now only happen for a document that is missing from the existing frozen tree, i.e. an iframe created or navigated after the snapshot. That is exactly the case the fixture now has to work around withpublishLayout.
Scenario: under--layout, the page callsgetBoundingClientRect(), then inserts an iframe and observes an element inside it.- main: the callback fires with 0x0.
- this branch: the callback never fires.
queue_resize_observer_checksre-queues the target on every DOM mutation (observer_runtime/dom_access.rs:154), but the on-demand tree stays frozen, so each attempt returnsNoLayoutSnapshotagain.
The body says the skip exists "so a later observation can still deliver the real one". Under on-demand that only happens if something else republishes. Recording 0x0 would allow that later delivery too, because any real size differs from 0x0.
This also makes late targets inconsistent. A late same-document target gets 0x0 (the disclosedro5limit), while a late child-frame target gets nothing.
Smallest fix: drop the skip and the fixture change. Late-frame targets then behave like main and like the disclosedro5case, and the original fix is unaffected (ro2never reaches this branch). If you keep the skip on purpose, state in the Limit section that, under on-demand layout, observed targets in frames created after the snapshot now get no callback at all, and fix the "later observation can still deliver" wording.
-
The picture is stale. It still shows the round-1 design:
ensure_initial_layout_for_document(document), "one layout pass per Document" and "sample published_geometry_batch". Its Cost numbers also differ from the body table: 94.2 MB vs 93.8 MB, 0.14 s / 102.3 MB vs 0.15 s / 102.1 MB, and the article rows show 0.08–0.09 s / 88.7 MB vs 0.10 s / 88.5 MB.
Fix: regenerate the picture from the current flow (observable_geometry_batch→ensure_initial_layout→ page snapshot, child frames answered from the same tree) and the current Cost rows.
CI at review time: Format, Node and Python harness and Windows storage pass. Nextest, Clippy, release builds and CDP smoke are still pending, so CI gives no verdict yet.
lanyue-llk
left a comment
There was a problem hiding this comment.
Independent review of e162296
No blocking findings
Round-2 follow-ups verified:
- The skip branch is gone.
build_resize_observer_entries(context_bootstrap/resize_observer_runtime.rs:393-402) now differs from main only in callingobservable_geometry_batch. It keeps main'sNoLayoutSnapshot => continue, which falls through to the recorded 0x0, so a target in a frame created after the snapshot behaves as it does on main (callback with 0x0). It no longer gets "no callback ever". - The fixture workaround is reverted: the diff touches no fixture, and
publishLayout/resize-observer-entry-webidl.jsmatch main. - The Limit section now covers both late orderings, same-document (
ro5) and later-created frames, and correctly defers them to #1823/#2795. - Round-1 still holds:
ensure_initial_layout()(native_bridge/context_host/layout.rs:281-306) publishes only the main document's recursive tree, and only when none exists. Child-frame targets are answered from that tree, guarded byondemand_ro_measures_child_frame_targets_from_the_page_snapshot.
Whole diff: the change is the smallest one that reaches the root cause. It reuses the existing first-demand path that getBoundingClientRect already uses, and adds no new flag, budget or protocol surface. Under mock layout, ensure_initial_layout returns early, so nothing changes. The new work runs only when an observation is due and no snapshot exists. resize_observer_measures_real_layout_before_any_snapshot_exists fails on main, where NoLayoutSnapshot leads to 0x0, and its expectation matches Chrome. The Cost numbers are plausible: one extra layout pass, only on observer pages.
Optional notes:
ensure_initial_layoutcan now surfaceNoLayoutRootfrom the observer flush (layout.rs:303). This happens on a page that has no published layout and nodocumentElement, for example afterdocumentElement.remove()with a detached element observed. The flush then throws "ResizeObserver layout failed" and skips the callback, where main and Chrome deliver 0x0. This is rare. If you want to close it, matchNoLayoutRoottogether withNoLayoutSnapshot => continueatresize_observer_runtime.rs:400.published_geometry_batchis now private, andobservable_geometry_batchis its only caller. Its doc comment "Style sampling never initializes layout as a side effect" (geometry/provider.rs:62) no longer describes any caller. Inline it or drop the comment.
Picture: still stale. It shows ensure_initial_layout_for_document, the removed "skip, record no size" branch, "one layout pass per Document" and the older Cost numbers (94.2 MB, 0.14 s / 102.3 MB, 88.7 MB), none of which match the current body.
CI at review time: Format, Node harness and Resolve revisions pass. Nextest, Clippy, Python, Windows, CDP smoke and both release builds are still pending, so CI gives no verdict yet.
Sequential Navigation Soak A/B✅ HEAD completed the 200-navigation resilience and memory observation. One browser process, one target, and one CDP session navigate CSDN → SegmentFault → Huaban → example.com repeatedly. This run issued 200 Common ancestor Session resilience
A public-page failure is reported separately from an unrecoverable session. The soak requires all navigation attempts, zero failed recovery, zero lifecycle/network ordering violations, and complete resource evidence. Process-tree memory
HEAD memory by 50-navigation quarter
Raw reports and per-navigation boundary samples: workflow run and full Public-site timing and memory are observational. Use the A/B deltas and trend shape as evidence, not as a deterministic performance threshold. |
Spider Bench A/B✅ All benchmark browser service runs completed; results are informational. Public HEAD: 189 / 240 rows; 39 / 48 sites produced rows; 9 unexpected empty sites. ✅ Deterministic fixture contract is clean. Common ancestor Public 48-site run · informational
Public-site content, timing, and memory are noisy. This single A/B run reports evidence only; site outcome counts explain missing rows without treating them as a deterministic regression. CPU and memory timelinesThese are bounded 40-point views of the same complete process-tree samples used by the HTML report. CPU uses 100% per occupied logical core; memory lines are RSS first and PSS second when complete PSS samples are available. Basexychart-beta
title "Base CPU"
x-axis "Elapsed seconds" [2.038, 3.542, 5.544, 7.045, 9.048, 11.05, 12.553, 14.556, 16.059, 18.06, 19.563, 21.563, 23.568, 24.068, 25.069, 27.072, 28.573, 30.574, 32.576, 34.078, 35.079, 36.081, 37.582, 39.585, 41.086, 43.088, 45.09, 46.593, 48.595, 50.095, 52.096, 54.098, 55.6, 57.604, 59.105, 61.107, 62.609, 63.61]
y-axis "CPU percent" 0 --> 200
line [105.94, 111.69, 117.97, 111.85, 125.74, 49.89, 105.87, 41.99, 7.99, 2, 19.99, 58, 137.69, 0, 3.99, 2, 4, 56.04, 131.87, 31.92, 107.83, 101.84, 103.91, 91.8, 91.89, 67.97, 39.96, 107.77, 27.98, 49.99, 49.91, 0, 39.94, 5.99, 83.91, 147.89, 93.91, 151.76]
xychart-beta
title "Base memory: RSS then PSS"
x-axis "Elapsed seconds" [0.037, 2.038, 3.542, 5.544, 7.045, 9.048, 11.05, 12.553, 14.556, 16.059, 18.06, 19.563, 21.563, 23.568, 24.068, 25.069, 27.072, 28.573, 30.574, 32.576, 34.078, 35.079, 36.081, 37.582, 39.585, 41.086, 43.088, 45.09, 46.593, 48.595, 50.095, 52.096, 54.098, 55.6, 57.604, 59.105, 61.107, 62.609, 63.61, 64.342]
y-axis "Memory MiB" 0 --> 500
line [13.98, 110.69, 126.18, 137.18, 164.49, 153.94, 146.97, 127.93, 134.62, 164.42, 149.62, 148.1, 131.57, 150.75, 150.87, 131.68, 125.94, 125.94, 122.09, 137.54, 147.05, 239.67, 185.32, 133.89, 155.3, 154.03, 132.89, 155.76, 232.48, 222.25, 233.47, 233.37, 229.07, 229.85, 133.41, 146.12, 148.28, 142.68, 167.59, 0]
line [10.39, 106.91, 122.43, 133.48, 160.7, 150.23, 143.2, 124.38, 130.83, 160.63, 145.83, 144.32, 127.77, 146.95, 147.07, 127.88, 122.15, 122.15, 118.34, 133.85, 143.28, 235.53, 181.53, 130.15, 151.59, 150.29, 129.14, 152.6, 228.38, 218.5, 229.72, 229.63, 225.33, 225.79, 129.67, 142.5, 144.39, 138.86, 163.79, 0]
HEADxychart-beta
title "HEAD CPU"
x-axis "Elapsed seconds" [1.038, 1.539, 2.539, 4.04, 5.543, 6.545, 7.546, 8.047, 9.55, 10.552, 12.054, 13.556, 14.557, 16.059, 17.56, 18.562, 20.065, 21.568, 22.57, 24.071, 25.573, 26.574, 28.075, 29.576, 30.577, 32.079, 33.58, 34.581, 35.583, 36.084, 37.584, 38.585, 40.087, 41.589, 42.59, 44.092, 45.595, 46.596]
y-axis "CPU percent" 0 --> 200
line [3.99, 107.77, 116.03, 97.83, 131.69, 107.77, 151.68, 123.74, 95.9, 133.92, 6, 127.87, 121.95, 45.98, 71.95, 119.76, 107.73, 103.76, 113.7, 137.92, 113.83, 101.87, 64.01, 65.89, 129.96, 137.9, 2, 37.96, 0, 25.97, 0, 2, 31.97, 7.99, 37.99, 138.01, 53.85, 139.84]
xychart-beta
title "HEAD memory: RSS then PSS"
x-axis "Elapsed seconds" [0.036, 1.038, 1.539, 2.539, 4.04, 5.543, 6.545, 7.546, 8.047, 9.55, 10.552, 12.054, 13.556, 14.557, 16.059, 17.56, 18.562, 20.065, 21.568, 22.57, 24.071, 25.573, 26.574, 28.075, 29.576, 30.577, 32.079, 33.58, 34.581, 35.583, 36.084, 37.584, 38.585, 40.087, 41.589, 42.59, 44.092, 45.595, 46.596, 47.808]
y-axis "Memory MiB" 0 --> 500
line [13.83, 66.65, 96.69, 113.75, 144.33, 131.03, 150.54, 133.82, 151.65, 137.49, 138.88, 121.88, 156.56, 156.88, 132.78, 147.27, 155.12, 250.92, 195.96, 184.61, 144.95, 152.59, 140.86, 147.86, 146.66, 219.18, 225.74, 225.88, 235.84, 232.74, 231.7, 233.68, 232.81, 230.11, 154.57, 148.78, 147.94, 150.93, 175.96, 0]
line [10.35, 63, 93.14, 110.08, 140.75, 127.38, 145.2, 130.09, 147.48, 133.77, 135.15, 118.15, 152.86, 153.43, 129.02, 142.53, 151.38, 246.63, 192.24, 173.16, 139.56, 149.42, 137.08, 144.1, 142.9, 215.51, 222.01, 222.16, 232.12, 229.02, 227.98, 229.93, 229.06, 226.36, 150.82, 145.07, 143.25, 147.18, 172.38, 0]
Deterministic fixture · required contractThe fixture exercises 15 routes: 8 are expected to emit rows and 7 intentionally exercise empty, loading, or timeout behavior.
Full HTML, JSON, CSV, logs, and page snapshots: workflow run and full The public-web A/B is informational. The exact HEAD fixture contract runs as a separate required CI check; timing and public-site content deltas remain non-blocking. |
CI Regression ReportSource CI run · source state at render:
Release regression — ✅ HEAD/base failures 0/0; raw binary 0.00%Package and image size
Startup latency and PSS
Concurrency matrix
Frontend differential — ✅ 1,020/1,020 cases matched; 0 issues
Agent episodes · Moli vs Chromium — ✅ 8/8 Moli episodes passed; 0 failures
Runtime and CDP session contracts — ✅ 26 contract cases; 0 failures
CDP smoke — ✅ 120/120 groups passed; 1,798 scenariosWorkers: 4 · cumulative group time: 398.74 s · failed groups: 0
WebMainBench · 545 pages — ✅ 545/545 completed; 0 unexpected failures
Frozen HTML · Linux · Diagnostics: All artifact fields are parsed by the trusted default-branch renderer; missing or invalid inputs remain visible as unavailable. |
3397793 to
c7fd35f
Compare
c7fd35f to
14bb8f7
Compare

**Track:** Web Fetch (Browser Eval). Replaces part of #1593.Closes #2759.
Replaces #1593.
Problem. Under
--layout, a ResizeObserver whose target was observed before any layout existed reported a 0×0 size. moli also stored 0×0 as the last reported size, so it never delivered the real one, and pages that render from the measurement (virtualized grids, auto-sizers) came back empty. Repro:moli fetch --layout http://127.0.0.1:8000/ro2.html --delay-ms 500 --dump htmlprintedEMPTY h=0; Chrome rendersROW-0 (h=200)…ROW-4 (h=200).Reproduce (steps from #2759)
ro2.html, served withpython3 -m http.server 8000:A smaller variant: observe a
<div style="width:300px;height:120px">from an inline script and writecontentRectinto a<pre>.Expected (Chrome 156 headless via CDP)
ro2:ROW-0 (h=200)…ROW-4 (h=200)calls:300x120Actual
ro2:<div>EMPTY h=0</div>calls:0x0The result is the same over
moli serve --layout+Runtime.evaluate. On that page,getBoundingClientRect()returns 400×200, and a new observer created later fromRuntime.evaluatereports[[400,200]]. The page's own observer is never delivered again.Change. In
context_bootstrap/resize_observer_runtime.rs, the observation sampledpublished_geometry_batch, which only reads an existing snapshot. When the page had no snapshot yet (NoLayoutSnapshot), the target fell back toLayoutResizeObserverMetrics::default(), and that 0×0 was recorded.observable_geometry_batch. That isensure_initial_layout()followed by the published batch, the same first-demand pathgetBoundingClientRectuses. The page's first layout is therefore published if none exists.published_geometry_batchhas no other caller, so it becomes private togeometry/provider.rs.Limit: under on-demand layout, a target added after the snapshot was published is absent from the frozen tree and still reports 0×0 until something republishes. This applies to a target added later in the same Document (
ro5.htmlin the #1593 triage) and to a target in a frame created later. That ordering needs live layout (#1823) or the #2795 work.Verification.
ro2.html --layout --delay-ms 500EMPTY h=0ROW-0 (h=200)…ROW-4 (h=200)--layoutcalls:0x0calls:300x120--layout(mock geometry)EMPTY h=20,calls:100x20resize_observer_measures_real_layout_before_any_snapshot_exists(OnDemand, no prior snapshot →300x120);ondemand_ro_measures_child_frame_targets_from_the_page_snapshot(page snapshot published, observer on an iframe target →80, and the layout pass count is unchanged).cargo nextest run -p moli-renderer-v8 -E 'test(resize_observer) | test(observer_callbacks) | test(ondemand_ro) | test(real_ro)': 41 passed.cargo fmt --allclean;cargo clippy -p moli-renderer-v8 --all-targets --all-features -- -D warningsclean (workspace clippy not run locally).Cost. Pages without a ResizeObserver never reach this code. A page with observers pays at most one layout pass, and only when the page has no published layout yet and an observation is due. Later observations, including child-frame targets, reuse the published snapshot. The debug builds compared are main cbf4d01 and this branch (on main e7ac1c1), same profile,
/usr/bin/time -l, alternating runs, median of 7:--layout --delay-ms 500--layout--layout(no observer)The observer pages cost more by design: main never laid them out and reported a fake 0×0, while the branch runs the one layout pass needed for a real size.