Closed iazel closed 1 week ago
After further analysis on Android, in case the main Activity is destroyed (i.e. AppLifecycleState.detached
), even though the Isolate still runs in background, once we open back the app, port will be limited on sending data as if they didn't share the same codebase.
Notice that even if we get paused
, the app will work without problems. It is only the detached
state that causes problems.
This can be easily tested on Android by enabling Don't keep activities
in Developer options
settings.
Code sample:
import 'dart:async';
import 'dart:isolate';
import 'dart:ui';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
final class A {}
final class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<StatefulWidget> createState() {
return _MyAppState();
}
}
final class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
final port = IsolateNameServer.lookupPortByName('name');
if (port == null) {
Isolate.spawn((_) async {
final port = ReceivePort();
port.listen((message) {
print('got $message');
});
IsolateNameServer.registerPortWithName(port.sendPort, 'name');
var i = 0;
while (true) {
++i;
await Future<void>.delayed(const Duration(seconds: 5));
print('looped $i times');
}
}, null);
} else {
port.send(A());
}
}
@override
Widget build(BuildContext context) {
return const MaterialApp(
home: Scaffold(
body: Text('Hello World'),
),
);
}
}
By changing send to something like:
port.send('restarted');
It will work as expected.
Thanks for the report. I also see the issue with the test and Don't keep activities
on app as reported. This doesn't seem to belong to Android as I see the same test failed on macOS. Labeling the issue for engine team input.
//cc @aam
The issue with original code https://github.com/flutter/flutter/issues/147469#issue-2267076870 is that registered isolate exits once it receives first message, so no further sends will work. It's confusing of course that the error talks about invalid object and only shows up if it's an instance of user-defined class being sent, it silently does nothing if primitive object is being sent.
The issue with original code #147469 (comment) is that registered isolate exits once it receives first message, so no further sends will work. It's confusing of course that the error talks about invalid object and only shows up if it's an instance of user-defined class being sent, it silently does nothing if primitive object is being sent.
Yes, we exit the isolate just to showcase the diverging behaviour. Is it expected for send
to fail in case the port is closed/not available?
Is it expected for send to fail in case the port is closed/not available?
No, it's not. Send should not behave differently whether port is open or closed. The underlying root cause of the exception is gap in the dart vm api Dart_NewSendPort/
Dart_SendPortGetId that IsolateNameServer uses to keep track of opened send ports.
The fix should have been rolled in flutter at https://github.com/flutter/flutter/commit/21da3a80633913db86cfbbb04bd38982b8d15dd5, main channel at this point
@aam do you know if the fix will also solve #113825 and #119589 ?
@aam do you know if the fix will also solve #113825 and #119589 ?
I don't think those issues are related to this.
Steps to reproduce
IsolateNameServer.registerPortWithName
listen
on the port, orclose
the port, orIsolate.kill()
the given isolateIsolateNameServer.lookupPortByName
send
Please be aware that it is important to send a custom class, otherwise the issue does not exist.
Another interesting aspect is that if we get
port
before it is closed, and keep using it, then no issue arise.Expected results
Ports normally exhibit the following behavior:
listen
is calledActual results
When using data types supported across different code-bases (e.g: int, string, etc...), no issues arise.
When using data types supported only on same code-bases (e.g. custom classes), then
ArgumentError
is thrown onport.send
if the receiving end is somehow closed or not ready.Code sample
Code sample
```dart import 'dart:isolate'; import 'dart:ui'; import 'package:flutter_test/flutter_test.dart'; final class A { } void main() { test('isolate', () async { TestWidgetsFlutterBinding.ensureInitialized(); await Isolate.spawn( (_) { final port = ReceivePort(); port.listen((_) { Isolate.exit(); }); IsolateNameServer.registerPortWithName(port.sendPort, 'test'); }, null, ); while (IsolateNameServer.lookupPortByName('test') == null) { await FutureScreenshots or Video
No response
Logs
Logs
```console dart:isolate _SendPort.send test/isolate_test.dart 28:51 main.Flutter Doctor output
Doctor output
```console Doctor summary (to see all details, run flutter doctor -v): [✓] Flutter (Channel stable, 3.19.6, on Ubuntu 23.10 6.5.0-28-generic, locale en_US.UTF-8) [✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0) [✓] Chrome - develop for the web [✓] Linux toolchain - develop for Linux desktop [✓] Android Studio (version 2023.2) [✓] IntelliJ IDEA Community Edition (version 2023.3) [✓] Connected device (2 available) [✓] Network resources • No issues found! ```