Skip to content

Fix concurrency crash in InternalDatabaseImpl #121

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

Closed

Conversation

benoitletondor
Copy link
Contributor

I'm getting the following crash in production:

          Fatal Exception: java.util.ConcurrentModificationException:
       at java.util.LinkedHashMap$LinkedHashIterator.nextNode(LinkedHashMap.java:760)
       at java.util.LinkedHashMap$LinkedKeyIterator.next(LinkedHashMap.java:782)
       at kotlin.collections.CollectionsKt___CollectionsKt.mapTo(CollectionsKt___Collections.kt:1628)
       at com.powersync.db.internal.InternalDatabaseImpl$1$2.emit(InternalDatabaseImpl.kt:75)
       at com.powersync.db.internal.InternalDatabaseImpl$1$2.emit(InternalDatabaseImpl.kt:74)
       at kotlinx.coroutines.flow.FlowKt__DelayKt$debounceInternal$1$3$1.invokeSuspend(Delay.kt:226)
       at kotlinx.coroutines.flow.FlowKt__DelayKt$debounceInternal$1$3$1.invoke(Delay.kt:11)
       at kotlinx.coroutines.flow.FlowKt__DelayKt$debounceInternal$1$3$1.invoke(Delay.kt:11)
       at kotlinx.coroutines.selects.SelectImplementation$ClauseData.invokeBlock(SelectImplementation.java:843)
       at kotlinx.coroutines.selects.SelectImplementation.complete(Select.kt:715)
       at kotlinx.coroutines.selects.SelectImplementation.doSelectSuspend(Select.kt:456)
       at kotlinx.coroutines.selects.SelectImplementation.access$doSelectSuspend(SelectImplementation.java:251)
       at kotlinx.coroutines.selects.SelectImplementation$doSelectSuspend$1.invokeSuspend(Select.kt:12)
       at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
       at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:100)
       at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.java:586)
       at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:829)
       at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:717)
       at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:704)

This is because the "debounce + accumulate" implem isn't thread safe, and it's totally possible to edit accumulatedUpdates in collect while adding values to it in onEach.

Adding a Mutex should fix this. Note that you can probably monitor Kotlin/kotlinx.coroutines#1302 as it's really what's done here and some, probably more robust, implems are shared

@DominicGBauer
Copy link
Contributor

Thanks a lot for this! Closing this PR as it is included in #120

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.

2 participants