Skip to content

Commit

Permalink
fix: Wait for the executor.dispose done before exit the isolate (#93)
Browse files Browse the repository at this point in the history
We send a `null` to the sub isolate the clean up the native
resources(`destroyNativeApiEngine`...), but we do not wait for the
clean-up logic to be finished, which will cause an error if we want to
clean up something relative to the native resources right after the
`dispose` function in sychronize way.
  • Loading branch information
littleGnAl committed Jan 29, 2024
1 parent d82e2c0 commit dc202d9
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
10 changes: 6 additions & 4 deletions lib/src/platform/io/iris_method_channel_internal_io.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ class _Messenger implements DisposableObject {
}
_isDisposed = true;
requestPort.send(null);
await responseQueue.cancel();
// Wait for `executor.dispose` done in isolate.
await responseQueue.next;
responseQueue.cancel();
}
}

Expand Down Expand Up @@ -450,8 +452,9 @@ class IrisMethodChannelInternalIO implements IrisMethodChannelInternal {
// Wait for messages from the main isolate.
await for (final request in apiCallPort) {
if (request == null) {
// Exit if the main isolate sends a null message, indicating there are no
// more files to read and parse.
executor.dispose();
mainApiCallSendPort.send(null);
// Ready exit the isolate.
break;
}

Expand Down Expand Up @@ -494,7 +497,6 @@ class IrisMethodChannelInternalIO implements IrisMethodChannelInternal {
}
}

executor.dispose();
Isolate.exit(onExitSendPort, 0);
}

Expand Down
4 changes: 0 additions & 4 deletions test/iris_method_channel_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,6 @@ void main() {
test('disposed', () async {
await irisMethodChannel.initilize([]);
await irisMethodChannel.dispose();
// Wait for `dispose` completed.
await Future.delayed(const Duration(milliseconds: 500));
final callRecord1 = messenger.callApiRecords
.where((e) => e.methodCall.funcName == 'destroyNativeApiEngine');
expect(callRecord1.length, 1);
Expand All @@ -227,8 +225,6 @@ void main() {
await irisMethodChannel.initilize([]);
await irisMethodChannel.dispose();
await irisMethodChannel.dispose();
// Wait for `dispose` completed.
await Future.delayed(const Duration(milliseconds: 500));
final callRecord1 = messenger.callApiRecords
.where((e) => e.methodCall.funcName == 'destroyNativeApiEngine');
expect(callRecord1.length, 1);
Expand Down

0 comments on commit dc202d9

Please sign in to comment.