Skip to content

[CP-stable]Windows: Enable D3D11 multithread protection on ANGLE's device - #196991

Merged
auto-submit[bot] merged 1 commit into
flutter:flutter-3.50-candidate.0from
flutteractionsbot:cp-stable-6ace1aedf035979d42137b3c93093ecd31e3df5d
Nov 24, 2026
Merged

auto-submit[bot] merged 1 commit into
flutter:flutter-3.50-candidate.0from
flutteractionsbot:cp-stable-6ace1aedf035979d42137b3c93093ecd31e3df5d

Conversation

@flutteractionsbot

@flutteractionsbot flutteractionsbot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Cherry-pick of #196897.

This pull request is created by automatic cherry pick workflow
Please fill in the form below, and a flutter domain expert will evaluate this cherry pick request.

Issue Link:

#196618

Impact Description:

Flutter Windows 3.50.0 crashes when the IO and raster threads do concurrent GPU work.

Changelog Description:

[flutter/196897] Fix Windows crash when IO and raster threads do concurrent GPU work

Workaround:

None. (Turning off Impeller does not workaround this issue.)

Risk:

What is the risk level of this cherry-pick?

  • Low
  • Medium
  • High

Test Coverage:

Are you confident that your fix is well-tested by automated tests?

  • Yes
  • No

Validation Steps:

Run the following minimal repro for a few minutes. It crashes before this fix.

Minimal repro...

Minimal repro provided by @jerome-saltmarsh:

// Minimal repro: frames presented every vsync while images are decoded and
// uploaded (the engine's IO thread) without pause.
import 'dart:async';
import 'dart:typed_data';
import 'dart:ui' as ui;

import 'package:flutter/widgets.dart';

void main() => runApp(const Race());

class Race extends StatefulWidget {
  const Race({super.key});

  @override
  State<Race> createState() => _RaceState();
}

class _RaceState extends State<Race> with SingleTickerProviderStateMixin {
  late final AnimationController _spin = AnimationController(vsync: this, duration: const Duration(seconds: 2))..repeat();
  final List<ui.Image> _images = [];
  int _decoded = 0;

  @override
  void initState() {
    super.initState();
    for (var i = 0; i < 4; i++) {
      _decodeForever(i);
    }
  }

  Future<void> _decodeForever(int seed) async {
    const side = 256;
    final pixels = Uint8List(side * side * 4);
    while (mounted) {
      for (var i = 0; i < pixels.length; i += 4) {
        pixels[i] = (i + _decoded * 7 + seed * 50) & 255;
        pixels[i + 1] = (i >> 8) & 255;
        pixels[i + 2] = 128;
        pixels[i + 3] = 255;
      }
      final done = Completer<ui.Image>();
      ui.decodeImageFromPixels(pixels, side, side, ui.PixelFormat.rgba8888, done.complete);
      final image = await done.future;
      _decoded++;
      _images.add(image);
      if (_images.length > 8) _images.removeAt(0).dispose();
    }
  }

  @override
  Widget build(BuildContext context) => Directionality(
        textDirection: TextDirection.ltr,
        child: AnimatedBuilder(
          animation: _spin,
          builder: (context, _) => Stack(children: [
            for (final image in _images) Positioned(left: 20.0 + 40 * _images.indexOf(image), top: 20, child: RawImage(image: image, width: 128, height: 128)),
            Positioned(left: 300 + 200 * _spin.value, top: 300, child: Text('decoded $_decoded')),
          ]),
        ),
      );
}

…er#196897)

This is a temporary mitigation for:
flutter#196618. This change will be
hotfixed to the 3.50 release branch.

This change will likely be reverted once the bug in ANGLE is fixed and
rolled to Flutter: https://issues.angleproject.org/issues/587081198

Derived from @loic-sharma gist:
https://gist.github.com/loic-sharma/370268be65175cb28a6a1349ed34448d

## Problem
Since ANGLE commit d778c8969d ("Replace global lock with per Display
lock"), EGL calls such as `eglSwapBuffers` and `eglMakeCurrent` only
take a per-display mutex. Flutter builds ANGLE with
`angle_force_context_check_every_call`, so GL calls still take ANGLE's
global mutex and the two no longer exclude each other.

As a result, the raster thread's present can run on ANGLE's D3D11
immediate context while the IO thread uploads a texture through the
resource context. The immediate context is not thread safe, so this
corrupts driver state and crashes the app.

## Fix
This PR mitigates this by enabling `ID3D10Multithread` protection on
ANGLE's immediate context so each D3D11 call is atomic. This does not
serialize ANGLE's own shared state and should be removed once ANGLE's
EGL calls exclude GL calls again.

## Manual test

Run the minimal repro for a few minutes. It crashes before this fix.

<details><summary>Minimal repro...</summary>

Minimal repro provided by @jerome-saltmarsh:

```dart
// Minimal repro: frames presented every vsync while images are decoded and
// uploaded (the engine's IO thread) without pause.
import 'dart:async';
import 'dart:typed_data';
import 'dart:ui' as ui;

import 'package:flutter/widgets.dart';

void main() => runApp(const Race());

class Race extends StatefulWidget {
  const Race({super.key});

  @OverRide
  State<Race> createState() => _RaceState();
}

class _RaceState extends State<Race> with SingleTickerProviderStateMixin {
  late final AnimationController _spin = AnimationController(vsync: this, duration: const Duration(seconds: 2))..repeat();
  final List<ui.Image> _images = [];
  int _decoded = 0;

  @OverRide
  void initState() {
    super.initState();
    for (var i = 0; i < 4; i++) {
      _decodeForever(i);
    }
  }

  Future<void> _decodeForever(int seed) async {
    const side = 256;
    final pixels = Uint8List(side * side * 4);
    while (mounted) {
      for (var i = 0; i < pixels.length; i += 4) {
        pixels[i] = (i + _decoded * 7 + seed * 50) & 255;
        pixels[i + 1] = (i >> 8) & 255;
        pixels[i + 2] = 128;
        pixels[i + 3] = 255;
      }
      final done = Completer<ui.Image>();
      ui.decodeImageFromPixels(pixels, side, side, ui.PixelFormat.rgba8888, done.complete);
      final image = await done.future;
      _decoded++;
      _images.add(image);
      if (_images.length > 8) _images.removeAt(0).dispose();
    }
  }

  @OverRide
  Widget build(BuildContext context) => Directionality(
        textDirection: TextDirection.ltr,
        child: AnimatedBuilder(
          animation: _spin,
          builder: (context, _) => Stack(children: [
            for (final image in _images) Positioned(left: 20.0 + 40 * _images.indexOf(image), top: 20, child: RawImage(image: image, width: 128, height: 128)),
            Positioned(left: 300 + 200 * _spin.value, top: 300, child: Text('decoded $_decoded')),
          ]),
        ),
      );
}
```

</details>

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [AI contribution guidelines] and understand my
responsibilities, or I am not using AI tools.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant in-code documentation (doc comments with
`///`).
- [x] If this PR introduces a new feature or capability, I created and
linked a website documentation issue or PR in [flutter/website] (or
verified none is needed).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.

---------

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@flutteractionsbot flutteractionsbot added the cp: review Cherry-picks in the review queue label Nov 19, 2026
@flutteractionsbot
flutteractionsbot requested a review from a team as a code owner November 19, 2026 17:47
@flutteractionsbot flutteractionsbot added the cp: review Cherry-picks in the review queue label Nov 19, 2026
@flutteractionsbot

Copy link
Copy Markdown
Contributor Author

@loic-sharma please fill out the PR description above, afterwards the release team will review this request.

@flutter-dashboard flutter-dashboard Bot added the CICD Run CI/CD label Nov 19, 2026
@flutter-dashboard

Copy link
Copy Markdown

This pull request was opened from and to a release candidate branch. This should only be done as part of the official Flutter release process. If you are attempting to make a regular contribution to the Flutter project, please close this PR and follow the instructions at Tree Hygiene for detailed instructions on contributing to Flutter.

Reviewers: Use caution before merging pull requests to release branches. Ensure the proper procedure has been followed.

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request introduces multithread protection on ANGLE's D3D11 device within the Windows EGL manager. It adds the EnableMultithreadProtection method to retrieve the D3D11 immediate context, query for the ID3D10Multithread interface, and enable multithread protection. Additionally, a corresponding unit test has been added to verify this behavior. There are no review comments, and I have no feedback to provide.

@loic-sharma
loic-sharma requested a review from mattkae November 19, 2026 18:21
@camsim99 camsim99 added the autosubmit Merge PR when tree becomes green via auto submit App label Nov 24, 2026
@auto-submit
auto-submit Bot merged commit ec45c67 into flutter:flutter-3.50-candidate.0 Nov 24, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

autosubmit Merge PR when tree becomes green via auto submit App CICD Run CI/CD cp: review Cherry-picks in the review queue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants