Skip to content

Hold a departing document's faults until it stays, as Firefox aborts its loads at beforeunload - #3280

Merged
max-sixty merged 7 commits into
mainfrom
agent-a5fc0075747c16207
Oct 20, 2026
Merged

max-sixty merged 7 commits into
mainfrom
agent-a5fc0075747c16207

Conversation

@max-sixty

Copy link
Copy Markdown
Owner

Firefox aborts a document's in-flight requests as soon as a beforeunload lets a navigation start, and keeps the old document running until the destination answers. The lifetime that gates fault reports (lfDocumentLifetime, prepaint.js) ended only at pageswap or pagehide, so navigating away in Firefox during startup or a state read reported the aborted loads as page faults. Stock Firefox 158 and Playwright's Firefox posted "read failed: The operation was aborted" (state body cut off) and "page failed to start: error loading dynamically imported module" (widget module cut off) to the page's log as error events, which the agent then hears. Chrome 155 keeps the old document's loads until it commits, so it reported nothing.

This resolves the TODO item "Verify cancellation when a browser document departs" and replaces it with the decision it leaves (below).

Behaviour that changed

  • prepaint.js. After any beforeunload, a fault report waits instead of running, because neither engine says whether the navigation will replace the document: a leave prompt the user stays at, or a link answered with a download or a 204, leaves it in place with its aborted requests failed for good. pageswap or pagehide drops what waited. The user's next press, key or edit reports it. A wheel turn and the document's own first pageshow report nothing; a persisted pageshow reopens the lifetime after the page cache restores the document. Only the first report of each fault waits, so a read that keeps failing adds nothing. The header's earlier record, "beforeunload is cancelable and therefore cannot end this lifetime", is rewritten: a canceled beforeunload only asks for a prompt, and the user may still leave.
  • Consumers. The lifetime is now whenActive(fault, report) rather than an active flag, and its four consumers report through it: prepaint's startup fault intake, leaf.js's startup catch, layer-client's reportPageError (which still marks a reason reported at once, so reportCarried dedupes as before), and bootstrap's sample error port.
  • Sample child. A persisted pagehide no longer tells the host the child departed, and no longer stops the child watching its own intersection. A frame's own navigation never enters the page cache, so a persisted pagehide belongs to the containing page, which brings child and port back together. With no-store lifted by a proxy, real Chrome restored a page whose sample host then waited forever for a next document.

Checked and found harmless

Chrome navigation away during registry load, state headers and state body; removing a loading sample in Chrome and Firefox, where the child's pagehide lands before its aborted fetches reject. In every case the page reloaded on Back, caught up with the log and recorded gestures. notes/browser-support.md records the findings.

Left to decide

No served Leaf page enters Chrome's or Firefox's back/forward cache today: documents and API answers carry Cache-Control: no-store, and Firefox also refuses a page holding a sample. TODO.md now asks whether instant Back is worth lifting no-store.

Tests

  • test_a_firefox_departure_reports_none_of_the_loads_it_aborts holds a widget module in Firefox, navigates away and asserts no startup fault, console error or error event. It fails on the old lifetime.
  • The runtime lifetime test covers a fault waiting through a wheel turn and the first pageshow, being reported at the next key once however often it repeated, a startup failure queued after many reports, and dropping at pageswap/pagehide.
  • test_a_page_cache_round_trip_keeps_the_sample_connected hands the child a cached round trip's events and asserts the host stays presented and the child's visibility still follows scrolling. It fails without either child fix.

This was written by Claude Code on behalf of max-sixty

max-sixty and others added 7 commits October 19, 2026 23:45
Firefox aborts a document's in-flight requests as soon as an uncanceled
`beforeunload` lets a navigation start, and keeps the document running
until the destination answers. The lifetime that gates fault reports
ended only at `pageswap` or `pagehide`, so a navigation away in Firefox
during startup or a state read reported the aborted loads as page
faults. A probe in stock Firefox 158 and Playwright's Firefox posted
"read failed: The operation was aborted" (state body cut off) and "page
failed to start: error loading dynamically imported module" (widget
module cut off) to the page's log as `error` events, which the agent
then hears. Chrome 155 keeps the old document's loads until it commits,
so it reported nothing.

