Skip to content

Windows: Enable D3D11 multithread protection on ANGLE's device - #196897

Merged
auto-submit[bot] merged 3 commits into
flutter:masterfrom
canonical:windows-d3d11-multithread-protection
Nov 19, 2026
Merged

auto-submit[bot] merged 3 commits into
flutter:masterfrom
canonical:windows-d3d11-multithread-protection

Conversation

@mattkae

@mattkae mattkae commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

This is a temporary mitigation for: #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.

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')),
          ]),
        ),
      );
}

Pre-launch Checklist

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

@github-actions github-actions Bot added engine flutter/engine related. See also e: labels. platform-windows Building on or for Windows specifically a: desktop Running on desktop team-windows Owned by the Windows platform team labels Nov 18, 2026
@loic-sharma loic-sharma added the CICD Run CI/CD label Nov 18, 2026
@mattkae
mattkae marked this pull request as ready for review November 18, 2026 18:17
@mattkae
mattkae requested a review from a team as a code owner November 18, 2026 18:17

@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 enables Direct3D multithread protection on ANGLE's D3D11 device within the Windows EGL manager to prevent concurrent access issues between the render and resource threads, and includes a regression test for this behavior. The feedback recommends adding null-pointer checks for the retrieved D3D11 device in both the manager implementation and the unit test to prevent potential null-pointer dereferences.

mattkae and others added 2 commits November 18, 2026 13:40
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>

@loic-sharma loic-sharma left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks!

@loic-sharma

Copy link
Copy Markdown
Member

cc @gaaclarke from the engine team for awareness

@knopp

knopp commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

Edit: Never mind, looks like with SetMultithreadProtected there are implicit sections around each device call, enter and leave are only necessary to have atomicity over a sequence of calls. But the second question remains - is there any Angle internal state that might potentially get corrupted?

@loic-sharma

Copy link
Copy Markdown
Member

But the second question remains - is there any Angle internal state that might potentially get corrupted?

Yes, very likely. This change is only a temporary mitigation and will be reverted once the bug in ANGLE is fixed and rolled to Flutter: issues.angleproject.org/issues/587081198.

@loic-sharma loic-sharma added the autosubmit Merge PR when tree becomes green via auto submit App label Nov 18, 2026
@auto-submit
auto-submit Bot added this pull request to the merge queue Nov 19, 2026
Merged via the queue into flutter:master with commit 6ace1ae Nov 19, 2026
25 checks passed
@flutter-dashboard flutter-dashboard Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Nov 19, 2026
auto-submit Bot pushed a commit to flutter/packages that referenced this pull request Nov 19, 2026
flutter/flutter@06f32f8...e8cc6f3

