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

StrictMode violation in ShellSectionRenderer.OnCreateView() leading to 8-second blocking during startup #19415

Closed
BioTurboNick opened this issue Dec 14, 2023 · 6 comments
Labels
area-controls-shell Shell Navigation, Routes, Tabs, Flyout platform/android 🤖 s/needs-attention Issue has more information and needs another look t/bug Something isn't working

Comments

@BioTurboNick
Copy link
Contributor

BioTurboNick commented Dec 14, 2023

Description

When investigating ANRs in my app, I turned on StrictMode violation logging. I found that ShellSectionRenderer.OnCreateView() is accessing the Android PreferenceManager, which results in a series of disk reads and writes (here, nearly 2 s each) during startup.

Steps to Reproduce

No response

Link to public reproduction project repository

No response

Version with bug

8.0.3

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android 13

Did you find any workaround?

No response

Relevant log output

[StrictMode] StrictMode policy violation; ~duration=1756 ms: android.os.strictmode.DiskReadViolation
[StrictMode] 	at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1658)
[StrictMode] 	at libcore.io.BlockGuardOs.access(BlockGuardOs.java:74)
[StrictMode] 	at libcore.io.ForwardingOs.access(ForwardingOs.java:128)
[StrictMode] 	at android.app.ActivityThread$AndroidOs.access(ActivityThread.java:8615)
[StrictMode] 	at java.io.UnixFileSystem.checkAccess(UnixFileSystem.java:313)
[StrictMode] 	at java.io.File.exists(File.java:813)
[StrictMode] 	at android.app.ContextImpl.getDataDir(ContextImpl.java:3136)
[StrictMode] 	at android.app.ContextImpl.getPreferencesDir(ContextImpl.java:735)
[StrictMode] 	at android.app.ContextImpl.getSharedPreferencesPath(ContextImpl.java:975)
[StrictMode] 	at android.app.ContextImpl.getSharedPreferences(ContextImpl.java:573)
[StrictMode] 	at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:226)
[StrictMode] 	at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:545)
[StrictMode] 	at crc640ec207abc449b2ca.ShellSectionRenderer.n_onCreateView(Native Method)
[StrictMode] 	at crc640ec207abc449b2ca.ShellSectionRenderer.onCreateView(ShellSectionRenderer.java:44)
[StrictMode] 	at androidx.fragment.app.Fragment.performCreateView(Fragment.java:3114)
[StrictMode] 	at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:557)
[StrictMode] 	at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:272)
[StrictMode] 	at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1943)
[StrictMode] 	at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1845)
[StrictMode] 	at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1782)
[StrictMode] 	at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3042)
[StrictMode] 	at androidx.fragment.app.FragmentManager.dispatchViewCreated(FragmentManager.java:2945)
[StrictMode] 	at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:3148)
[StrictMode] 	at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:588)
[StrictMode] 	at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:272)
[StrictMode] 	at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1943)
[StrictMode] 	at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1845)
[StrictMode] 	at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1782)
[StrictMode] 	at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager.java:3042)
[StrictMode] 	at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:2952)
[StrictMode] 	at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:263)
[StrictMode] 	at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:350)
[StrictMode] 	at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:251)
[StrictMode] 	at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1510)
[StrictMode] 	at android.app.Activity.performStart(Activity.java:8616)
[StrictMode] 	at android.app.ActivityThread.handleStartActivity(ActivityThread.java:4204)
[StrictMode] 	at android.app.servertransaction.TransactionExecutor.performLifecycleSequence(TransactionExecutor.java:221)
[StrictMode] 	at android.app.servertransaction.TransactionExecutor.cycleToPath(TransactionExecutor.java:201)
[StrictMode] 	at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:173)
[StrictMode] 	at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
[StrictMode] 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2574)
[StrictMode] 	at android.os.Handler.dispatchMessage(Handler.java:106)
[StrictMode] 	at android.os.Looper.loopOnce(Looper.java:226)
[StrictMode] 	at android.os.Looper.loop(Looper.java:313)
[StrictMode] 	at android.app.ActivityThread.main(ActivityThread.java:8762)
[StrictMode] 	at java.lang.reflect.Method.invoke(Native Method)
[StrictMode] 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:604)
[StrictMode] 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)
[StrictMode] StrictMode policy violation; ~duration=1756 ms: android.os.strictmode.DiskReadViolation
[StrictMode] 	at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1658)
[StrictMode] 	at libcore.io.BlockGuardOs.access(BlockGuardOs.java:74)
[StrictMode] 	at libcore.io.ForwardingOs.access(ForwardingOs.java:128)
[StrictMode] 	at android.app.ActivityThread$AndroidOs.access(ActivityThread.java:8615)
[StrictMode] 	at java.io.UnixFileSystem.checkAccess(UnixFileSystem.java:313)
[StrictMode] 	at java.io.File.exists(File.java:813)
[StrictMode] 	at android.app.ContextImpl.ensurePrivateDirExists(ContextImpl.java:790)
[StrictMode] 	at android.app.ContextImpl.ensurePrivateDirExists(ContextImpl.java:781)
[StrictMode] 	at android.app.ContextImpl.getPreferencesDir(ContextImpl.java:737)
[StrictMode] 	at android.app.ContextImpl.getSharedPreferencesPath(ContextImpl.java:975)
[StrictMode] 	at android.app.ContextImpl.getSharedPreferences(ContextImpl.java:573)
[StrictMode] 	at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:226)
[StrictMode] 	at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:545)
[StrictMode] 	at crc640ec207abc449b2ca.ShellSectionRenderer.n_onCreateView(Native Method)
[StrictMode] 	at crc640ec207abc449b2ca.ShellSectionRenderer.onCreateView(ShellSectionRenderer.java:44)
...
[StrictMode] StrictMode policy violation; ~duration=1756 ms: android.os.strictmode.DiskWriteViolation
[StrictMode] 	at android.os.StrictMode$AndroidBlockGuardPolicy.onWriteToDisk(StrictMode.java:1614)
[StrictMode] 	at libcore.io.BlockGuardOs.mkdir(BlockGuardOs.java:255)
[StrictMode] 	at libcore.io.ForwardingOs.mkdir(ForwardingOs.java:491)
[StrictMode] 	at android.system.Os.mkdir(Os.java:459)
[StrictMode] 	at android.app.ContextImpl.ensurePrivateDirExists(ContextImpl.java:793)
[StrictMode] 	at android.app.ContextImpl.ensurePrivateDirExists(ContextImpl.java:781)
[StrictMode] 	at android.app.ContextImpl.getPreferencesDir(ContextImpl.java:737)
[StrictMode] 	at android.app.ContextImpl.getSharedPreferencesPath(ContextImpl.java:975)
[StrictMode] 	at android.app.ContextImpl.getSharedPreferences(ContextImpl.java:573)
[StrictMode] 	at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:226)
[StrictMode] 	at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:545)
[StrictMode] 	at crc640ec207abc449b2ca.ShellSectionRenderer.n_onCreateView(Native Method)
[StrictMode] 	at crc640ec207abc449b2ca.ShellSectionRenderer.onCreateView(ShellSectionRenderer.java:44)
...
[StrictMode] StrictMode policy violation; ~duration=1755 ms: android.os.strictmode.DiskWriteViolation
[StrictMode] 	at android.os.StrictMode$AndroidBlockGuardPolicy.onWriteToDisk(StrictMode.java:1614)
[StrictMode] 	at libcore.io.BlockGuardOs.chmod(BlockGuardOs.java:81)
[StrictMode] 	at libcore.io.ForwardingOs.chmod(ForwardingOs.java:165)
[StrictMode] 	at android.system.Os.chmod(Os.java:119)
[StrictMode] 	at android.app.ContextImpl.ensurePrivateDirExists(ContextImpl.java:794)
[StrictMode] 	at android.app.ContextImpl.ensurePrivateDirExists(ContextImpl.java:781)
[StrictMode] 	at android.app.ContextImpl.getPreferencesDir(ContextImpl.java:737)
[StrictMode] 	at android.app.ContextImpl.getSharedPreferencesPath(ContextImpl.java:975)
[StrictMode] 	at android.app.ContextImpl.getSharedPreferences(ContextImpl.java:573)
[StrictMode] 	at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:226)
[StrictMode] 	at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:545)
[StrictMode] 	at crc640ec207abc449b2ca.ShellSectionRenderer.n_onCreateView(Native Method)
[StrictMode] 	at crc640ec207abc449b2ca.ShellSectionRenderer.onCreateView(ShellSectionRenderer.java:44)
...
[StrictMode] StrictMode policy violation; ~duration=1750 ms: android.os.strictmode.DiskReadViolation
[StrictMode] 	at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1658)
[StrictMode] 	at libcore.io.BlockGuardOs.access(BlockGuardOs.java:74)
[StrictMode] 	at libcore.io.ForwardingOs.access(ForwardingOs.java:128)
[StrictMode] 	at android.app.ActivityThread$AndroidOs.access(ActivityThread.java:8615)
[StrictMode] 	at java.io.UnixFileSystem.checkAccess(UnixFileSystem.java:313)
[StrictMode] 	at java.io.File.exists(File.java:813)
[StrictMode] 	at android.app.SharedPreferencesImpl$EditorImpl.isSpeg(SharedPreferencesImpl.java:505)
[StrictMode] 	at android.app.SharedPreferencesImpl$EditorImpl.apply(SharedPreferencesImpl.java:517)
[StrictMode] 	at crc640ec207abc449b2ca.ShellSectionRenderer.n_onCreateView(Native Method)
[StrictMode] 	at crc640ec207abc449b2ca.ShellSectionRenderer.onCreateView(ShellSectionRenderer.java:44)
...
[StrictMode] StrictMode policy violation; ~duration=1645 ms: android.os.strictmode.DiskReadViolation
[StrictMode] 	at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1658)
[StrictMode] 	at libcore.io.BlockGuardOs.access(BlockGuardOs.java:74)
[StrictMode] 	at libcore.io.ForwardingOs.access(ForwardingOs.java:128)
[StrictMode] 	at android.app.ActivityThread$AndroidOs.access(ActivityThread.java:8615)
[StrictMode] 	at java.io.UnixFileSystem.checkAccess(UnixFileSystem.java:313)
[StrictMode] 	at java.io.File.exists(File.java:813)
[StrictMode] 	at android.app.ContextImpl.ensurePrivateDirExists(ContextImpl.java:790)
[StrictMode] 	at android.app.ContextImpl.ensurePrivateDirExists(ContextImpl.java:781)
[StrictMode] 	at android.app.ContextImpl.getPreferencesDir(ContextImpl.java:737)
[StrictMode] 	at android.app.ContextImpl.getSharedPreferencesPath(ContextImpl.java:975)
[StrictMode] 	at android.app.ContextImpl.getSharedPreferences(ContextImpl.java:573)
[StrictMode] 	at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:226)
[StrictMode] 	at crc640ec207abc449b2ca.ShellSectionRenderer.n_onCreateView(Native Method)
[StrictMode] 	at crc640ec207abc449b2ca.ShellSectionRenderer.onCreateView(ShellSectionRenderer.java:44)
...
[StrictMode] StrictMode policy violation; ~duration=1645 ms: android.os.strictmode.DiskReadViolation
[StrictMode] 	at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1658)
[StrictMode] 	at android.app.SharedPreferencesImpl.awaitLoadedLocked(SharedPreferencesImpl.java:279)
[StrictMode] 	at android.app.SharedPreferencesImpl.contains(SharedPreferencesImpl.java:357)
[StrictMode] 	at crc640ec207abc449b2ca.ShellSectionRenderer.n_onCreateView(Native Method)
[StrictMode] 	at crc640ec207abc449b2ca.ShellSectionRenderer.onCreateView(ShellSectionRenderer.java:44)
...
@BioTurboNick BioTurboNick added the t/bug Something isn't working label Dec 14, 2023
@jsuarezruiz jsuarezruiz added area-controls-shell Shell Navigation, Routes, Tabs, Flyout platform/android 🤖 labels Dec 14, 2023
@PureWeen PureWeen added the s/needs-repro Attach a solution or code which reproduces the issue label Dec 18, 2023
@ghost
Copy link

