Skip to content

Free 35 MB per opened 4 MB text document in Chromium, and record why whole-page walks crash its renderer - #6031

Merged
max-sixty merged 3 commits into
mainfrom
grind/tab-crash
Nov 15, 2026
Merged

max-sixty merged 3 commits into
mainfrom
grind/tab-crash

Conversation

@max-sixty

@max-sixty max-sixty commented Nov 15, 2026 •

Copy link
Copy Markdown
Owner

A review of #5996 found that opening all 30 tabs of a page of 30 text documents
of 4 MB, one after another, crashed Chromium's renderer. Each opened tab also left
about 57 MB more in the renderer for the life of the page. The two have separate
causes.

The crash is Chromium's, reached through the harness

Every page.query_selector walks all 2.96 million elements of that page in
Playwright's injected CSS engine. With no tab opened at all, the renderer died
with Target crashed (the browser logged V8 process OOM (ExternalEntityTable::AllocateEntry)) on the 22nd query. A page of 2.96 million
plain rows built by a script, with no Leaf, died the same way: on the 23rd to
32nd Playwright query across runs, or on the 24th to 26th run of a page script's
querySelectorAll("*") loop, installed Chrome 157 included.
With a full GC forced after each walk, 100 script walks passed, and clicking the
30 tabs by position opened them all without a crash. Playwright's Firefox 159
survived both journeys.

notes/browser-support.md records the gap, how a harness on a page this size
finds its controls instead, and the recheck; dev/leaf_dev/browser.py's header
points there.

The growth is partly Leaf's

