From dc202d9e537c6cfdc702346f27c09fc2db77c53a Mon Sep 17 00:00:00 2001 From: Littlegnal <8847263+littleGnAl@users.noreply.github.com> Date: Mon, 29 Jan 2024 11:27:03 +0800 Subject: [PATCH] fix: Wait for the executor.dispose done before exit the isolate (#93) 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. --- .../platform/io/iris_method_channel_internal_io.dart | 10 ++++++---- test/iris_method_channel_test.dart | 4 ---- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/lib/src/platform/io/iris_method_channel_internal_io.dart b/lib/src/platform/io/iris_method_channel_internal_io.dart index 72b07b8..9134d6b 100644 --- a/lib/src/platform/io/iris_method_channel_internal_io.dart +++ b/lib/src/platform/io/iris_method_channel_internal_io.dart @@ -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(); } } @@ -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; } @@ -494,7 +497,6 @@ class IrisMethodChannelInternalIO implements IrisMethodChannelInternal { } } - executor.dispose(); Isolate.exit(onExitSendPort, 0); } diff --git a/test/iris_method_channel_test.dart b/test/iris_method_channel_test.dart index 4752c19..5a5d0d8 100644 --- a/test/iris_method_channel_test.dart +++ b/test/iris_method_channel_test.dart @@ -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); @@ -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);