ghost commented Dec 18, 2023

Hi @BioTurboNick. We have added the "s/needs-repro" label to this issue, which indicates that we require steps and sample code to reproduce the issue before we can take further action. Please try to create a minimal sample project/solution or code samples which reproduce the issue, ideally as a GitHub repo that we can clone. See more details about creating repros here: https://github.com/dotnet/maui/blob/main/.github/repro.md

This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

@BioTurboNick
Copy link
Contributor Author

The stacktrace was misleading, unfortunately. On trying to find a repro, I tracked down the real source of the problem.

It was really related to code I was calling in the constructor of my main page. Why doesn't any indication of the true source of the code show up in the trace?

In any case, one source is from accessing Microsoft.Maui.ApplicationModel.VersionTracking.IsFirstLaunchEver. The other source was reading from Microsoft.Maui.Storage.Preferences. The former is documented as using the latter, so that makes sense.

So... I guess ultimately this issue is a user issue, because I'm literally reading from disk during creation of the UI. However, it seems like something could be done to improve the situation. Ensuring that the problem site can be more easily found in the debug log would do wonders.

@ghost ghost added s/needs-attention Issue has more information and needs another look and removed s/needs-repro Attach a solution or code which reproduces the issue labels Dec 19, 2023
@PureWeen
Copy link
Member

