damontecres / StashAppAndroidTV

Android TV App for Stash
GNU Affero General Public License v3.0
74 stars 4 forks source link

Fix race condition when adding tags/performers to a scene #257

Closed damontecres closed 3 months ago

damontecres commented 3 months ago

In some cases, a race condition can occur after adding a performer or tag to scene.

While, the onActivityResult is called before onResume, it is possible that the scene data fetched in onResume happens before the mutation in onActivityResult completes. And then the UI updates in onActivityResult occurs before the ones in onResume. This is possible because network calls and UI updates occurs in different coroutines. In this surprisingly common case, the scene is successfully modified, but the UI doesn't reflect the changes.

This PR fixes that race condition by optionally allowing a QueryEngine and MutationEngine to share a ReadWriteLock allowing the mutations to block queries when needed.

Since this is a concurrency issue, it requires thorough testing and extra scrutiny.