Skip to content

Race condition when using multiple isolates #5252

@anthonymoretti

Description

@anthonymoretti

Describe the bug
Breakpoints don't work with multiple isolates unless a post-frame callback is used.
Related to microsoft/vscode#5220.

To Reproduce
Steps to reproduce the behavior:

  1. Run the code sample on macOS.
  2. Set a breakpoint on Widget build(BuildContext context) {.
  3. Click FAB.

Expected behavior
Program breaks on breakpoint after each push of the FAB.

Code sample
import 'dart:isolate';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

void _isolateEntryPoint(Map<String, Object?> message) {
  final sendPort = message['sendPort'] as SendPort;
  Isolate.exit(sendPort, null);
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Isolates bug',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Isolates bug'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final _receivePort = ReceivePort();
  var _receivePortIsListening = false;

  void _spawnIsolate() {
    if (!_receivePortIsListening) {
      _receivePort.listen(_onIsolateData);
      _receivePortIsListening = true;
    }

    Isolate.spawn(_isolateEntryPoint, {'sendPort': _receivePort.sendPort});
  }

  void _onIsolateData(message) {
    debugPrint('Isolate has completed.');

    setState(() {});

    // Run this line (break points won't work):
    _compute();

    // Or

    // Run this line (break points will work):
    // WidgetsBinding.instance.addPostFrameCallback((_) => _compute());
  }

  void _compute() {
    compute(
      (message) => debugPrint(message['key']),
      {'key': 'Compute function has completed.'},
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _spawnIsolate,
        child: const Icon(Icons.play_arrow_outlined),
      ),
    );
  }
}
Workspace Environment
Dart Code extension: 3.96.0
Flutter extension: 3.96.0 (activated)

App: Visual Studio Code
App Host: desktop
Version: mac 1.93.0

Workspace type: Flutter (LSP)
Workspace name: bug_2024_09_06

Dart (3.5.2): /Users/anthonymoretti/Development/flutter/bin/cache/dart-sdk
Flutter (3.24.2): /Users/anthonymoretti/Development/flutter (macOS (darwin/macos))

Output from 'dart info'

/Users/anthonymoretti/Development/flutter/bin/cache/dart-sdk/bin/dart info

If providing this information as part of reporting a bug, please review the information
below to ensure it only contains things you're comfortable posting publicly.

General info

  • Dart 3.5.2 (stable) (Wed Aug 28 10:01:20 2024 +0000) on "macos_x64"
  • on macos / Version 14.6.1 (Build 23G93)
  • locale is en-US

Project info

  • sdk constraint: '^3.5.2'
  • dependencies: cupertino_icons, flutter
  • dev_dependencies: flutter_lints, flutter_test

Process info

Memory CPU Elapsed time Command line
61 MB 0.0% 01:03:09 dart devtools --machine --allow-embedding --dtd-uri ws:/XYlfE2vKxdxYN94m
707 MB 0.0% 01:03:09 dart language-server --protocol=lsp --client-id=VS-Code --client-version=3.96.0
73 MB 0.0% 01:03:09 dart tooling-daemon --machine
80 MB 0.0% 01:03:09 flutter_tools.snapshot daemon
Output from 'flutter doctor'

/Users/anthonymoretti/Development/flutter/bin/flutter doctor -v

[✓] Flutter (Channel stable, 3.24.2, on macOS 14.6.1 23G93 darwin-x64, locale en-US)
    • Flutter version 3.24.2 on channel stable at /Users/anthonymoretti/Development/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 4cf269e36d (3 days ago), 2024-09-03 14:30:00 -0700
    • Engine revision a6bd3f1de1
    • Dart version 3.5.2
    • DevTools version 2.37.2

[✗] Android toolchain - develop for Android devices
    ✗ Unable to locate Android SDK.
      Install Android Studio from: https://developer.android.com/studio/index.html
      On first launch it will assist you in installing the Android SDK components.
      (or visit https://flutter.dev/to/macos-android-setup for detailed instructions).
      If the Android SDK has been installed to a custom location, please use
      `flutter config --android-sdk` to update to that location.


[✓] Xcode - develop for iOS and macOS (Xcode 15.4)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15F31d
    • CocoaPods version 1.15.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[!] Android Studio (not installed)
    • Android Studio not found; download from https://developer.android.com/studio/index.html
      (or visit https://flutter.dev/to/macos-android-setup for detailed instructions).

[✓] VS Code (version 1.93.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.94.0

[✓] Network resources
    • All expected network resources are available.

! Doctor found issues in 2 categories.

Metadata

Metadata

Assignees

No one assigned

    Labels

    in debuggingRelates to the debug adapter or process of running debug sessionsis bug

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions