Skip to content

Keep bridge and adapter errors from crashing the app or hanging calls - #5

Merged
gartnera merged 2 commits into
mainfrom
claude/serene-fermat-qq257h
Nov 9, 2026
Merged

gartnera merged 2 commits into
mainfrom
claude/serene-fermat-qq257h

Conversation

@gartnera

@gartnera gartnera commented Nov 9, 2026

Copy link
Copy Markdown
Member

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.onError when 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() throws IllegalArgumentException("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.
  • Building the engine throws UnsatisfiedLinkError when the native library fails to load. The old catch (e: Exception) missed it because it's an Error.

Changes:

  • Tasks now run through execute(), which logs the exception and fails the call it concerns.
  • A call that fails before it starts is reported as notSent, so retrying it is safe.
  • Streams are registered only after start() succeeds.
  • Empty writes are skipped.
  • A refused engine shutdown is logged instead of thrown.
  • Malformed method calls get an error reply instead of a cast exception on the main thread.

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!), and http3Origins pairs were indexed without a bounds check, so a malformed call crashed the app. They're now checked, and a bad call gets a badArguments FlutterError.

Dart (channel_http_client.dart)

  • An exception thrown by onResponse, or a malformed event, escaped the event listener and left its call waiting forever. Now a malformed event fails its own call with internal. An onResponse exception goes to FlutterError.onError and the call carries on.
  • A refused open used to throw a raw PlatformException and keep the call registered. It now fails the call as notSent.
  • A refused write fails the call with internal.
  • A refused cancel ends the call on the Dart side, since no canceled event will ever come.
  • Error reports name the event type and call id, not the whole event, so request and response data stay out of crash reporters. The Android bad-arguments log line doesn't print the arguments either, since they include header values.

Tests

  • Six new Dart unit tests. Each one fails against the previous code.
  • Android instrumented tests for a rejected header, an empty write, and malformed arguments.
  • An XCTest for malformed arguments.
  • The XCTest helper now fails a test on an error reply instead of skipping it.

Generated by Claude Code

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
@gartnera
gartnera enabled auto-merge (squash) November 9, 2026 21:38
@gartnera
gartnera merged commit c048468 into main Nov 9, 2026
6 checks passed
@gartnera
gartnera deleted the claude/serene-fermat-qq257h branch November 9, 2026 21:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants