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

WIP: Fix ANR caused by operationRepo.enqueue while loading is in progress #2231

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -169,33 +169,34 @@ abstract class ModelStore<TModel>(

val str = _prefs.getString(PreferenceStores.ONESIGNAL, PreferenceOneSignalKeys.MODEL_STORE_PREFIX + name, "[]")
val jsonArray = JSONArray(str)
synchronized(models) {
val shouldRePersist = models.isNotEmpty()
for (index in jsonArray.length() - 1 downTo 0) {
val newModel = create(jsonArray.getJSONObject(index)) ?: continue

/*
* NOTE: Migration fix for bug introduced in 5.1.12
* The following check is intended for the operation model store.
* When the call to this method moved out of the operation model store's initializer,
* duplicate operations could be cached.
* See https://github.com/OneSignal/OneSignal-Android-SDK/pull/2099
*/
val hasExisting = models.any { it.id == newModel.id }
if (hasExisting) {
Logging.debug("ModelStore<$name>: load - operation.id: ${newModel.id} already exists in the store.")
continue
}

models.add(0, newModel)
// listen for changes to this model
newModel.subscribe(this)
val shouldRePersist = models.isNotEmpty()
for (index in jsonArray.length() - 1 downTo 0) {
val newModel = create(jsonArray.getJSONObject(index)) ?: continue

/*
* NOTE: Migration fix for bug introduced in 5.1.12
* The following check is intended for the operation model store.
* When the call to this method moved out of the operation model store's initializer,
* duplicate operations could be cached.
* See https://github.com/OneSignal/OneSignal-Android-SDK/pull/2099
*/
val hasExisting = models.any { it.id == newModel.id }
if (hasExisting) {
Logging.debug("ModelStore<$name>: load - operation.id: ${newModel.id} already exists in the store.")
continue
}
hasLoadedFromCache = true
// optimization only: to avoid unnecessary writes
if (shouldRePersist) {
persist()

synchronized(models) {
models.add(0, newModel)
}
// listen for changes to this model
newModel.subscribe(this)
}
hasLoadedFromCache = true
// optimization only: to avoid unnecessary writes
if (shouldRePersist) {
persist()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,41 @@ import java.util.UUID

class ModelingTests : FunSpec({

test("ensure prolonged loading in the background thread does not block insertion in the main thread") {
// Given
val prefs = MockPreferencesService()
val operationModelStore = OperationModelStore(prefs)

// add an arbitrary operation to the cache
val cachedOperation = LoginUserFromSubscriptionOperation()
val newOperation = LoginUserOperation()
cachedOperation.id = UUID.randomUUID().toString()
val jsonArray = JSONArray()
jsonArray.put(cachedOperation.toJSON())
prefs.saveString(PreferenceStores.ONESIGNAL, PreferenceOneSignalKeys.MODEL_STORE_PREFIX + "operations", jsonArray.toString())

// simulate a background thread to load operations
val backgroundThread =
Thread {
operationModelStore.loadOperations()
}

val mainThread =
Thread {
operationModelStore.add(newOperation)
}

backgroundThread.start()
mainThread.start()

mainThread.join(100)

// Then
// insertion from the main thread is done without blocking
operationModelStore.list().count() shouldBe 1
operationModelStore.list().first() shouldBe newOperation
}

test("Deadlock related to Model.setOptAnyProperty") {
// Given
val modelStore = MockHelper.configModelStore()
Expand Down
Loading