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

[epoxy-paging] IndexOutOfBoundsException #986

Closed
denis-bezrukov opened this issue May 28, 2020 · 1 comment
Closed

[epoxy-paging] IndexOutOfBoundsException #986

denis-bezrukov opened this issue May 28, 2020 · 1 comment

Comments

@denis-bezrukov
Copy link
Contributor

We encountered the following crash in our app:

java.lang.IndexOutOfBoundsException: Index: 81, Size: 81
    at androidx.paging.PagedStorage.get(PagedStorage.java:138)
    at androidx.paging.PagedList.get(PagedList.java:410)
    at com.airbnb.epoxy.paging.PagedListModelCache.getModels(PagedListModelCache.kt:200)
    at com.airbnb.epoxy.paging.PagedListEpoxyController.buildModels(PagedListEpoxyController.kt:74)
    at com.airbnb.epoxy.EpoxyController$1.run(EpoxyController.java:276)
    at android.os.Handler.handleCallback(Handler.java:815)
    at android.os.Handler.dispatchMessage(Handler.java:104)
    at android.os.Looper.loop(Looper.java:207)
    at android.os.HandlerThread.run(HandlerThread.java:61)

After a quick look at the lines from the stacktrace I found that access to modelCache in PagedListModelCache may be non thread safe.

(0 until modelCache.size).forEach { position ->
    if (modelCache[position] == null) { // <- crash here, so it's concurrent modification issue
        modelCache[position] = modelBuilder(position, currentList[position])
    }
}

functions that accesses to modelCache are marked by @Synchronized but synchronization performed on different objects:
getModels is synchronized on PagedListModelCache instance while onRemoved is synchronized on updateCallback object. So there is no synchronization between these two functions.

Fix will be to use the same monitor object for synchronization these functions. e.g. by making

override fun onRemoved(position: Int, count: Int) = synchronized(this@PagedListModelCache) {
    assertUpdateCallbacksAllowed()
    (0 until count).forEach {
        modelCache.removeAt(position)
     }
     rebuildCallback()
}
@elihart
Copy link
Contributor

elihart commented May 28, 2020

thanks for the report! are you interested in putting up the fix?

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

No branches or pull requests

2 participants