Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[beta] [wasm] is isolates supported or I'm doing something wrong? (Unsupported operation: ReceivePort) #145132

Closed
d-polikhranidi opened this issue Mar 14, 2024 · 5 comments
Labels
r: invalid Issue is closed as not valid

Comments

@d-polikhranidi
Copy link

d-polikhranidi commented Mar 14, 2024

Steps to reproduce

  1. ensure that you're on beta channel
  2. copy example code to main.dart
  3. build with flutter build web --wasm
  4. serve it with dhttpd '--headers=Cross-Origin-Embedder-Policy=credentialless;Cross-Origin-Opener-Policy=same-origin' from build/web folder
  5. click on FAB
  6. look at errors in developer console((
Снимок экрана 2024-03-14 в 10 50 10

Same behavior with Isolate.run and compute()

Is Isolates not supported in WASM or I'm doing Isolates in wrong way?

Expected results

Code will run in separate thread

Actual results

Throwing an Exception and not executes

Code sample

Code sample
import 'dart:async';
import 'dart:convert';
import 'dart:isolate';

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

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

  final String title;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(title),
      ),
      body: const Center(
        child: FlutterLogo(size: 200),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () async {
          final worker = Worker();
          await worker.spawn();
          await worker.parseJson('{"key":"value"}');
        },
        child: const Icon(Icons.run_circle),
      ),
    );
  }
}

class Worker {
  late SendPort _sendPort;
  final Completer<void> _isolateReady = Completer.sync();

  Future<void> spawn() async {
    final receivePort = ReceivePort();
    receivePort.listen(_handleResponsesFromIsolate);
    await Isolate.spawn(_startRemoteIsolate, receivePort.sendPort);
  }

  void _handleResponsesFromIsolate(dynamic message) {
    if (message is SendPort) {
      _sendPort = message;
      _isolateReady.complete();
    } else if (message is Map<String, dynamic>) {
      print(message);
    }
  }

  static void _startRemoteIsolate(SendPort port) {
    final receivePort = ReceivePort();
    port.send(receivePort.sendPort);

    receivePort.listen((dynamic message) async {
      if (message is String) {
        final transformed = jsonDecode(message);
        port.send(transformed);
      }
    });
  }

  Future<void> parseJson(String message) async {
    await _isolateReady.future;
    _sendPort.send(message);
  }
}

Flutter Doctor output

Doctor output
[✓] Flutter (Channel beta, 3.21.0-1.0.pre.2, on macOS 14.3.1 23D60 darwin-arm64, locale ru-BY)
    • Flutter version 3.21.0-1.0.pre.2 on channel beta at /Users/admin/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision c398442c35 (26 hours ago), 2024-03-12 22:26:24 -0700
    • Engine revision 0d4f78c952
    • Dart version 3.4.0 (build 3.4.0-190.1.beta)
    • DevTools version 2.33.1

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.2)
    • Android SDK at /Users/admin/Library/Android/sdk
    • Platform android-33, build-tools 33.0.2
    • Java binary at: /Users/admin/Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.9+0-17.0.9b1087.7-11185874)
    • All Android licenses accepted.

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

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

[✓] Android Studio (version 2023.2)
    • Android Studio at /Users/admin/Applications/Android Studio.app/Contents
    • 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.9+0-17.0.9b1087.7-11185874)

[✓] Connected device (5 available)
    • iPhone (XXXX) (mobile)         • XXX-XXXXX            • ios            • iOS 17.3.1 21D61
    • iPhone 15 Pro Max (mobile)      • XXXX-XX-XX-XX-XXXXXX • ios            • com.apple.CoreSimulator.SimRuntime.iOS-17-4 (simulator)
    • macOS (desktop)                 • macos                                • darwin-arm64   • macOS 14.3.1 23D60 darwin-arm64
    • Mac Designed for iPad (desktop) • mac-designed-for-ipad                • darwin         • macOS 14.3.1 23D60 darwin-arm64
    • Chrome (web)                    • chrome                               • web-javascript • Google Chrome 122.0.6261.129

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

• No issues found!
@mraleph
Copy link
Member

mraleph commented Mar 14, 2024

Isolates are, currently, not supported on the web and that includes dart2wasm.

@d-polikhranidi
Copy link
Author

Isolates are, currently, not supported on the web and that includes dart2wasm.

Thought they would finally be fully supported in WASM. But it's good to hear that they are not supported currently, so is there any chance that there will be support in the future?

@mraleph
Copy link
Member

mraleph commented Mar 14, 2024

so is there any chance that there will be support in the future?

We would like to support them, but a lot of this depends on whether or not Wasm provides necessary primitives for us to build upon. Specifically I mean proposals like these: https://github.com/WebAssembly/shared-everything-threads/blob/main/proposals/shared-everything-threads/Overview.md

Today we could wire some limited form of isolates but with restrictions on what you can send between them - and all the messages will have to be copied.

@darshankawar darshankawar added the in triage Presently being triaged by the triage team label Mar 14, 2024
@darshankawar
Copy link
Member

Based on above comment, I'll close this as not an issue.

@darshankawar darshankawar added r: invalid Issue is closed as not valid and removed in triage Presently being triaged by the triage team labels Mar 14, 2024
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 28, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
r: invalid Issue is closed as not valid
Projects
None yet
Development

No branches or pull requests

3 participants