Skip to content
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

Call SkiaLayerComponent.onComposeInvalidation() only from the event dispatch thread #1288

Merged
merged 1 commit into from
Apr 18, 2024

Conversation

m-sasha
Copy link
Member

@m-sasha m-sasha commented Apr 18, 2024

Currently, ComposeSceneMediator.onComposeInvalidation() calls SkiaLayerComponent.onComposeInvalidation() directly. However, ComposeSceneMediator.onComposeInvalidation() is not guaranteed to be called from the event dispatch thread, whereas SkiaLayerComponent.onComposeInvalidation() must be called from the event dispatch thread. When it's called from another thread, it throws an exception.

RelNote: Fix crash when modifying Compose state from a non-UI thread.

Proposed Changes

Make ComposeSceneMediator.onComposeInvalidation() call SkiaLayerComponent.onComposeInvalidation() on the event dispatch thread only.

Testing

Test: Added a unit test, and also tested manually with the following:

var value by mutableStateOf(0)

fun main() = singleWindowApplication {
    val textMeasurer = rememberTextMeasurer()
    Canvas(Modifier.size(100.dp)) {
        drawText(textMeasurer, "$value")
    }
    LaunchedEffect(Unit) {
        withContext(Dispatchers.IO) {
            for (i in 1..5_000) {
                value = i
                Snapshot.sendApplyNotifications()
                delay((1..20).random().toLong())
            }
        }
    }
}

Issues Fixed

Fixes: JetBrains/compose-multiplatform#4546

@m-sasha m-sasha requested a review from igordmn April 18, 2024 18:29
@m-sasha m-sasha force-pushed the m-sasha/invalidate-compose-from-awt-thread-only branch from 3da225b to e7e3ca1 Compare April 18, 2024 19:45
@m-sasha m-sasha requested a review from igordmn April 18, 2024 19:46
@m-sasha m-sasha merged commit 29ee4d3 into jb-main Apr 18, 2024
6 checks passed
@m-sasha m-sasha deleted the m-sasha/invalidate-compose-from-awt-thread-only branch April 18, 2024 22:39
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.

IllegalStateException accessing state in non-AWT thread
3 participants