Is it possible to detect 'dead' completers in Dart? - dart

In the Dart vm the following code will not hang - the VM will exit:
import 'dart:async';
Future<Null> main() async {
await neverCompletes();
print('Will not run');
}
Future<Null> neverCompletes() => new Completer<Null>().future;
Is there any way to detect this situation? It would be easier to investigate if the VM hangs waiting for the Future. Any flags I can pass?

Related

Could not start Observatory HTTP server

Here is my dart code
import 'package:tpdart/tpdart.dart' as tpdart;
void main(List<String> arguments) {
print('Hello world: ${tpdart.calculate()}!');
}
But when I type F5, I get this issue:
I tried to looking in Google but I didn't see any solution.

Why doesn't readLineSync works in an Isolate

void func(String dummy) {
String? name = stdin.readLineSync();
print(name);
}
void main(List<String> args) {
Isolate.spawn(func, "Testing");
}
Why doesn't my program prompts a user input ..and waits for me to enter it. Instead, it simply exits. Can someone help me out with an explanation. Not sure where to look.
I did find a similar question posted where they were using a while loop and using readLineSync inside that..and because of single thread of dart ..it wasn't working there.
Dart programs terminates when the main-isolate does not have anymore to do, no events on the event queue, and does not subscribe to any event source which would result in new events being added to the event queue (like ReceivePort or Timer). It does not matter if spawned isolates are still being executed since they will just be killed.
You need to have the main-isolate to do something, or make the main-isolate subscribe to a signal from your spawned isolate using ReceivePort/SendPort as this will prevent the main-isolate from being terminated (since it subscribes to an event source which potentially could add new events on the event queue).
An example of using addOnExitListener on an Isolate object can be seen here:
import 'dart:async';
import 'dart:io';
import 'dart:isolate';
void func(String dummy) {
print('Enter your name:');
final name = stdin.readLineSync();
print('Your name is: $name');
}
Future<void> main(List<String> args) async {
final onExitReceivePort = ReceivePort();
final isolate = await Isolate.spawn(func, "Testing");
isolate.addOnExitListener(
onExitReceivePort.sendPort,
response: 'ReadLineIsolateStopped',
);
// Listen on spawned isolate is stopped event
await for (final onExitEvent in onExitReceivePort) {
print('Got event: $onExitEvent');
onExitReceivePort.close();
}
}
We are here closing the onExitReceivePort as soon we gets one event (since the spawned isolate is then gone) which will stop our main-isolate and the rest of the program.

Async-await in dart runs synchronously?

I am trying out the Future with async await for asynchronous programming.
The code i tested is a simple Future.
//order a new coffee perhaps!
Future<String> order(String newOrder){
final String Function() func = ()=> "${newOrder} is requested!";
final _order = Future.delayed(Duration(seconds: 5),func);
return _order;
}
When i run this code like a promise.
order("promised order")
.then((result) => print(result))
.catchError((err){
print(err.error);
});
This behaves like a asynchronous non-blocking code
However the same code when i run it with async/await behaves like a synchronous code and blocks all the other code, it waits 5 secs to run the next line.
void main(List<String> arguments) async {
final _myOrder = await order('Lattee mocha');
print(_myOrder);
//...all the other code waits for
}
So i thought async/await is same as futures, where non-blocking..
How come it blocks the other code execution ?
The whole point of the async/await pattern is to wait (hence await) until the Future completes and only then continue with this code execution.
Think of it as a way to not have endless .then() chains and much simplified error handling.
If you want to not wait, then you just leave out the await keyword. Obviously, if you don't (a)wait, you don't get the result.
You can still use classic .then() chains when it suits your purpose better in your program.
What people talk about when they say "non blocking" they mean that when the compiler sees the await keyword, it knows to keep the rest of the program, the event loops, animations etc running and not block the complete program.

can you run Isolate.spawn multiple times?

here is a simple code that I use to learn isolate, I spawn twice, but the second spawn does not show anything, any mistake here? Thanks
import 'dart:isolate';
Future<void> main() async {
print('start');
await Isolate.spawn(echo, 'Dart');
await Isolate.spawn(echo, 'Flutter'); // why this 2nd spawn not showing up?
print('end');
}
void echo(msg) {
print(msg);
}
Your program quits before the Isolate has done its job. You can confirm this if you add
await Future.delayed(Duration(seconds: 1));
somewhere towards the end of your program.
Setting up Isolates is often a bit challenging, with all the SendPort stuff etc.

Dart: Using JsObject and window API in an isolate

I am having some difficulties making use of isolates in Dart. The first problem is I wanted to use dart:js to use a javascript library in one of my isolates. I tried with the following code:
void runCode(SendPort sendPort)
{
print("still ok...");
JsObject object = new JsObject(context['jsCode']);
print("still ok?");
}
void main()
{
ReceivePort receivePort = new ReceivePort();
JsObject object = new JsObject(context['jsCode']);
print("ok so far");
Isolate.spawn(runCode, receivePort.sendPort);
}
The code runs as far as "still ok..." in the runCode function and breaks when I try to use JsObject.
The second problem was I wanted to use the fileSystem API in the isolate. So I tried the following:
void runCode(SendPort sendPort)
{
window.requestFileSystem.then((FileSystem filesytem) => print('ok'));
}
void main()
{
ReceivePort receivePort = new ReceivePort();
Isolate.spawn(runCode, receivePort.sendPort);
}
This second example breaks when I reach the filesystem.
I have read: Dart : Isolate not working when using html import and from here it suggests that dart:html cannot be used in an isolate. Is this the reason why the filesystem API will not work? Is this the same case for dart:js? Or am I completely missing something?
Thanks for any help!
I've read somewhere that only the main thread has access to the DOM, which would cause any other JS action to fail if not in the main thread.

Resources