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

Recently after every upload I get "IllegalStateException: Cannot access database on the main thread since it may potentially lock the UI for a long period of time." #5898

Closed
Tracked by #5715
nicolas-raoul opened this issue Oct 26, 2024 · 9 comments · Fixed by #5902 or #5914
Assignees
Milestone

Comments

@nicolas-raoul
Copy link
Member

nicolas-raoul commented Oct 26, 2024

It started this week, probably caused by a recent commit?

Tasks:

  1. Reproduce the issue
  2. Find since what commit it started (use git bisect)
  3. Either revert the commit or improve it.

If you can upload without any such issue please let me know. Thanks! :-)

java.lang.IllegalStateException: Cannot access database on the main thread since it may potentially lock the UI for a long period of time.
at androidx.room.RoomDatabase.assertNotMainThread(RoomDatabase.kt:435)
at androidx.room.RoomDatabase.beginTransaction(RoomDatabase.kt:505)
at fr.free.nrw.commons.customselector.database.UploadedStatusDao_Impl.insert(UploadedStatusDao_Impl.java:120)
at fr.free.nrw.commons.customselector.database.UploadedStatusDao.insertUploaded(UploadedStatusDao.kt:51)
at fr.free.nrw.commons.upload.worker.UploadWorker$saveIntoUploadedStatus$1$1.invokeSuspend(UploadWorker.kt:538)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:108)
at android.os.Handler.handleCallback(Handler.java:991)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loopOnce(Looper.java:232)
at android.os.Looper.loop(Looper.java:317)
at android.app.ActivityThread.main(ActivityThread.java:8787)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:591)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:871)
Suppressed: kotlinx.coroutines.internal.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@7cb3322, Dispatchers.Main]
@rohit9625
Copy link
Contributor

I want to take this one. Well, I am experiencing this error when I tried to build the app using the latest Android Studio. Do you have any idea about this?

image

@rohit9625
Copy link
Contributor

When I try to open the custom selector and open a folder, I get this crash:

Screenshot from 2024-10-26 20-54-40

I guess the issue is with accessing database. I am looking into it.

@rohit9625
Copy link
Contributor

This commit removes the suspend keyword from UploadStatusDao making it blocking functions, i.e., it'll block the thread from where these functions will be called. In ImageLoader and UploadWorker, one of the DAO's methods was called from MainScope() that's why we are facing that error.
However, I added the suspend keyword again and removed the MainScope with CoroutineScope.

I will link a PR once assigned :)

@RitikaPahwa4444
Copy link
Collaborator

Hey @rohit9625, the commit removes suspend keyword from DepictsDao as well. Did you notice any issues because of that too during testing?

@rohit9625
Copy link
Contributor

rohit9625 commented Oct 27, 2024

Sorry, I didn't notice that before. The changes are in the PlacesDao also. However, in the Depicts Dao, the function calls to the database is handled like this:

private suspend fun deleteDepicts(depicts: Depicts) =
        CoroutineScope(Dispatchers.IO).launch {
            delete(depicts)
        }

But, I need to cross-check for usages of functions in DepictsDao. Or should revert the changes because those functions are meant to be thread-safe? Currently, they are blocking the IO thread.

@rohit9625
Copy link
Contributor

Okay, I will revert the changes. Since there's no sense in using CoroutineScope(Dispatcher. IO) in the context of DepictsDao. Because, if some other operation is executing on the IO thread and somehow we perform a task that executes the functions from DepictsDao, then it'll affect the execution of other tasks on the thread.

Thank you for pointing out @RitikaPahwa4444

@sivaraam
Copy link
Member

Reopening as the fix in MR #5902 seems incomplete according to this comment.

@sivaraam sivaraam reopened this Oct 28, 2024
@sivaraam sivaraam mentioned this issue Oct 28, 2024
8 tasks
@sivaraam sivaraam added this to the 5.0.x milestone Oct 28, 2024
@rohit9625
Copy link
Contributor

Hey @nicolas-raoul, should I create a new PR, or will pushing commits to the existing PR's branch be sufficient?

@nicolas-raoul
Copy link
Member Author

Either is fine. 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment