-
Notifications
You must be signed in to change notification settings - Fork 17
fix: handle exceptions when FlutterEngine is suspended #595
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ttypic
merged 1 commit into
main
from
ECO-5703/fix-errors-because-of-flutter-engine-inactivity
Apr 8, 2026
+179
−21
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| on: | ||
| workflow_dispatch: | ||
| pull_request: | ||
| push: | ||
| branches: | ||
| - main | ||
|
|
||
| jobs: | ||
| ios-unit-tests: | ||
| runs-on: macos-latest | ||
| steps: | ||
| - uses: actions/checkout@v4 | ||
|
|
||
| - uses: futureware-tech/simulator-action@v5 | ||
| id: ios-simulator | ||
|
|
||
| - uses: subosito/flutter-action@v2 | ||
| with: | ||
| flutter-version: '3.29' | ||
| cache: true | ||
|
|
||
| - name: Set up iOS test project | ||
| # --skip-tests sets up the CocoaPods workspace (downloads all deps, generates | ||
| # the ably_flutter-Unit-Tests scheme) without booting a simulator | ||
| run: | | ||
| cd ios | ||
| pod lib lint ably_flutter.podspec \ | ||
| --allow-warnings \ | ||
| --skip-tests \ | ||
| --validation-dir=/tmp/ably_test_build \ | ||
| --no-clean | ||
|
|
||
| - name: Run iOS unit tests | ||
| timeout-minutes: 30 | ||
| run: | | ||
| xcodebuild test \ | ||
| -workspace /tmp/ably_test_build/App.xcworkspace \ | ||
| -scheme ably_flutter-Unit-Tests \ | ||
| -destination 'id=${{ steps.ios-simulator.outputs.udid }}' \ | ||
| CODE_SIGNING_ALLOWED=NO | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| #import <XCTest/XCTest.h> | ||
| #import <Flutter/Flutter.h> | ||
| #import "AblyStreamsChannel.h" | ||
|
|
||
| // Mock binary messenger: captures the binaryMessageHandler registered by AblyStreamsChannel, | ||
| // and optionally throws NSInternalInconsistencyException on sendOnChannel:message: to simulate | ||
| // a suspended/detached FlutterEngine. | ||
| @interface MockThrowingMessenger : NSObject <FlutterBinaryMessenger> | ||
| @property(nonatomic, copy) FlutterBinaryMessageHandler capturedHandler; | ||
| @property(nonatomic, assign) BOOL shouldThrow; | ||
| @property(nonatomic, assign) NSInteger sendCallCount; | ||
| @end | ||
|
|
||
| @implementation MockThrowingMessenger | ||
|
|
||
| - (void)sendOnChannel:(NSString*)channel message:(NSData*)message { | ||
| _sendCallCount++; | ||
| if (_shouldThrow) { | ||
| [NSException raise:NSInternalInconsistencyException | ||
| format:@"Sending a message before the FlutterEngine has been run."]; | ||
| } | ||
| } | ||
|
|
||
| - (void)sendOnChannel:(NSString*)channel message:(NSData*)message binaryReply:(FlutterBinaryReply)callback {} | ||
|
|
||
| - (FlutterBinaryMessengerConnection)setMessageHandlerOnChannel:(NSString*)channel | ||
| binaryMessageHandler:(FlutterBinaryMessageHandler _Nullable)handler { | ||
| _capturedHandler = handler; | ||
| return 0; | ||
| } | ||
|
|
||
| - (FlutterBinaryMessengerConnection)setMessageHandlerOnChannel:(NSString*)channel | ||
| binaryMessageHandler:(FlutterBinaryMessageHandler _Nullable)handler | ||
| taskQueue:(NSObject<FlutterTaskQueue>* _Nullable)taskQueue { | ||
| _capturedHandler = handler; | ||
| return 0; | ||
| } | ||
|
|
||
| - (void)cleanUpConnection:(FlutterBinaryMessengerConnection)connection {} | ||
|
|
||
| - (NSObject<FlutterTaskQueue>*)makeBackgroundTaskQueue { return nil; } | ||
|
|
||
| @end | ||
|
|
||
| // Helper stream handler that captures the eventSink passed to it during stream setup. | ||
| @interface CapturingSinkHandler : NSObject <FlutterStreamHandler> | ||
| @property(nonatomic, copy) FlutterEventSink capturedSink; | ||
| @end | ||
|
|
||
| @implementation CapturingSinkHandler | ||
|
|
||
| - (FlutterError*)onListenWithArguments:(id)arguments eventSink:(FlutterEventSink)events { | ||
| _capturedSink = events; | ||
| return nil; | ||
| } | ||
|
|
||
| - (FlutterError*)onCancelWithArguments:(id)arguments { return nil; } | ||
|
|
||
| @end | ||
|
|
||
| @interface AblyStreamsChannelTests : XCTestCase | ||
| @end | ||
|
|
||
| @implementation AblyStreamsChannelTests | ||
|
|
||
| // Sets up an AblyStreamsChannel with the given messenger and triggers a "listen#1" call, | ||
| // which causes the channel to create a sink and pass it to the handler's onListenWithArguments:. | ||
| // Returns the handler so tests can invoke the captured sink. | ||
| - (CapturingSinkHandler*)setupChannelWithMessenger:(MockThrowingMessenger*)messenger { | ||
| AblyStreamsChannel *channel = [AblyStreamsChannel | ||
| streamsChannelWithName:@"test.channel" | ||
| binaryMessenger:messenger | ||
| codec:[FlutterStandardMethodCodec sharedInstance]]; | ||
|
|
||
| CapturingSinkHandler *handler = [CapturingSinkHandler new]; | ||
| [channel setStreamHandlerFactory:^NSObject<FlutterStreamHandler>*(id args) { return handler; }]; | ||
|
|
||
| // Simulate Flutter sending a "listen#1" message to open a stream. | ||
| // Arguments must be non-nil: AblyStreamsChannel stores them in an NSMutableDictionary | ||
| // which rejects nil values. | ||
| NSData *listenMsg = [[FlutterStandardMethodCodec sharedInstance] | ||
| encodeMethodCall:[FlutterMethodCall methodCallWithMethodName:@"listen#1" arguments:@{}]]; | ||
| messenger.capturedHandler(listenMsg, ^(NSData* reply){}); | ||
|
|
||
| return handler; | ||
| } | ||
|
|
||
| // Regression test for: NSInternalInconsistencyException crash when the FlutterEngine is | ||
| // suspended after the app is backgrounded. | ||
| // The sink block in AblyStreamsChannel must catch the exception instead of propagating it. | ||
| - (void)testSinkDoesNotCrashWhenEngineNotRunning { | ||
| MockThrowingMessenger *messenger = [MockThrowingMessenger new]; | ||
| messenger.shouldThrow = YES; | ||
| CapturingSinkHandler *handler = [self setupChannelWithMessenger:messenger]; | ||
|
|
||
| XCTAssertNoThrow(handler.capturedSink(@"state-change-event"), | ||
| @"Sink should swallow NSInternalInconsistencyException when engine is stopped"); | ||
| } | ||
|
|
||
| // Verify the normal (foreground) code path still works after the fix. | ||
| - (void)testSinkEmitsNormallyWhenEngineIsRunning { | ||
| MockThrowingMessenger *messenger = [MockThrowingMessenger new]; | ||
| messenger.shouldThrow = NO; | ||
| CapturingSinkHandler *handler = [self setupChannelWithMessenger:messenger]; | ||
|
|
||
| XCTAssertNoThrow(handler.capturedSink(@"state-change-event")); | ||
| XCTAssertEqual(messenger.sendCallCount, 1, | ||
| @"Messenger should be called once for a normal event emission"); | ||
| } | ||
|
|
||
| @end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.