Skip to content

dom: request the next animation frame again when requestAnimationFrame was replaced - #347523

Closed
Bryan Chen (bryanchen-d) wants to merge 7 commits into
mainfrom
copilot/vscode/anim-frame-stale-raf
Closed

Bryan Chen (bryanchen-d) wants to merge 7 commits into
mainfrom
copilot/vscode/anim-frame-stale-raf

Conversation

@bryanchen-d

@bryanchen-d Bryan Chen (bryanchen-d) commented Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator

In main CI 495093, every unit job (Electron on Linux, Windows and macOS, Browser Chromium and WebKit) timed out. About 70 tests failed in a cascade across SessionsList, the Sessions workbench, CanvasEditor and other suites. Those tests had passed in their own PRs.

Mechanism

scheduleAtNextAnimationFrame coalesces all runners for a window behind one pending requestAnimationFrame. The per-window animFrameRequested flag stays set until that frame's callback runs.

A test that fakes requestAnimationFrame and restores it before the faked frame fires leaves the request pending forever. sinon's useFakeTimers() fakes it by default. After that, no animation frame runner runs again in the renderer: every scheduleAtNextAnimationFrame sees the flag already set and never requests a real frame. Every later test that waits on a frame times out.

In main CI's full single-renderer unit run, the first non-native call comes from AutomationsCardsWidget › run feedback survives acknowledgement and refresh until the new run session appears (#347444). It uses useFakeTimers(), and a list splice schedules a frame while the fake is installed. PR CI runs only the affected tests in 4 workers, so the poisoner and its victims never shared a renderer there. Flake Fixer instrumented dom.js locally to find the first non-native call.

Change

  • Remember which requestAnimationFrame made the pending request, and request a frame again, for the same queue, when a different one is installed. In the product the function never changes, so this costs one comparison per schedule.
  • All requests for a pending queue share one token. The first callback that fires runs the queue, whichever function made its request, and every other callback for that queue does nothing. So the queue never drains twice in one frame, and a native frame still runs it if it fires while a fake is installed or after the fake is removed.
  • This doesn't replace fixing the test, which sessions: keep animation frames real in automation card fake-clock tests #347524 does. It keeps one test that leaves a faked frame pending from hanging every test after it.

Validation

  • In animationFrame.test.ts, on an isolated registered window, new tests cover four cases:
    • a fake that is restored with a frame pending, then a later schedule
    • native → fake → native with no later schedule
    • a native frame that fires while the fake is installed
    • two requests that fire in the same frame
    • a removed, unregistered iframe with a frame pending, followed by a later iframe. Unregistered windows share one queue. This is the likely cause of the intermittent WebKit OverlayWebview and WorkbenchSizeService clipping failures in main CI 495183, which passed again in 495188.
  • With the change: 49 passing in animationFrame.test.ts + dom.test.ts. On main: 46/3.
  • Against the real cascade: on 495093's head 225e6b5944, with only this dom.ts and the poisoning test unchanged, all vs/sessions/** browser unit tests in Chromium go from 29 failing to 8,077 passing and 0 failing.

Fixes microsoft/vscode-engineering#4555
Refs microsoft/vscode-engineering#4556

Authored with Copilot

…e was replaced