Each tab open runs reach.js's holdsOwnStop over the opened document's listing,
and tabStops walked every row through children. Chromium keeps the
HTMLCollection that children returns on its element for the element's life: 35
MB of Blink's heap for the 98,000 rows of a 4 MB document. renderedElements now
steps from firstElementChild through nextElementSibling and pushes each element
into one array, so it allocates no collection, and Tab stops over a 96,000-row
listing take 36 to 39 ms in Firefox (54 to 60 through a generator for each
element) and 21 to 23 ms in Chromium. browser/AGENTS.md ("One answer for each
question") now states the sibling-stepping rule for every walk.

Measured in Playwright's Chromium headless shell 158.0.8127.18 on that page,
clicking tabs by position, with a full GC before each reading:

  • tabStops over one closed document's listing: Blink's heap grew 34.8 MB
    before, 0.2 MB after (memory-infra dump).
  • Renderer private footprint over 30 opened tabs: 1357 to 3012 MB before (57 MB a
    tab), 1361 to 1947 MB after (20 MB a tab). Median main-thread busy time per tab
    open: 699 ms before, 562 ms after, at similar machine load.

The stops are unchanged: compared with main's walk from every root a caller hands
tabStops, they matched on 57 pages in Chromium (the corpus, the site and a page
built to hold the walk's edge cases) and 12 in Firefox.

What remains, about 20 MB a tab

Over six tab opens the memory dump grows V8's heap 15 MB a tab, malloc 11 MB and
Blink's heap 3.4 MB. About 10 MB of V8's is passages.js's reading of the opened
document, which its piece cache keeps until the document changes (sampled heap
profile); the rest of V8's and all of Blink's are unattributed. A script that
walks one closed document's 98,000 rows grows malloc by 8 to 13 MB however it
walks them, keeping nothing, so the malloc growth follows script touching the rows;
what holds it is not measured. This change leaves all of it.

Other rendered-tree walks still read children or childNodes:
renderedChildElements in shadow-tree.js (used by keyboard/scopes.js),
readingChildren in passages.js, renderedChildren in shadow.js, and
matching in focus.js. They break the new browser/AGENTS.md row: a walk that
reads children leaves the collection measured above on each element it reaches,
and Chromium keeps a childNodes list on its node too (not measured here). One
shared walk primitive would carry the rule for all of them. The
grind/tab-open-walks change is reworking reach.js and focus.js walks in parallel;
whichever lands second merges main.

Checks

Ran pnpm run test:runtime, pnpm run check:browser, and the browser tests over
Tab stops, lent scroll stops, a scroll box in a widget's shadow tree, a hidden
scroller once shown, and a thread card's place in the Tab order (27 cases). No
test asserts the memory, which is a reading rather than a pass or fail
(tests/AGENTS.md, "Results do not depend on execution speed").

…whole-page walks crash its renderer

A review of #5996 found that opening all 30 tabs of a page of 30 text documents
of 4 MB, one after another, crashed Chromium's renderer. Each opened tab also left
about 57 MB more in the renderer for the life of the page. The two have separate
causes.

The crash is Chromium's, reached through the review's harness. Every
`page.query_selector` walks all 2.96 million elements of that page in
Playwright's injected CSS engine, and with no tab opened at all the renderer died
with `V8 process OOM (ExternalEntityTable::AllocateEntry)` on the 22nd query. A
page of 2.96 million plain rows built by a script, with no Leaf, died the same
way on the 23rd query, or on the 24th run of a page script's
`querySelectorAll("*")` loop (25th in installed Chrome 157). With a full GC
forced after each walk, 40 walks passed, and clicking the 30 tabs by position
opened them all without a crash. Playwright's Firefox 159 survived both
journeys. notes/browser-support.md records the gap and how to recheck it.

The growth is partly Leaf's. Each tab open runs reach.js's `holdsOwnStop` over
the opened document's listing, and `tabStops` walked every row through
`children`. Chromium keeps the HTMLCollection that `children` returns on its
element for the element's life: 35 MB of Blink's heap for the 98,000 rows of a
4 MB document. `renderedElements` now steps from `firstElementChild` through
`nextElementSibling` and allocates no collection.

Measured in Playwright's Chromium headless shell 158.0.8127.18 on that page,
clicking tabs by position, with a full GC before each reading:

- `tabStops` over one closed document's listing: Blink's heap grew 34.8 MB
  before, 0.2 MB after (memory-infra dump).
- Renderer private footprint over 30 opened tabs: 1357 to 3012 MB before (57 MB
  a tab), 1343 to 1894 MB after (19 MB a tab). Median main-thread busy time per
  tab open went from 699 to 600 ms.

The 19 MB a tab that remains is not changed here: passages.js keeps each opened
document's whole reading in its piece cache until the document changes (about
11 MB of JS heap), and the text-node wrappers that reading creates live as long
as their nodes.

Ran `pnpm run test:runtime`, `pnpm run check:browser`, and the browser tests
over Tab stops, lent scroll stops and a thread card's place in the Tab order (17
cases). No test asserts the memory, which is a reading rather than a pass or
fail (tests/AGENTS.md, "Results do not depend on execution speed").
…arness meets the crash

`renderedElements` now pushes each element it reaches into one array, which
`tabStops` filters, rather than yielding through a generator for each element.
Over a listing of 96,000 rows, Tab stops took Firefox 159 54 to 60 ms through the
per-element generators and 36 to 39 ms with the array; Chromium 158 took 27 to 29
ms and 21 to 23 ms. The stops are unchanged: compared with main's walk from every
root a caller hands `tabStops`, they matched on 57 pages in Chromium (the
corpus, the site and a page built to hold the walk's edge cases) and 12 in
Firefox.

The browser note's entry now:

- gives the crash as a harness author sees it, `Target crashed`;
- widens the walks before the crash to what the runs showed: the 24th to 26th
  script walk, and the 22nd to 32nd Playwright selector;
- rechecks with 100 script walks against the same run with a forced full GC
  after each walk, which passed all 100;
- says only that opening the 30 tabs walks each opened document, since the
  runtime walks whole subtrees elsewhere, such as a revision's install;
- names `dev/leaf_dev/browser.py` as its owner, whose header now tells a harness
  on a page of millions of elements to find controls with a native query.

@leaf-agent leaf-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The new comment on renderedElements states a rule that other rendered-tree walks also need, but writes it down only in focus.js (inline).

Comment thread browser/runtime/focus.js Resolved
@max-sixty
max-sixty marked this pull request as ready for review November 15, 2026 09:29
The rule that a rendered-tree walk steps by `firstElementChild` and
`nextElementSibling`, never through `children` or `childNodes`, applies to every
walk, but it was written only beside the Tab-stop walk in focus.js. It is now a
row in browser/AGENTS.md's "One answer for each question", where a walk's author
looks before writing one, and the focus.js header points there, keeping only
its own choice of one array over a generator for each element. The header of
dev/leaf_dev/browser.py points at the browser note's entry rather than
restating it.

No code changes.
@max-sixty
max-sixty merged commit 9d57fe5 into main Nov 15, 2026
5 checks passed
@max-sixty
max-sixty deleted the grind/tab-crash branch November 15, 2026 09:51
max-sixty added a commit that referenced this pull request Nov 15, 2026
…rawn tree by one step (#6067)

A reading of the place in a long text document no longer asks each of
the document's lines whether the browser draws it, each document a
reader opens keeps a reading 5 MB smaller, and the runtime's walks of
the tree as the browser draws it share one step down that keeps no child
lists. Over a 96,000-line document, reading the document's place takes
3.0–4.2 ms in Firefox (5.4–7.1 ms on main) and 4.1–4.4 ms in Chromium
(5.5–5.7 ms).

## What changed

**A reading place skips a document's undrawn lines a run at a time**
(`reading-place.js`, `drawnSegments`). The place reads a text document's
listing as one block, one segment a line. Each reading asked every
segment whether the box skipping it held it, 95,945 `contains` calls a
scroll rest over 96,000 lines, though only the runs near the window are
drawn. Once a segment's parent is undrawn, `lastHeld` finds the last
segment the skipping box holds by doubling a step and then halving it,
since a walk meets what a box holds together: 11,214 calls a rest. A
segment a box draws without holding it, as a slot's assigned text is,
ends that search early and goes on through the usual question, so the
segments yielded are the same.

**One step down the drawn tree** (`shadow-tree.js`, `eachRenderedChild`
and `eachRenderedElement`). Four walks each had their own step from a
node to what it draws, and read `children` or `childNodes`, which
Chromium keeps for the node's life once asked (#6031):
`renderedChildElements` (keyboard scopes), `renderedChildren` in
`shadow.js` (margin pins' cover and the controls inside a widget),
`readingChildren` (place previews) and focus.js's keyed match. Each now
calls the one step, which goes sibling to sibling, as does focus.js's
Tab-stop walk; `renderedChildElements` and `shadow.js`'s
`renderedChildren` are deleted. The trees differ only in which shadow
root a host draws in place of its children, so a walk names it with
`rootOf`: an open root by default, a declared one for the page reading,
a closed stage too for the keyboard, and none for the keyed match, which
reads the item's own tree. Element walks step from element to element:
handed every node, the Tab-stop walk made opening a tab with a 5,000-row
table 22–30 ms slower in Chromium.

The step follows a slot as the browser draws it: what is assigned to it,
a slot forwarded into it among them, or its own children where nothing
is; a slot outside a shadow tree is an ordinary element. Three walks
change at the edges:

- The keyboard walk no longer visits a host's light children that no
slot draws, and visits a slot's fallback only when nothing is assigned
to it. Neither is drawn.
- The margin and Tab-stop walks no longer lose the children of a
`<slot>` outside a shadow tree, for which `assignedNodes({flatten:
true})` answers nothing in Chromium and Firefox (checked with `leaf-dev
probe`).
- `readingChildren` lists a slot forwarded into another as that slot's
child, as `readingParent` climbs, where it listed what the forwarded
slot is assigned. This is the one place a place preview's step and the
page reading's `walk` still differ: `walk` passes through a forwarded
slot to what it is assigned, so both reach the same words with the
forwarded slot as one more step in the preview's tree.
- An empty slot's fallback is read alike before and after, by the
preview and by `walk`: Chromium and Firefox give a slot's own children
as its flattened assignment when nothing is assigned to it (checked with
`leaf-dev probe`). happy-dom gives none, which made the runtime tests
read the two as different; `tests/runtime/dom.mjs` now flattens as the
DOM spec does.

**A reading holds one object for each text node** (`passages.js`,
`indexed`). Indexing a reading copied each segment the walk made, to add
where it starts in the reading's text; it now writes that into the
walk's segment, which the walk builds in one literal rather than a
spread.

## Evidence

Pages: `leaf-dev bench`'s `documents` page at size 8 (three tabs, each a
text document bound to a source), with each document 96,000 lines (4.6
MB) as on #4781's page, and the same page with short documents and a
5,000-row table in an `lf-scroll` box in each tab. The script that adds
both to `leaf-dev bench` and writes them as probe sources is not in this
change; the 96,000-line page is `leaf_dev.commands.bench` with
`DOCUMENT_STEPS = 48000`, its markup `side_documents(8)` and its data
`document_sources(8)`. CPU readings are `leaf-dev bench --read cpu
--base`, interleaved arms, lowest of the rounds; base is `4c8696480e`.
The host was shared, with a load average from 21 to over 100.

| Reading | Engine | main | This change |
| --- | --- | --- | --- |
| `capturePlace` of the open document's region, mean per call in five
batches of 20 (probe journey) | Chromium | 5.45–5.65 ms | 4.07–4.35 ms |
| same | Firefox | 5.40–7.05 ms | 3.00–4.15 ms |
| `contains` calls a wheel step, over four steps | both | 95,945 |
11,214 |
| A wheel step in the document, CPU (5 rounds, load 21–36) | Firefox |
111.5 ms | 98.3 ms |
| same (3 rounds) | Chromium | 27.2 ms | 26.0 ms |
| Opening a tab with a 96,000-line document (3 rounds) | Chromium |
324.5 ms | 315.2 ms |
| same | Firefox | 1,638 ms | 1,604 ms |
| JS heap after GC, growth for each document opened with a wheel step in
it (three in turn) | Chromium | 25.3 MB | 20.2 MB |
| Opening a tab with a 5,000-row table in an `lf-scroll` box (5 rounds)
| Chromium | 780.2 ms | 766.9 ms |
| same (3 rounds) | Firefox | 533.0 ms | 531.5 ms |

The wheel step's CPU in Firefox varied from run to run more than the
change: three other runs at loads of 24–90 read −23, −10 and +10 ms. The
`capturePlace` and `contains` rows measure the change directly. The
openings move within their noise.

## Measured and left as they were

Each was measured on main before choosing what to change. Profile shares
are from `leaf-dev bench --profile` in Chromium; the rest are CPU
readings without a profiler.

- **A scroller's Tab-stop walk on each reach paint** (`reach.js`,
`holdsOwnStop`). Opening a tab holding a 5,000-row table in an
`lf-scroll` box asks each of its 15,000 elements for a Tab stop on every
reach paint. Pricing it by answering `false` instead: the opening took
754 ms of CPU in Chromium on main and 727 ms without the walk, and 494
against 491 ms in Firefox. Keeping each box's answer needs an
invalidation mechanism, which #6036 built (an observer over each box's
tree and the shadow trees it crosses, custom element definitions and
layout notices) and was closed; 27 ms of one opening in one engine
doesn't pay for it.
- **A reading place reads a text document whole the first time it
shows** (`reading-place.js`, `shownRuns`; `passages.js`,
`partCollected`). Opening a tab with a 96,000-line document, the page's
place collects every line of the document's part, about half of the
opening's CPU in Chromium's profile (348 of 690 ms over two openings),
though every block of it stands in the document's bounded region and the
page's place drops them. Reading only the region's own words moves the
cost to the first scroll rest in the document, as #6036 found. Its
quote's uniqueness costs once more on that rest, when the document's
reading is indexed and spelled (48 of the first rest's 56 ms in the
profile), and about 1 ms on later rests. Making either cost follow the
window needs `passages.js` to read a datum in pieces smaller than a
part, such as a text document's runs, so a place reads the runs near the
window; that is a redesign of the reading's pieces, left for a change of
its own.
- **Each opened document's reading stays until the document changes**
(`passages.js`, `parts`). Released when its tab closes, the reading
would be read again whenever it is next needed, and, read from the code,
something needs it often: anchor placement re-resolves every quote
placement after any change to the page's words (`anchor-placement.js`,
`hearPageWords`), so a commented document in a closed tab would be read
whole on every change, and page search reads every part for each query.
This change makes each kept reading 5 MB smaller instead. Bounding what
is kept needs placement to leave anchors in hidden documents unresolved
until they show, a call for Max.
- **The page's place lays out every cell of a table in a box that
scrolls on its own.** On the same opening as the first item, the page's
place took 617 of 1,534 profiled ms over two openings, most of it
measuring each of the 15,000 cells in the `lf-scroll` box (`layOut`, 523
ms), and then dropped them all, since words in a box scrolling on its
own name no place (`scrollsAlone`). Other readers of `blocksOnScreen`,
such as the block a page walk starts from, keep them, so skipping them
is a behavior choice too. Not changed.
- In the same profile, lf-tabs' `#activate` spent 180 ms of each opening
inside `setAttribute`, which reads as style invalidation; not
investigated.