It was really related to code I was calling in the constructor of my main page.

if you enable first chance exceptions does that break where you want?

You do that by making sure there's a check in this box

image

I might be remembering incorrectly but the stack trace might be slightly off because the exception is happening in the constructor.

@PureWeen PureWeen added s/needs-info Issue needs more info from the author and removed s/needs-attention Issue has more information and needs another look labels Dec 19, 2023
@ghost
Copy link

ghost commented Dec 19, 2023

Hi @BioTurboNick. We have added the "s/needs-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

@BioTurboNick
Copy link
Contributor Author

It was already on, and I don't think this counts as an exception per se? Hmm. Well, maybe there's no easy improvement here... if so, feel free to close.

@ghost ghost added s/needs-attention Issue has more information and needs another look and removed s/needs-info Issue needs more info from the author labels Dec 20, 2023
@BioTurboNick
Copy link
Contributor Author

Okay here's a challenge...

App constructor => AppShell constructor => MainPage constructor

Then App.OnStart runs, which is where we're generally instructed to put application initialization logic. AppCenter, etc.

Oh, I see, the guidance has changed. I must have missed this in the XF --> MAUI migration. https://learn.microsoft.com/en-us/appcenter/sdk/getting-started/xamarin says "Open the project's App.xaml.cs file and add the following line in the constructor (or in the OnStart() method for Xamarin.Forms)"

@github-actions github-actions bot locked and limited conversation to collaborators Jan 20, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-controls-shell Shell Navigation, Routes, Tabs, Flyout platform/android 🤖 s/needs-attention Issue has more information and needs another look t/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants