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

Do not execute blocking operations in the main thread in Downloads #1636

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

svillar
Copy link
Member

@svillar svillar commented Nov 19, 2024

This PR fixes 2 strict mode policy violations in the downloads manager code

  • querying the list of downloads might involve on disk sql operations
  • removing downloads might involve on disk read/write operations

We should not perform blocking operations in the main thread. Use an executor do run them in a separate disk IO thread.

This PR sits on top of #1632 and #1635

StrictMode is most commonly used to catch accidental disk or network access on the
application's main thread, where UI operations are received and animations take
place. Keeping disk and network operations off the main thread makes for much
smoother, more responsive applications[1].

[1] https://developer.android.com/reference/android/os/StrictMode
SettingsStore used to call editor.commit() to apply the value of
a setting and write it to disk. That should not be done in the main
thread though as it's a blocking operation. It's better to use
apply() which atomically modifies the SharedPreferences object in
memory and then uses a background thread to write it to disk.

It's safe to replace commit() by apply() as long as the return
value is not used, which is the case.

This fixes reports like:
D  StrictMode policy violation; ~duration=3 ms: android.os.strictmode.DiskWriteViolation
at android.os.StrictMode$AndroidBlockGuardPolicy.onWriteToDisk(StrictMode.java:1615)
at libcore.io.BlockGuardOs.write(BlockGuardOs.java:446)
at libcore.io.ForwardingOs.write(ForwardingOs.java:951)
at libcore.io.IoBridge.write(IoBridge.java:649)
at java.io.FileOutputStream.write(FileOutputStream.java:401)
at com.android.internal.util.FastXmlSerializer.flushBytes(FastXmlSerializer.java:253)
at com.android.internal.util.FastXmlSerializer.flush(FastXmlSerializer.java:274)
at com.android.internal.util.FastXmlSerializer.endDocument(FastXmlSerializer.java:219)
at com.android.internal.util.XmlSerializerWrapper.endDocument(XmlSerializerWrapper.java:68)
at com.android.internal.util.XmlUtils.writeMapXml(XmlUtils.java:408)
at android.app.SharedPreferencesImpl.writeToFile(SharedPreferencesImpl.java:803)
at android.app.SharedPreferencesImpl.access$900(SharedPreferencesImpl.java:59)
at android.app.SharedPreferencesImpl$2.run(SharedPreferencesImpl.java:672)
at android.app.SharedPreferencesImpl.enqueueDiskWrite(SharedPreferencesImpl.java:691)
at android.app.SharedPreferencesImpl.access$100(SharedPreferencesImpl.java:59)
at android.app.SharedPreferencesImpl$EditorImpl.commit(SharedPreferencesImpl.java:604)
at com.igalia.wolvic.browser.SettingsStore.resetCrashRestartCount(SettingsStore.java:766)
at com.igalia.wolvic.VRBrowserActivity.lambda$onCreate$0$com-igalia-wolvic-VRBrowserActivity(VRBrowserActivity.java:296)
at com.igalia.wolvic.VRBrowserActivity$$ExternalSyntheticLambda3.run(D8$$SyntheticClass:0)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:214)
at android.os.Looper.loop(Looper.java:304)
at android.app.ActivityThread.main(ActivityThread.java:7918)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1010)
Quering the list of downloads might involve SQL queries that can
write to a file. That's a blocking operation that should not happen
in the main thread.

Instead get the diskIO executor in the constructor and use it to
run the query. Note that notifications to listeners should still
happen in the main thread.

This fixes this report:
D  StrictMode policy violation; ~duration=38 ms: android.os.strictmode.DiskWriteViolation
at android.os.StrictMode$AndroidBlockGuardPolicy.onWriteToDisk(StrictMode.java:1615)
at android.database.sqlite.SQLiteConnection.applyBlockGuardPolicy(SQLiteConnection.java:1201)
at android.database.sqlite.SQLiteConnection.executeForString(SQLiteConnection.java:794)
at android.database.sqlite.SQLiteConnection.setJournalMode(SQLiteConnection.java:407)
at android.database.sqlite.SQLiteConnection.setWalModeFromConfiguration(SQLiteConnection.java:347)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:260)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:205)
at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:505)
at android.database.sqlite.SQLiteConnectionPool.tryAcquireNonPrimaryConnectionLocked(SQLiteConnectionPool.java:989)
at android.database.sqlite.SQLiteConnectionPool.waitForConnection(SQLiteConnectionPool.java:695)
at android.database.sqlite.SQLiteConnectionPool.acquireConnection(SQLiteConnectionPool.java:380)
at android.database.sqlite.SQLiteSession.acquireConnection(SQLiteSession.java:896)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteDatabase.validateSql(SQLiteDatabase.java:1945)
at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:573)
at android.database.sqlite.SQLiteQueryBuilder.query(SQLiteQueryBuilder.java:463)
at com.android.providers.downloads.DownloadProvider.query(DownloadProvider.java:1270)
at android.content.ContentProvider.query(ContentProvider.java:1408)
at android.content.ContentProvider.query(ContentProvider.java:1504)
at android.content.ContentProvider$Transport.query(ContentProvider.java:272)
at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:107)
at android.os.Binder.execTransactInternal(Binder.java:1179)
at android.os.Binder.execTransact(Binder.java:1143)
at android.os.StrictMode.readAndHandleBinderCallViolations(StrictMode.java:2497)
at android.os.Parcel.readExceptionCode(Parcel.java:2371)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:139)
at android.content.ContentProviderProxy.query(ContentProviderNative.java:481)
at android.content.ContentResolver.query(ContentResolver.java:1219)
at android.content.ContentResolver.query(ContentResolver.java:1151)
at android.content.ContentResolver.query(ContentResolver.java:1107)
at android.app.DownloadManager$Query.runQuery(DownloadManager.java:1019)
at android.app.DownloadManager.query(DownloadManager.java:1165)
at android.app.DownloadManager.query(DownloadManager.java:1160)
at com.igalia.wolvic.downloads.DownloadsManager.getDownloads(DownloadsManager.java:281)
at com.igalia.wolvic.downloads.DownloadsManager.notifyDownloadsUpdate(DownloadsManager.java:313)
at com.igalia.wolvic.downloads.DownloadsManager.$r8$lambda$0HiP7vssHY38l68BiKxs3V29Nug(Unknown Source:0)
at com.igalia.wolvic.downloads.DownloadsManager$$ExternalSyntheticLambda0.run(D8$$SyntheticClass:0)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:214)
at android.os.Looper.loop(Looper.java:304)
at android.app.ActivityThread.main(ActivityThread.java:7918)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1010)
Removing a download involves on disk reading/writing operations.
Those are blocking operations that should not happen in the main
thread. Use the recently added disk executor to carry on the task.

This fixes this report:
D  StrictMode policy violation; ~duration=102 ms: android.os.strictmode.DiskReadViolation
at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1659)
at libcore.io.BlockGuardOs.access(BlockGuardOs.java:74)
at libcore.io.ForwardingOs.access(ForwardingOs.java:131)
at android.app.ActivityThread$AndroidOs.access(ActivityThread.java:7795)
at java.io.UnixFileSystem.checkAccess(UnixFileSystem.java:281)
at java.io.File.exists(File.java:813)
at android.app.ContextImpl.ensurePrivateDirExists(ContextImpl.java:751)
at android.app.ContextImpl.ensurePrivateDirExists(ContextImpl.java:742)
at android.app.ContextImpl.getFilesDir(ContextImpl.java:787)
at com.android.providers.downloads.Helpers.isFilenameValid(Helpers.java:665)
at com.android.providers.downloads.Helpers.isFilenameValid(Helpers.java:527)
at com.android.providers.downloads.DownloadProvider.delete(DownloadProvider.java:1629)
at android.content.ContentProvider.delete(ContentProvider.java:1785)
at android.content.ContentProvider$Transport.delete(ContentProvider.java:430)
at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:215)
at android.os.Binder.execTransactInternal(Binder.java:1179)
at android.os.Binder.execTransact(Binder.java:1143)
at android.os.StrictMode.readAndHandleBinderCallViolations(StrictMode.java:2497)
at android.os.Parcel.readExceptionCode(Parcel.java:2371)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:139)
at android.content.ContentProviderProxy.delete(ContentProviderNative.java:629)
at android.content.ContentResolver.delete(ContentResolver.java:2333)
at android.content.ContentResolver.delete(ContentResolver.java:2299)
at android.app.DownloadManager.markRowDeleted(DownloadManager.java:1138)
at android.app.DownloadManager.remove(DownloadManager.java:1150)
at com.igalia.wolvic.downloads.DownloadsManager.removeDownload(DownloadsManager.java:250)
at com.igalia.wolvic.utils.EnvironmentsManager$1.onUnzipFinish(EnvironmentsManager.java:193)
at com.igalia.wolvic.utils.zip.UnzipResultReceiver.lambda$onReceiveResult$0(UnzipResultReceiver.java:59)
at com.igalia.wolvic.utils.zip.UnzipResultReceiver$$ExternalSyntheticLambda0.accept(D8$$SyntheticClass:0)
at java.util.ArrayList.forEach(ArrayList.java:1262)
at com.igalia.wolvic.utils.zip.UnzipResultReceiver.onReceiveResult(UnzipResultReceiver.java:50)
at android.os.ResultReceiver$MyRunnable.run(ResultReceiver.java:50)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:214)
at android.os.Looper.loop(Looper.java:304)
at android.app.ActivityThread.main(ActivityThread.java:7918)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1010)
@svillar svillar changed the title Do not remove downloads on main thread Do not execute blocking operations in the main thread in Downloads Nov 19, 2024
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.

1 participant