Skip to content

fix(scheduler): keep source fairness runs across lifecycle turns - #2806

Merged
ldm0 merged 1 commit into
mainfrom
fix/scheduler-fairness-run
Nov 12, 2026
Merged

ldm0 merged 1 commit into
mainfrom
fix/scheduler-fairness-run

Conversation

@lanyue-llk

@lanyue-llk lanyue-llk commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

PR #2806 at a glance

Closes #2794. Replaces part of #1584.

Problem. When a page has a pending lifecycle turn (for example an async script still loading) and a Worker floods the page with messages, a due timer can wait until the whole message backlog has drained. With the issue's page (moli fetch URL --wait networkidle --dump html, a 250 ms setTimeout started while 50,000 worker messages are queued and a 2.5 s async script is pending), Chromium reports data-worker-timer-delay="251". v1.1.23 reports ~2,600 ms, and a debug build of main reports 16-17 s.

Change. In runtime/page_turn_scheduler.rs, select_turn_class forces a document-lifecycle turn after 8 ordinary turns. record_selected_turn_class then called PageTaskArbitrationState::break_consecutive_run(), which reset the per-source run counter. select_ready_descriptor rotates to another source only after a source has run 8 times in a row, so the counter was always reset first and rotation never happened while lifecycle work was pending. The reset and the helper are removed. A lifecycle turn is not an ordinary-source selection, so it neither extends nor ends the current source's run. The existing test lifecycle_turn_breaks_an_ordinary_source_consecutive_run encoded the old behaviour. It becomes lifecycle_turn_keeps_an_ordinary_source_consecutive_run, which asserts that the 9th selection from the dominant source rotates to the other ready source even with a lifecycle turn in between.

Verification.

  • Issue repro (local server), --wait networkidle --dump html:
    • v1.1.23: 2619, 2677, 2617;
    • main debug (cbf4d01): 17239, 16444, 16490;
    • this branch, debug: 251, 251, 250, 251, 251 (Chromium 251).
  • cargo nextest run -p moli-renderer-v8 -E 'test(page_turn_scheduler)': 23 passed.
  • cargo nextest run -p moli -p moli-core -E 'test(networkidle) | test(worker) | test(timer)': 515 of 516 passed. The failure was cli_quiet_page_networkidle_uses_remaining_best_effort_budget, a test that leaves 250 ms of budget. On re-runs the pair of cli_quiet_page_* tests passed in 8 of 10 runs while the host load average was ~8-10, and in 6 of 6 runs once load dropped. The same scenario run directly (500 ms page, --timeout 750, 15 interleaved runs each) passed 15/15 on both main and this branch for networkidle and domstable. The quiet page has no lifecycle-plus-flood contention, so the scheduler change does not affect it. CI is the authority.
  • cargo fmt --all clean; cargo clippy -p moli-renderer-v8 --all-targets --all-features -- -D warnings clean (workspace clippy not run locally).
  • Not run locally: the full workspace nextest (the renderer test binary does not link within local RAM); left to CI.

Cost. The change removes work: one counter reset per lifecycle turn. The fairness override still applies only after 8 consecutive tasks from one source while another source is ready, so ordinary pages schedule as before. The debug builds compared are main cbf4d01 and this branch (main 6786667 + this change), with the same profile, /usr/bin/time -l, alternating runs, median of 7:

case main wall / max RSS branch wall / max RSS
issue repro, --wait networkidle 13.60 s / 163.6 MB 3.08 s / 159.2 MB
ordinary local article, default wait 0.09 s / 88.3 MB 0.08 s / 88.3 MB
ordinary local article, --wait networkidle 0.59 s / 88.3 MB 0.59 s / 88.3 MB

@lanyue-llk lanyue-llk left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Independent review of 4d28ea4

No blocking findings

  • Root cause: fixed. record_selected_turn_class no longer resets the source run (page_turn_scheduler.rs:290-303), so the >= 8 check at :327-331 can now fire while lifecycle work is pending. This is the cause described in #2794, not a workaround for it.
  • The other direction: there is no new starvation. The change only touches PageTaskArbitrationState. Class arbitration (consecutive_from_class, :256-271), the runnable-continuation priority (:229-235) and select_lifecycle_turn work exactly as before. A lifecycle turn still runs after at most 8 ordinary turns, so the load event is not delayed. A timer flood or a worker flood now gives another ready source (worker, network, timer) 1 turn in every 9 instead of none. That matches the existing behaviour when no lifecycle turn is pending.
  • Old test: git log -S break_consecutive_run and -S lifecycle_turn_breaks_an_ordinary_source both lead only to the squashed 27b5135cb6 Public preview, so no rationale is recorded. The test asserted that the oldest source keeps winning after a lifecycle turn. HTML gives no ordering guarantee across task sources, and the module doc says fairness "is not an HTML cross-source ordering guarantee". The test therefore did not encode a real requirement, and replacing it is correct. The only other caller, document_write_resource_tests.rs:475, uses a fresh scheduler and is unaffected.
  • New test: on main the reset sets the counter to 0, so the 9th selection returns DynamicImportOwnerAction and the new ModulepreloadStart assertion fails. On the branch the counter is 8 and rotation selects ModulepreloadStart. So it fails on main and passes here, which I worked out from the code without running it.
  • Cost: the change removes one reset per lifecycle turn and adds no per-turn work. The body's numbers are plausible: the repro takes about 3.08 s wall time, mostly the 2.5 s async script.
  • CI: Format, Node, Python and Windows pass. Nextest, Clippy, the release builds and CDP smoke were still pending when I reviewed.
  • Picture: pending (the body has only the <!-- picture --> placeholder).

@ldm0
ldm0 self-requested a review November 12, 2026 09:35
@ldm0

ldm0 commented Nov 12, 2026

Copy link
Copy Markdown
Member

Awesome work!

@ldm0
ldm0 merged commit 7521d39 into main Nov 12, 2026
8 of 10 checks passed
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.

A 250 ms timer fires 2-3.5 s late when a Worker floods messages while page load is pending

2 participants