scheduleAtNextAnimationFrame requests one frame per window and waits for
it before requesting another. When a test fakes requestAnimationFrame
(sinon's useFakeTimers does by default) and restores it before the faked
frame fires, that request never completes, and no animation frame runner
runs again in the window. In main CI's single-renderer unit run, one
such test (an automation cards test from #347444) made about 70 later
tests across SessionsList, the Sessions workbench and others time out
until every unit job hit its 30-minute limit (495093).

Remember which requestAnimationFrame made the pending request, and
request again when a different one is installed. In the product the
function never changes, so this is one comparison per schedule.

Co-authored-by: Copilot <[email protected]>
@bryanchen-d

Copy link
Copy Markdown
Collaborator Author

Independent confirmation against the real cascade: on 225e6b5944 (main CI 495093's head), with only this PR's dom.ts applied and the poisoning automation-cards test unchanged, running all vs/sessions/** browser unit tests in Chromium goes from 29 failing to 8,077 passing and 0 failing.

The test-side trigger is removed separately in #347524. This change makes the shared animation frame runner recover from any future test that does the same.

Co-authored with Copilot

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Superseded animation-frame callbacks can drain newly queued work and cause duplicate runs per frame, while the recovery test risks leaving the shared scheduler stuck on failure.

2 open issues
What changed in this PR

This PR makes VS Code’s shared animation-frame scheduler recover when requestAnimationFrame is replaced while a frame is pending, preventing abandoned fake requests from blocking later tests.

Changes:

  • Track which function issued each window’s pending request and request another frame when that function changes.
  • Clear the tracking state when a window unregisters.
  • Add regression coverage for restoring the real function with a fake frame still pending.
File Description
src/​vs/​base/​test/​browser/​dom.test.ts Tests recovery after a pending fake animation-frame request.
src/​vs/​base/​browser/​dom.ts Adds per-window function tracking and replacement detection.

Review effort: Balanced

Comment thread src/vs/base/browser/dom.ts Resolved
Comment thread src/vs/base/test/browser/dom.test.ts Resolved
…ated window

Address review: re-requesting left the older request's callback live, so
when both fired in one frame the first could drain work queued for the
next frame. Each request now carries a token, and only the current one
runs the queue. The tests move to animationFrame.test.ts and use an
isolated registered window, so a regression can't leave the shared
runner stuck for later tests.

Co-authored-by: Copilot <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

A pending native frame can be discarded after a native-to-fake-to-native transition, leaving runners queued and tests hanging; the callback recovery path and regression coverage need updating.

1 open issue
2 resolved since last review

Review effort: Balanced

Comment thread src/vs/base/browser/dom.ts Resolved
…request unreachable

Address review: after native -> fake -> native with no schedule after the
restore, the native frame's callback was superseded by the faked request,
which never fires, so queued runners waited for the next schedule. A
superseded callback now requests a frame through the installed
requestAnimationFrame when the current request went through one that is
no longer installed. It still never runs the queue itself.

Co-authored-by: Copilot <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

A callback guard can discard the original native callback while a temporary fake is installed, leaving queued runners without a live callback and causing the original runner to hang.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Native callback during fake installation can strand queued runners

src/​vs/​base/​browser/​dom.ts:488

If the original native callback fires while the temporary fake is still installed, this guard discards it because the pending request's function still matches the fake. Restoring the native function afterward, without another schedule, leaves both runners queued with no live callback, so awaiting the original runner hangs. Allow the first live callback to drain the pending queue while invalidating other callbacks for that queue (for example, with a shared queue-generation token), and add coverage where the native callback fires before restoration.

1 resolved since last review

Review effort: Balanced

…st made it

Address review: with a token per request, a native frame that fired while
a fake was installed was treated as superseded, stranding the queue.
All requests for a pending queue now share one token: the first callback
that fires runs the queue and the others do nothing. This also covers
native -> fake -> native without a later schedule, so the re-request in
the runner is no longer needed.

Co-authored-by: Copilot <[email protected]>
@bryanchen-d

Copy link
Copy Markdown
Collaborator Author

Addressed the previously missed issue (native callback firing while the fake is installed) in 1b312ca, and simplified the change. Requests now share one token per pending queue rather than one per request. The first callback that fires for the queue runs it, whichever requestAnimationFrame made it, and every other callback for that queue does nothing. That covers:

  • a native frame firing while a fake is installed (new test runs when a native frame fires while requestAnimationFrame is faked, which fails on the previous commit),
  • native -> fake -> native with no later schedule (the runner's re-request is gone, since the native callback now runs the queue),
  • two requests firing in the same frame, where only the first drains (a superseded frame request does not run the queue).

animationFrame.test.ts + dom.test.ts: 48 passing on this head, 47/1 on the previous commit, 46/2 on main.

Co-authored with Copilot

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues remain for the animation-frame scheduler changes.

No open issues 🎉

Review effort: Balanced

Unregistered windows have no window ID, so they share one animation frame
queue. A test iframe removed with a frame pending left that queue waiting
for a frame that never comes, so later iframes' runners never ran. That's
the likely cause of the intermittent WebKit OverlayWebview and
WorkbenchSizeService clipping failures in main CI 495183. The previous
change already covers it: the later iframe has a different
requestAnimationFrame, so a frame is requested again.

Co-authored-by: Copilot <[email protected]>
@github-actions

github-actions Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Base: ca4b8fdc Current: 4cf73618

No screenshot changes.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Human assessment is needed to evaluate an animation-frame lifecycle concern that can retain an unregistered iframe’s JavaScript realm after its callbacks finish.

No open issues 🎉

Review effort: Balanced

Address review: keeping it after the frame ran retained a removed
unregistered iframe's realm. It is only needed while a request is
pending.

Co-authored-by: Copilot <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Human review is needed to assess a memory-retention risk where a strong reference keeps a detached iframe’s JavaScript realm alive because neither cleanup path runs.

No open issues 🎉

Review effort: Balanced

Address review: a removed unregistered iframe whose frame never fires
kept its realm alive through this map until the next schedule. A
collected function can't match the installed one, so the next schedule
still requests a frame.

Co-authored-by: Copilot <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues are reported for the animation-frame scheduler changes.

No open issues 🎉

Review effort: Balanced

@bryanchen-d

Copy link
Copy Markdown
Collaborator Author

Closing this, since #347731 covers the same problem from the test side: each test iframe now gets its own window ID, so a removed iframe can't block animation frames for later tests. The aux-window pair has passed on every main CI run since #347731 merged, and vscode-engineering#4556 is closed. The other stuck-frame case, a test faking requestAnimationFrame with sinon, was fixed in that test by #347524. We can reopen this if that kind of hang shows up again.

Co-authored with Copilot

@bryanchen-d
Bryan Chen (bryanchen-d) deleted the copilot/vscode/anim-frame-stale-raf branch November 12, 2026 22:46
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