2026-11-19 [email protected] Roll Skia from 280e0965687d to 3dfb69d709c7 (1 revision) (flutter/flutter#196970)
2026-11-19 [email protected] Update outdated `_formattedValues` documentation (flutter/flutter#195765)
2026-11-19 [email protected] [Impeller] Fix Vulkan GPU frame time metrics in benchmarks (flutter/flutter#196339)
2026-11-19 [email protected] Windows: Enable D3D11 multithread protection on ANGLE's device (flutter/flutter#196897)
2026-11-19 [email protected] Roll Skia from 7356c7627e87 to 280e0965687d (1 revision) (flutter/flutter#196957)
2026-11-19 [email protected] Roll Dart SDK from 091f346ad95c to d8d40cf9c21b (2 revisions) (flutter/flutter#196949)
2026-11-19 [email protected] Roll Skia from 73b9219fd1fa to 7356c7627e87 (4 revisions) (flutter/flutter#196951)
2026-11-19 [email protected] Roll Skia from 7bca23228537 to 73b9219fd1fa (1 revision) (flutter/flutter#196948)
2026-11-19 [email protected] [libimpeller] Fix swapped x and y glyph coordinates (flutter/flutter#196552)
2026-11-19 [email protected] [ios][spm]When using cocoapods plugin, give instruction that plugins may just need update (flutter/flutter#195853)
2026-11-19 [email protected] [web] Let the browser handle Control+wheel zoom outside of macOS (flutter/flutter#193362)
2026-11-19 [email protected] Fix focus violation when canRequestFocus set to false for all siblings in same build (flutter/flutter#191536)
2026-11-19 [email protected] Roll Skia from 9fc4f9e23d56 to 7bca23228537 (10 revisions) (flutter/flutter#196941)
2026-11-19 [email protected] Roll Dart SDK from 753488ede427 to 091f346ad95c (2 revisions) (flutter/flutter#196940)
2026-11-19 [email protected] iOS: Convert platform views to points with the frame's pixel ratio (flutter/flutter#196858)
2026-11-19 [email protected] [engine] Defer default font manager resolution to first use for platforms that have a prefetching API only (flutter/flutter#196377)
2026-11-18 [email protected] [flutter_tools] Add example app to package template and decouple platform configurations (flutter/flutter#192441)
2026-11-18 [email protected] [flutter_tools] Rebuild asset bundle when generated manifest content changes (flutter/flutter#191638)
2026-11-18 [email protected] Roll Skia from b11d69796f71 to 9fc4f9e23d56 (2 revisions) (flutter/flutter#196909)
2026-11-18 [email protected] [flutter_tools] Bump cupertino_icons to ^2.0.0 in app and module templates (flutter/flutter#196828)
2026-11-18 [email protected] Update iOS plugin language instructions (flutter/flutter#196614)
2026-11-18 [email protected] iOS: Add tests for rounded rect clips with per-corner/elliptical radii (flutter/flutter#196854)
2026-11-18 [email protected] Revert: [flutter_tools] Migrate lib/runner.dart off runInContext and overrides (flutter/flutter#196926)
2026-11-18 [email protected] Revert: Move macOS desktop builds to ThinLTO (flutter/flutter#196917)
2026-11-18 [email protected] [FGP] Offset split-APK versionCodes from a manifest-only versionCode (flutter/flutter#196561)
2026-11-18 [email protected] [flutter_tools] Migrate lib/runner.dart off runInContext and overrides (flutter/flutter#196387)
2026-11-18 [email protected] docs: remove broken image references from dev/snippets/README.md (flutter/flutter#195587)
2026-11-18 [email protected] Remove the FGP AGP-public-API migration coordination docs (flutter/flutter#196733)
2026-11-18 [email protected] [web] Skip stale safeBlur focus transfer when the element was refocused (flutter/flutter#196550)
2026-11-18 [email protected] Roll Skia from 81ee7327e81c to b11d69796f71 (4 revisions) (flutter/flutter#196900)

If this roll has caused a breakage, revert this CL and set the roller
to dry run mode using the controls here:
https://autoroll.skia.org/r/flutter-packages
Please CC [email protected],[email protected] on the revert to ensure that a human
is aware of the problem.

To file a bug in Packages: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
@loic-sharma loic-sharma added the cp: stable cherry pick this pull request to stable release candidate branch label Nov 19, 2026
jonasbark added a commit to OpenBikeControl/bikecontrol that referenced this pull request Nov 19, 2026
7.6.1 crashed on Windows as soon as a trainer connected. The crash is in
ANGLE (rx::Renderer11::drawArrays, null program executable). The release
workflow built on Flutter "latest", which became 3.50 the day before.
3.50 brought in a new ANGLE whose EGL and GL calls no longer exclude each
other on its D3D11 context (flutter/flutter#196618; the fix in
flutter/flutter#196897 hasn't shipped yet). Turning Impeller off doesn't
help. Pin every release build to 3.47.9, which still uses the previous
ANGLE.

pubspec.lock had dropped universal_ble back to f0fcdd3, before the
strlen(NULL) peripheral-error fix, while pubspec.yaml asks for 14a8c73.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
auto-submit Bot pushed a commit that referenced this pull request Nov 24, 2026
…vice (#196991)

Cherry-pick of #196897.

This pull request is created by [automatic cherry pick workflow](https://github.com/flutter/flutter/blob/main/docs/releases/Flutter-Cherrypick-Process.md#automatically-creates-a-cherry-pick-request)
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?

### Test Coverage:
Are you confident that your fix is well-tested by automated tests?

### Validation Steps:

Run the following 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

a: desktop Running on desktop CICD Run CI/CD cp: stable cherry pick this pull request to stable release candidate branch engine flutter/engine related. See also e: labels. platform-windows Building on or for Windows specifically team-windows Owned by the Windows platform team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants