Repository navigation
fix(android): keep image attachment state writes on the UI thread - #295
Conversation
AidenAttachmentImage's LaunchedEffect decodes on Dispatchers.Default and then writes `bitmap`/`failed` in the continuation. It relied on the host effect dispatcher to hop back to the main thread. The Compose test rule's effect dispatcher is an UnconfinedTestDispatcher, so the continuation resumed on a DefaultDispatcher worker. The rule's ApplyingContinuationInterceptor then sent snapshot apply notifications from that worker, which let recomposition/measure run off the main thread while the UI thread was drawing. The result was an intermittent "performMeasureAndLayout called during measure layout" crash in AidenImageCarouselUiTest. Run the effect body under Dispatchers.Main.immediate, which is a no-op in production where effects already run on the UI thread, so the load and decode still happen off-main but the state writes happen on it. Apply the same pinning to the gallery save coroutine, which toggles `saving` and shows a Toast after an IO hop. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Hermes Review BotConfidence: 5 Engine: SummaryThis change pins Compose state mutations and UI notifications in Android image attachment handling to the main UI thread. In Confidence Score: 5/5Fully traced the coroutine resumption paths and Compose snapshot threading behavior across 📁 Important Files Changed
FindingsNo findings. Sequence DiagramUnchanged or not applicable for this change. []
|
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes This initial review covers the Android image-load and gallery-save dispatcher changes.
- Image-load state updates The load/decode continuation returns to
Dispatchers.Main.immediatebefore updating Compose state, while the existing IO and Default dispatcher hops remain intact. - Gallery save state updates The save coroutine runs on
Dispatchers.Main.immediate, keeping the post-IO state update and Toast on the UI thread.
GPT Luna | 𝕏

Flake
AidenImageCarouselUiTest > cardDeckSwipesOneImageAndOpensTheSelectedGalleryPagefailed once on hosted CI withIllegalArgumentException: performMeasureAndLayout called during measure layoutand passed on rerun:https://github.com/sambitcreate/aiden-agent/actions/runs/36656165396/job/109700927204
Root cause
I reproduced it locally on an API 36 emulator (about 1 in 40 single-test runs). The full trace shows the UI thread's ordinary
Choreographer → ViewRootImpl.performDraw → AndroidComposeView.dispatchDraw → measureAndLayoutfindingduringMeasureLayoutalready set. That flag is cleared in afinally, so a second thread was inside a measure/layout pass on the same root. In the captured run, the crash happened during the firstcaptureToImage, while the deck's images were still decoding.AidenAttachmentImage'sLaunchedEffectdecodes onDispatchers.Defaultand then writes thebitmap/failedstate in the continuation. It relied on the host effect dispatcher to bring it back to the main thread. That holds in production (AndroidUiDispatcher), but the Compose test rule (v1, ui-test 1.10.6) uses anUnconfinedTestDispatcherfor effects. The logs confirmed that the continuation resumed onDefaultDispatcher-worker-N. The rule'sApplyingContinuationInterceptorthen sends snapshot apply notifications from that worker, and that lets recomposition and measure/layout run off the main thread, racing the UI thread's draw.Fix (production code)
withContext(Dispatchers.Main.immediate). Loading and decoding still hop to background dispatchers, but the continuation and the state writes always land on the UI thread. In production this changes nothing, because effects already run on the main thread andimmediatedoesn't dispatch.savingand shows aToastafter an IO hop.The test itself is unchanged: no retries, sleeps, timeouts, or ignores.
Verification
main. The single test then passed 150 of 150 runs with the emulator's CPU under extra load. The fullconnectedDebugAndroidTestsuite also passed (27 tests, 0 failures).:app:testDebugUnitTestpasses.🤖 Generated with Claude Code