Why does creating a single ReceiverPort cause the Dart VM to hang?

Issue

e.g.:

import 'dart:isolate';

void main() { var p = new ReceivePort(); }

This will make the whole VM hang until I Ctrl-C it. Why is this?

Solution

Dart’s main function operates a bit differently than other platforms. It’s more of an ‘init’ than anything else; it can exit and the application may continue running. A Dart VM application stays alive if it is listening for events. This generally means one or more open Streams. A ReceivePort is a Stream. Closing this stream would terminate the application.

You can verify this by running this script with dart --observe script.dart and viewing the application in Observatory. You’ll notice that you have one isolate and it is ‘idle’ – this means there are ports open that are waiting for messages. You can click ‘see ports’ in the isolate panel and the ReceivePort will be the only item in the list. In general, if you are hanging and you can’t figure out why, fire up Observatory and check which ports are open.

Answered By – Joe Conway

Answer Checked By – Robin (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.