Repository navigation
Keep bridge and adapter errors from crashing the app or hanging calls - #5
Merged
Merged
Conversation
Android: every bridge task runs on a plain executor thread, where an uncaught exception kills the process. Cronet throws there on a header value it rejects (start(), "Invalid header"), on an empty write before the end of the stream, and, as an UnsatisfiedLinkError that `catch (e: Exception)` missed, when its native library fails to load. Tasks now run through execute(), which logs and fails the call concerned: a call that never started is reported as never sent. Streams are registered only once start() succeeds, empty writes are skipped, a failed engine shutdown is logged, and malformed method calls get an error reply instead of a cast exception on the main thread. Cronet already catches exceptions thrown from its own callbacks. iOS: method arguments were force-cast (as!), and http3Origins pairs were indexed without a bounds check, so a malformed call crashed. They are now checked and refused with a badArguments FlutterError. Dart: an exception from onResponse, or a malformed event, escaped the event listener and left its call unfinished forever. A malformed event now fails its own call (Code.internal); onResponse errors and other errors with no caller go to FlutterError.onError and the call goes on. A refused open fails the call as notSent instead of throwing a raw PlatformException and leaking the call, a refused write fails it with Code.internal, and a refused cancel ends the call locally, since no canceled event will come. Tests: six Dart unit tests (each fails against the previous code), Android instrumented tests for a rejected header, an empty write and malformed arguments, and an XCTest for malformed arguments. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BqmpXULCM6eTJFFsWD6xfQ
Review fixes for the previous commit: - CronetBridge.handle's reply lambda inferred (Reply) -> Boolean from Handler.post, and dispatch() takes (Reply) -> Unit, so the plugin didn't compile (CI run 42702978828). It is typed explicitly now. - The bad-arguments log line printed the whole argument map, which for open includes request header values such as Authorization. It logs the method and exception only. - reportError for a malformed event stringified the event, up to 32 KiB of body bytes plus response headers, into FlutterError.onError and so into crash reporters. It names the event type and call instead. - The README promised nothing would crash the app; the bridge's guard catches exceptions, not Errors such as OutOfMemoryError, so it says exceptions now. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01BqmpXULCM6eTJFFsWD6xfQ
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Before this change, several inputs or platform failures could crash the app or leave a Connect call waiting forever. Now each one fails only the call it concerns, or is reported through
FlutterError.onErrorwhen there's no caller to hand it to.Android (
CronetBridge.kt)Every bridge task runs on a plain executor thread, and an uncaught exception there kills the process. Cronet throws on that thread in three known cases:
start()throwsIllegalArgumentException("Invalid header…")for a header value Cronet rejects, such as a token with a line break.write()throws on an empty chunk that isn't the end of the stream.UnsatisfiedLinkErrorwhen the native library fails to load. The oldcatch (e: Exception)missed it because it's anError.Changes:
execute(), which logs the exception and fails the call it concerns.notSent, so retrying it is safe.start()succeeds.Cronet already catches exceptions thrown inside its own callbacks (confirmed in
CronetBidirectionalStream.java), so those need no change.iOS (
URLSessionBridge.swift)Method arguments were force-cast (
as!), andhttp3Originspairs were indexed without a bounds check, so a malformed call crashed the app. They're now checked, and a bad call gets abadArgumentsFlutterError.Dart (
channel_http_client.dart)onResponse, or a malformed event, escaped the event listener and left its call waiting forever. Now a malformed event fails its own call withinternal. AnonResponseexception goes toFlutterError.onErrorand the call carries on.openused to throw a rawPlatformExceptionand keep the call registered. It now fails the call asnotSent.writefails the call withinternal.cancelends the call on the Dart side, since nocanceledevent will ever come.Tests
Generated by Claude Code