Skip to content

[focus] Fix enqueueFocus cleanup not cancelling RAF when a newer call has overwritten rafId - #4721

Closed
stefee wants to merge 1 commit into
mui:masterfrom
stefee:master
Closed

stefee wants to merge 1 commit into
mui:masterfrom
stefee:master

Conversation

@stefee

@stefee stefee commented May 1, 2026 •

Copy link
Copy Markdown
Contributor

The cleanup function returned by enqueueFocus only cancels the queued RAF if rafId still equals currentRafId:

return () => {
  if (rafId === currentRafId) {  // misses if a newer call has overwritten rafId
    cancelAnimationFrame(currentRafId);
    rafId = 0;
  }
};

When a second call to enqueueFocus happens after the first (e.g. a nested or concurrent popup interaction), rafId gets overwritten. The first call's cleanup no longer matches, so its RAF is never cancelled on unmount and fires as a ghost after the component is gone.

Moving the cancelAnimationFrame outside the guard fixes this — it's always safe to cancel a specific RAF id, even if it has already fired or been cancelled by another call.

Fixes a scenario where a Combobox inside a Dialog leaves a pending RAF after unmount that fires during a subsequent render and steals focus, preventing the popup from opening.

@pkg-pr-new

pkg-pr-new Bot commented May 1, 2026

Copy link
Copy Markdown
  • base-ui-tanstack-start

  • vite-css-base-ui-example

    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/react@4721
    
    pnpm add https://pkg.pr.new/mui/base-ui/@base-ui/utils@4721
    

commit: 1fcd089

@code-infra-dashboard

code-infra-dashboard Bot commented May 1, 2026 •

Copy link
Copy Markdown

Bundle size

Bundle Parsed size Gzip size
@base-ui/react 0B(0.00%) 🔺+1B(0.00%)

Details of bundle changes

Performance

Total duration: 1,384.52 ms 🔺+124.48 ms(+9.9%) | Renders: 53 (+0) | Paint: 2,120.02 ms 🔺+178.69 ms(+9.2%)

Test Duration Renders
Select mount (200 instances) 179.28 ms 🔺+44.11 ms(+32.6%) 3 (+0)
Mixed surface mount (app-like density) 119.18 ms 🔺+38.31 ms(+47.4%) 5 (+0)
Tabs mount (200 instances) 273.83 ms 🔺+36.51 ms(+15.4%) 4 (+0)
Menu mount (300 instances) 161.62 ms 🔺+26.42 ms(+19.5%) 2 (+0)
Select open (500 options) 65.23 ms 🔺+13.78 ms(+26.8%) 14 (+0)

…and 7 more — details


Check out the code infra dashboard for more information about this PR.

@netlify

netlify Bot commented May 1, 2026

Copy link
Copy Markdown

✅ Deploy Preview for base-ui ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 1fcd089
🔍 Latest deploy log https://app.netlify.com/projects/base-ui/deploys/69f4da57a00e630008dd2d07
😎 Deploy Preview https://deploy-preview-4721--base-ui.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@atomiks

atomiks commented May 4, 2026

Copy link
Copy Markdown
Contributor

Do you have a regression test for the Combobox-in-Dialog repro? I can see a bug with cancelPrevious: false, but all callsites use the default cancelPrevious: true, so I’m not seeing how the stale rAF survives a later enqueueFocus call in any component 🤔

@stefee

stefee commented May 5, 2026

Copy link
Copy Markdown
Contributor Author

@atomiks Hmm, yeah fair - I think maybe the scenario I hit is actually the component unmounting while a RAF from enqueueFocus is still pending (before a subsequent call with cancelPrevious: true).

So maybe the correct thing would be to cancel the pending RAF in FloatingFocusManager's unmount handler?

@stefee

stefee commented May 5, 2026 •

Copy link
Copy Markdown
Contributor Author

This is a weird thing and a bit tricky to repro outside of a real project. I actually encountered it when upgrading from a M1 Pro macbook to M5 Pro and running jsdom tests (with React Testing Library). There was a race condition in the tests that meant the tests passed 100% of the time on M1 Pro and failed 100% of the time on M5 Pro.

This was the fix that led to tests passing on both machines:

Screenshot 2026-05-05 at 10 17 30 am

@stefee

stefee commented May 5, 2026

Copy link
Copy Markdown
Contributor Author

OK I was actually able to put together a minimal repro here: https://github.com/stefee/base-ui-4721-repro

Hope this helps!

@atomiks

atomiks commented May 6, 2026 •

Copy link
Copy Markdown
Contributor

Yeah the enqueueFocus part isn't the right fix for this because enqueueFocus isn't called again in the test's flow. The issue is that enqueueFocus's rAF doesn't get canceled if focus redirects before it fires, but there's no simple way (e.g. effect cleanup) to cancel it in this case. Rather it needs to check if focus moved somewhere acceptable before the rAF completed so it can decide if it should be ignored. (Or a focusin handler needs to listen for this and call cancelAnimationFrame from there.)

In a real app, there's no practical bug because user interaction doesn't happen before that rAF fires (1 frame ~5-20ms), so it only shows up in a test or automated interactions

The root cause:

  • Dialog opens, FloatingFocusManager queues initial focus via queueMicrotask + rAF
  • The test waits for Fruit to become enabled, then clicks the Combobox input
  • The Combobox opens on click, which focuses the input
  • The Dialog’s pending initial focus rAF fires later than the above interactions and focuses the Dialog’s default tabbable element, which in the repro is Cancel, not the Combobox input
  • The Combobox input loses focus because Dialog's initialFocus "steals" it away, so the Combobox closes - findByText("apple") fails because the popup was opened and then immediately closed

@stefee

stefee commented May 6, 2026

Copy link
Copy Markdown
Contributor Author

I think this makes sense @atomiks - what do you think of something like this: stefee@133ac72 ?

@atomiks

atomiks commented May 8, 2026

Copy link
Copy Markdown
Contributor

Yes that looks like it'd work, if all tests pass

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: bug It doesn't behave as expected.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants