libsu icon indicating copy to clipboard operation
libsu copied to clipboard

StrictMode violation: reading files from the disk in the main thread

Open lihenggui opened this issue 1 year ago • 0 comments

Background

The RootService.bind method has to be called in the main thread, or it will throw an exception:

java.lang.IllegalStateException: This method can only be called on the main thread
at com.topjohnwu.superuser.internal.RootServiceManager.enforceMainThread(RootServiceManager.java:122)
at com.topjohnwu.superuser.internal.RootServiceManager.bindInternal(RootServiceManager.java:248)
at com.topjohnwu.superuser.internal.RootServiceManager.createBindTask(RootServiceManager.java:285)
at com.topjohnwu.superuser.ipc.RootService.bindOrTask(RootService.java:146)
at com.topjohnwu.superuser.ipc.RootService.bind(RootService.java:112)
at com.topjohnwu.superuser.ipc.RootService.bind(RootService.java:126)

But the code violates StrictMode:

StrictMode policy violation; ~duration=10 ms: android.os.strictmode.DiskReadViolation
at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1666)
at libcore.io.BlockGuardOs.access(BlockGuardOs.java:74)
at libcore.io.ForwardingOs.access(ForwardingOs.java:128)
at android.app.ActivityThread$AndroidOs.access(ActivityThread.java:8053)
at java.io.UnixFileSystem.checkAccess(UnixFileSystem.java:313)
at java.io.File.exists(File.java:813)
at android.app.ContextImpl.getDataDir(ContextImpl.java:3196)
at android.app.ContextImpl.getCodeCacheDir(ContextImpl.java:905)
at com.topjohnwu.superuser.internal.Utils.hasStartupAgents(Utils.java:130)
at com.topjohnwu.superuser.internal.RootServiceManager.startRootProcess(RootServiceManager.java:153)
at com.topjohnwu.superuser.internal.RootServiceManager.createBindTask(RootServiceManager.java:292)
at com.topjohnwu.superuser.ipc.RootService.bindOrTask(RootService.java:146)
at com.topjohnwu.superuser.ipc.RootService.bind(RootService.java:112)
at com.topjohnwu.superuser.ipc.RootService.bind(RootService.java:126)

If the application enables the StrictMode, the application will be killed. There is no workaround to fix this issue temporarily.

How to reproduce Enable the StrictMode with detectAll option.

lihenggui avatar Feb 02 '24 23:02 lihenggui