Repository: flutter
Issue: Max simultaneously running Isolate count >= 8 causes the main isolate to freeze.
Labels: waiting for customer response,in triage

### Steps to reproduce

We were stepping into a problem with our app that caused it to freeze randomly, after investigating the issue we noticied that creating more than 8 isolates causes the main isolate to freeze. This is especially problematic because we are using ffi to call into c code which runs synchronous.

But it seems that issue is not limited to ffi calls, normal dart code is also causing this issue. I provided a sample repository to reproduce this issue:

https://github.com/ajungg/flutter_isolate_lockup

In this demo, you can press a button and that launches an Isolate that runs for 10 seconds, blocking using sleep. But it does not matter what you do in that isolate. The only thing that does not block the main thread would be using Future.delayed instead of sleep, but we are trying to simulate a heavy synchronous process here.

When you press this button more than 8 times in 10 seconds, the UI freezes as indicated using the CircularProgressIndicator.

### Expected results

I would expect to be able to launch more than 8 isolates since this is the only way to launch threads from inside dart code.

Native Android apps do not have problems running over 1000 threads doing a while(true) loop and rendering the UI. You may start seeing the scheduler struggle at over 1000 threads, but no freezing. Im not asking for performance here tho, i want it to just not lock up completely and that problem is a solved problem operating systems are already handling.

### Actual results

App is locking up at over 8 simultaneously running isolates

### Code sample

<details open><summary>Code sample</summary>

```dart
import 'dart:io';
import 'dart:isolate';

import 'package:flutter/material.dart';

Future<void> main() async {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  int _counter = 0;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              const CircularProgressIndicator(),
              Text('Number of isolates: $_counter'),
              FilledButton(
                onPressed: () async {
                  setState(() {
                    _counter++;
                  });
                  await Isolate.run(isolate);
                  setState(() {
                    _counter--;
                  });
                },
                child: const Text('Spawn Isolate'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

// It does not matter if we are using sleep, do some heavy calculation or calling into ffi here.
// Note: Using await Future.delayed(const Duration(seconds: 10)) instead of sleep would of course work, but we
// are trying to simulate a heavy calculation here.
Future<void> isolate() async {
  print('spawn');
  sleep(const Duration(seconds: 10));
  print('finish');
}

```

</details>

Sample repository:

https://github.com/ajungg/flutter_isolate_lockup

### Screenshots or Video

<details open>
<summary>Screenshots / Video demonstration</summary>

[Upload media here]

</details>


### Logs

<details open><summary>Logs</summary>

```console
[Paste your logs here]
```

</details>


### Flutter Doctor output

<details open><summary>Doctor output</summary>

```console
[✓] Flutter (Channel stable, 3.27.1, on Microsoft Windows [Version 10.0.26100.2605], locale de-DE)
    • Flutter version 3.27.1 on channel stable at C:\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 17025dd882 (3 weeks ago), 2024-12-17 03:23:09 +0900
    • Engine revision cb4b5fff73
    • Dart version 3.6.0
    • DevTools version 2.40.2

[✓] Windows Version (Installed version of Windows is version 10 or higher)

[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
    • Android SDK at C:\Android
    • Platform android-35, build-tools 35.0.0
    • ANDROID_HOME = C:\Android
    • Java binary at: C:\Users\impi\AppData\Local\Programs\Android Studio\jbr\bin\java
    • Java version OpenJDK Runtime Environment (build 17.0.11+0--11852314)
    • All Android licenses accepted.

[✓] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[✓] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.8.0)
    • Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Community
    • Visual Studio Community 2022 version 17.8.34309.116
    • Windows 10 SDK version 10.0.22621.0

[✓] Android Studio (version 2024.1)
    • Android Studio at C:\Users\impi\AppData\Local\Programs\Android Studio
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.11+0--11852314)

[✓] IntelliJ IDEA Community Edition (version 2024.2)
    • IntelliJ at C:\Users\impi\AppData\Local\Programs\IntelliJ IDEA Community Edition
    • Flutter plugin version 82.1.3
    • Dart plugin version 242.21829.3

[✓] VS Code, 64-bit edition (version 1.91.1)
    • VS Code at C:\Program Files\Microsoft VS Code
    • Flutter extension version 3.100.0

[✓] Connected device (4 available)
    • Pixel 9 (mobile)  • 46120DLAQ00238 • android-arm64  • Android 15 (API 35)
    • Windows (desktop) • windows        • windows-x64    • Microsoft Windows [Version 10.0.26100.2605]
    • Chrome (web)      • chrome         • web-javascript • Google Chrome 131.0.6778.205
    • Edge (web)        • edge           • web-javascript • Microsoft Edge 131.0.2903.70

[✓] Network resources
    • All expected network resources are available.

• No issues found!
```

</details>