You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[web] Stale safeBlur timer steals focus from a text field that was refocused right after a button press #196522
Flutter web, Flutter 3.47.6, Chrome, with semantics on (the issue shows with the accessible DOM text editing strategy).
Focus a TextField.
Click a button that rebuilds the page and calls focusNode.requestFocus() on the same TextField in a post-frame callback.
Do this while the browser's main thread is busy (CPU throttling or load), or delay zero-delay timers in the page, for example by wrapping setTimeout so that delay 0 callbacks run 300 ms later, in order.
Expected results
The text field keeps the browser focus, and typed keys reach it.
Actual results
The browser focus ends on the flutter-view element, while Flutter still draws the caret in the field.
Typed keys are lost, and nothing restores the focus.
Analysis
The pointer press on the button blurs the focused <input>.
SemanticsTextEditingStrategy.disable() calls FlutterViewManager.safeBlur(input), which defers _transferFocusToViewRoot(input) to a zero-delay Future.
The app's post-frame requestFocus focuses the same <input> again first.
When the deferred timer runs late, input is the active element again, so _transferFocusToViewRoot moves focus to the view root even though the blur it was scheduled for has already been undone.
A stack captured by wrapping HTMLElement.prototype.focus confirms the call comes from the deferred safeBlur closure (_TimerImpl → _transferFocusToViewRoot).
Workaround
Refocus the field after one more zero-delay Timer (scheduled after the post-frame callback), so the stale safeBlur runs first and finds its element no longer focused by it.
Suggestion
In _transferFocusToViewRoot, skip the transfer if the element was refocused after safeBlur was scheduled (for example, track a generation or the last focus event), or cancel the pending transfer when the element regains focus.
Confirmed from source: safeBlur defers _transferFocusToViewRoot with a zero-delay Future, and when it runs it only checks that the element is the active element, so if the same <input> was refocused in between, focus still moves to the view root. The code is the same on master.
Related to #195763, where the same stale safeBlur is the second half of the chain, but the trigger here is different (a post-frame requestFocus under load). I'll work on a fix.
Activity
mbcorona commentedon Nov 13, 2026
Thanks for the analysis @Mateo-taltx.
Confirmed from source:
safeBlurdefers_transferFocusToViewRootwith a zero-delayFuture, and when it runs it only checks that the element is the active element, so if the same<input>was refocused in between, focus still moves to the view root. The code is the same on master.Related to #195763, where the same stale
safeBluris the second half of the chain, but the trigger here is different (a post-framerequestFocusunder load). I'll work on a fix.mbcorona commentedon Nov 13, 2026
Here is a minimal reproduction. It fails on every run, without needing CPU throttling.
1.
web/index.html: add this inside<head>. It delays every zero-delay timer by 300 ms, which stands in for a busy main thread:2.
lib/main.dart3. Steps
flutter run -d chromeActual: the caret is still drawn in the field, but the typed keys are lost. The browser focus is on the
flutter-viewelement.Expected: the typed characters appear in the field.
I opened #196550 with a fix. With it, the same app keeps the focus on the field.
[web] Skip stale safeBlur focus transfer when the element was refocus…