onExit property

Future<void>? 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 {
  // TODO(lrn): Is there a way to see if an isolate is dead
  // so we can close the receive port for this future?
  // Using [ping] for now.
  if (_onExitFuture == null) {
    var channel = SingleResponseChannel<void>();
    isolate.addOnExitListener(channel.port);
    _onExitFuture = channel.result.then(ignore);
    ping().then((bool alive) {
      if (!alive) {
        channel.interrupt();
        _onExitFuture = null;
      }
    });
  }
  return _onExitFuture;
}