onExit property

Future<void>? get onExit

Waits for the isolate to terminate.

Completes the returned future when the isolate terminates.

If the isolate has already stopped responding to commands, the returned future will be completed after one second, using ping to check if the isolate is still alive.

Implementation

Future<void>? get onExit {
  // Using [ping] to see if the isolate is dead.
  // Can't distinguish that from a slow-to-answer isolate.
  if (_onExitFuture == null) {
    var channel = SingleResponseChannel<void>();
    isolate.addOnExitListener(channel.port);
    _onExitFuture = channel.result.then(ignore);
    ping().then<void>((bool alive) {
      if (!alive) {
        channel.interrupt();
        _onExitFuture = null;
      }
    });
  }
  return _onExitFuture;
}