Skip to content

fix(android): reject instead of crashing when bitmap decoding fails - #224

Draft
retyui wants to merge 2 commits into
masterfrom
fix/android-reject-on-decode-failure
Draft

retyui wants to merge 2 commits into
masterfrom
fix/android-reject-on-decode-failure

Conversation

@retyui

@retyui retyui commented Dec 2, 2026

Copy link
Copy Markdown
Collaborator

K1: crash. ?: throw Error(...) is replaced with JSApplicationIllegalArgumentException, and catch (Exception) with catch (Throwable).

kotlin.Error is java.lang.Error, which is not an Exception. So ?: throw Error("Could not create bitmap decoder…") went straight through catch (e: Exception) into a bare CoroutineScope(Dispatchers.Default) with no CoroutineExceptionHandler. The result was a process crash, and the promise never settled.

Repro: cropImage('https://…/report.pdf'), a GIF/HEIC, or a server that returns an HTML error page with HTTP 200. BitmapRegionDecoder.newInstance returns null and the app crashes. iOS and web reject for the same input. An OutOfMemoryError from decodeRegion escaped the same way.

Promise.reject(Throwable) (1-arg) exists in the 0.79.5 and 0.87.1 AARs. The 2-arg reject(String, String) would not accept an Error.

Verification

Not compiled. No :android:compileDebugKotlin run and nothing run on a device. The Kotlin was checked for syntax errors only, and the external API signatures were checked against the published artifacts.

Split out of #221 (now closed), which bundled six Android fixes in ImageEditorModuleImpl.kt. Each one can be reviewed and merged separately.

Both crop paths signalled an unusable source with `?: throw Error(...)`.
`kotlin.Error` maps to `java.lang.Error`, which is not an `Exception`, so
the throw slipped past the only guard around the crop work
(`catch (e: Exception)`) and escaped `moduleCoroutineScope.launch { }` —
a bare `CoroutineScope(Dispatchers.Default)` with no
CoroutineExceptionHandler. It reached the thread's uncaught handler, so
the app crashed and the promise never settled.

`BitmapRegionDecoder.newInstance` returns null for anything it can't
decode, so this is easy to hit: a non-image URL (`cropImage(url)` on a
PDF, or a server returning an HTML error page with HTTP 200), or a
GIF/HEIC/corrupt file. iOS and web reject for the same input.
`OutOfMemoryError` while decoding large images escaped the same way.

Throw `JSApplicationIllegalArgumentException`, which the JS layer already
surfaces as a normal rejection, and widen the guard to `Throwable` so
errors become rejections too.
retyui added a commit that referenced this pull request Dec 3, 2026
…er invalidate

A crop job never suspends, so cancelling the scope in `invalidate()` doesn't
stop it: it went on to write its file after the cache cleanup had run, and
that file stayed until the next launch. Skip writing once invalidated, and
delete the file if invalidation claimed the promise while it was written.

Build the result map before claiming the promise, so a failure there (e.g.
reading the file back for `includeBase64`) is still rejected instead of
leaving the promise unsettled.

Catch `Throwable` rather than `Exception`: an `Error` (OutOfMemoryError, or
the `throw Error` on a null decoder) escaped `settleOnce` and left the promise
pending. This overlaps #224, which makes the same change.
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.

1 participant