Behaviour that changed:

- prepaint.js: an uncanceled `beforeunload` now ends the lifetime too.
  The user's next input reopens it, for a navigation that leaves the
  document in place (a link answered with a download). A canceled
  `beforeunload` still ends nothing, and `pageshow` still reopens it.
  The header's earlier "beforeunload cannot end this lifetime" record is
  rewritten.
- A sample child no longer tells its host it departed on a persisted
  `pagehide`, and no longer stops watching its own intersection then.
  A frame's own navigation never enters the page cache, so a persisted
  pagehide is the containing page's, which brings child and port back
  together. With `no-store` lifted by a proxy, real Chrome restored a
  page whose sample host then waited forever for a next document.

Checked and found harmless: Chrome navigation away during registry
load, state headers and state body; removing a loading sample in Chrome
and Firefox (the child's `pagehide` lands before its aborted fetches
reject). No served Leaf page enters Chrome's or Firefox's back/forward
cache today, because documents and API answers carry `no-store`;
TODO.md now asks whether that should change.

Tests: a Firefox browser test holds a widget module, navigates away and
asserts no startup fault, console error or error event (fails on the
old lifetime). The runtime lifetime test covers uncanceled and canceled
`beforeunload` and the reopening input. A sample test hands the child a
cached round trip's events and asserts the host stays presented and the
child's visibility still follows scrolling (fails without either child
fix).

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…ng a canceled beforeunload

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…nd bound them

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@max-sixty
max-sixty merged commit a16d4ad into main Oct 20, 2026
3 checks passed
@max-sixty
max-sixty deleted the agent-a5fc0075747c16207 branch October 20, 2026 07:22
leaf-agent added a commit that referenced this pull request Oct 24, 2026
The Firefox departure test now accepts the browser's intermittent
loopback bounce-tracker notice while still requiring the aborted
module-load warning and rejecting other console problems. The [failed
main run](https://github.com/max-sixty/leaf/actions/runs/40129051834)
reached the intended startup and log assertions, then failed because
that additional privacy notice was present in the collected console
findings.

The test continues to protect the behavior introduced by #3280: a
departing page does not report its aborted widget load as a startup
fault or log error. This changes only the test's classification of a
browser-generated notice; the page runtime and its error handling are
unchanged.

Verified with `uv run pytest -q -n0
tests/browser/startup/test_readiness.py::test_a_firefox_departure_reports_none_of_the_loads_it_aborts`
(passed). The main run's diagnostic supplies the warning-present case;
the local focused run exercised the warning-absent case.

Co-authored-by: leaf-agent <[email protected]>
leaf-agent added a commit that referenced this pull request Nov 1, 2026
Iframe removal can reject a sample's pending registry fetch before
`pagehide` or `window.closed` marks its document as departed. The shared
fault lifetime then reports that cancellation as a startup failure.
Fault reports now read the lifetime in the next task, after native
removal finishes. Each queued report retains its departure generation so
a page-cache return cannot revive a canceled report.

This preserves #3280's intent: genuine faults still reach the agent,
while a departing document's canceled work does not. Sample retirement
still destroys the old realm before releasing its capability. No
error-message filtering or sample-specific exception suppression is
added.

Validation: all three reported sample paths failed on the original code
and pass with the correction, including the deliberate startup
exception's original name and stack. Twelve focused browser cases pass
across sample reset/retirement, reload, Chromium/WebKit native
departure, canceled departure, startup faults and Firefox departure. All
737 runtime tests and the browser build pass. The lifetime regression
also fails against the original prepaint and passes with the fix; it
covers rejection before pagehide and queued cancellation across a
page-cache return. The canceled-navigation test now waits for both the
native exception and Leaf's deferred report.

Closes #4906 — automated triage

Co-authored-by: leaf-agent <[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.

2 participants