## Tests

- `test_a_place_in_a_long_document_names_the_line_at_its_top`
(`tests/browser/navigation/test_reading_place.py`): five lines of a
1,500-line document, each brought to the top of its box with skipped
runs above, are what the document's place quotes. It fails when
`lastHeld` skips to the block's end or 200 lines past a run, and passes
with main's `reading-place.js`.
- `tests/runtime/shadow-climb.test.mjs`: the step down meets what
`renderedParent` climbs, a forwarded slot and a slot's fallback
included, an element walk steps over text a slot is assigned, and a slot
outside a shadow tree draws its own children. Taking the step through
`assignedNodes({flatten: true})` fails the first.
- `a preview reads an empty slot's fallback as the page's reading does`
(`tests/runtime/place-preview.test.mjs`): a heading before a declared
stage whose only slot is empty previews the slot's fallback words, and
`pageText()` reads them too. A step that skips the fallback fails it.
- `tests/runtime/command-controls.test.mjs`: the nested-slot test counts
`assignedNodes`, the call the walk now makes, rather than
`assignedElements`, which it no longer calls (17 reads for 16 levels).

After merging main, `pnpm run test:runtime` (987 passed) and
`test_reading_place.py` and `test_code_and_documents.py` pass (83).
Before it, ran `pnpm run test:runtime` (985 passed), `pnpm run
check:browser`, and in the browser suites
`navigation/test_reading_place.py`,
`projection/test_reading_position.py`,
`widgets/test_code_and_documents.py`, `navigation/test_tabs.py`,
`widgets/test_tabs.py` and `widgets/test_workspaces_and_bounds.py` (184
passed); `controls/test_focus.py`, `navigation/test_key_scopes.py`,
`margin/test_pins.py`, `margin/test_scrollers.py` and the place-preview
tests of `controls/test_previews.py` (157 passed); the focus, key and
line tests of `widgets/test_diffs.py` (16) and the lent, claimed and
reach-stop tests of `test_render_aim.py` (10). An earlier run of the
first group failed
`test_a_page_up_in_a_log_a_stream_feeds_leaves_its_end` in Firefox; run
interleaved on main and this branch under load, it failed 3 of 6 times
on main and 2 of 6 here, and it passed in the final run.

Not verified: WebKit and Safari; release Firefox as opposed to
Playwright's build; memory in Firefox.

Part of #5194
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.

2 participants