Skip to content

[web] Stale safeBlur timer steals focus from a text field that was refocused right after a button press #196522

Closed

Description

@Mateo-taltx

Steps to reproduce

  1. Flutter web, Flutter 3.47.6, Chrome, with semantics on (the issue shows with the accessible DOM text editing strategy).
  2. Focus a TextField.
  3. Click a button that rebuilds the page and calls focusNode.requestFocus() on the same TextField in a post-frame callback.
  4. 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

  1. The pointer press on the button blurs the focused <input>.
  2. SemanticsTextEditingStrategy.disable() calls FlutterViewManager.safeBlur(input), which defers _transferFocusToViewRoot(input) to a zero-delay Future.
  3. The app's post-frame requestFocus focuses the same <input> again first.
  4. 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.

Activity

  1. added
    in triage
    a: text input
    engine
    a: accessibility
    platform-web
    found in release: 3.47
    team-web
    and removed
    in triage
    on Nov 13, 2026
  2. mbcorona commented on Nov 13, 2026

    @mbcorona
    Member

    Thanks for the analysis @Mateo-taltx.

    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.

  3. mbcorona commented on Nov 13, 2026

    @mbcorona
    Member

    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:

    <script>
      const _origSetTimeout = window.setTimeout;
      window.setTimeout = (fn, ms, ...args) =>
          _origSetTimeout(fn, (ms | 0) === 0 ? 300 : ms, ...args);
    </script>

    2. lib/main.dart

    import 'package:flutter/rendering.dart';
    import 'package:material_ui/material_ui.dart';
    
    void main() {
      WidgetsFlutterBinding.ensureInitialized();
      SemanticsBinding.instance.ensureSemantics();
      runApp(const MaterialApp(home: Repro()));
    }
    
    class Repro extends StatefulWidget {
      const Repro({super.key});
      @override
      State<Repro> createState() => _ReproState();
    }
    
    class _ReproState extends State<Repro> {
      final FocusNode _focusNode = FocusNode();
      int _count = 0;
    
      void _onPressed() {
        setState(() => _count++);
        WidgetsBinding.instance.addPostFrameCallback((_) {
          _focusNode.requestFocus();
        });
      }
    
      @override
      void dispose() {
        _focusNode.dispose();
        super.dispose();
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Padding(
            padding: const EdgeInsets.all(24),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                TextField(focusNode: _focusNode, autofocus: true),
                const SizedBox(height: 16),
                ElevatedButton(onPressed: _onPressed, child: Text('Rebuild + refocus ($_count)')),
              ],
            ),
          ),
        );
      }
    }

    3. Steps

    1. flutter run -d chrome
    2. Type a few characters in the text field.
    3. Click "Rebuild + refocus".
    4. Wait about half a second, then type again.

    Actual: the caret is still drawn in the field, but the typed keys are lost. The browser focus is on the flutter-view element.

    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.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    a: accessibilitya: text inputenginefound in release: 3.47found in release: 3.50platform-webteam-web

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions