diff --git a/packages/flutter/lib/src/foundation/_isolates_io.dart b/packages/flutter/lib/src/foundation/_isolates_io.dart index 104b25a8c5..e612530b3e 100644 --- a/packages/flutter/lib/src/foundation/_isolates_io.dart +++ b/packages/flutter/lib/src/foundation/_isolates_io.dart @@ -18,6 +18,7 @@ Future compute(isolates.ComputeCallback callback, Q message, { St final Flow flow = Flow.begin(); Timeline.startSync('$debugLabel: start', flow: flow); final ReceivePort resultPort = ReceivePort(); + final ReceivePort exitPort = ReceivePort(); final ReceivePort errorPort = ReceivePort(); Timeline.finishSync(); final Isolate isolate = await Isolate.spawn<_IsolateConfiguration>>( @@ -30,7 +31,7 @@ Future compute(isolates.ComputeCallback callback, Q message, { St flow.id, ), errorsAreFatal: true, - onExit: resultPort.sendPort, + onExit: exitPort.sendPort, onError: errorPort.sendPort, ); final Completer result = Completer(); @@ -45,8 +46,13 @@ Future compute(isolates.ComputeCallback callback, Q message, { St result.completeError(exception, stack); } }); + exitPort.listen((dynamic exitData) { + if (!result.isCompleted) { + result.completeError(Exception('Isolate exited without result or error.')); + } + }); resultPort.listen((dynamic resultData) { - assert(resultData == null || resultData is R); + assert(resultData is R); if (!result.isCompleted) result.complete(resultData as R); }); @@ -78,12 +84,11 @@ class _IsolateConfiguration { } Future _spawn(_IsolateConfiguration> configuration) async { - R result; - await Timeline.timeSync( + final R result = await Timeline.timeSync( configuration.debugLabel, () async { final FutureOr applicationResult = await configuration.apply(); - result = await applicationResult; + return await applicationResult; }, flow: Flow.step(configuration.flowId), );