Skip to content

Possible regression: Failed to persist session on the file system #5083

Closed

Description

@jamescrosswell

I think this might be back. I've had to disable auto tracking to avoid a crash. I thought a try catch would work, but doesn't:

System.IO.FileNotFoundException thrown
  at Interop.ThrowExceptionForIoErrno(Interop.ErrorInfo errorInfo, string path, bool isDirError) in System.Private.CoreLib.dll
  at Interop.CheckIo(Interop.Error error, string path, bool isDirError) in System.Private.CoreLib.dll
  at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(string path, Interop.Sys.OpenFlags flags, int mode, bool failForSymlink, out bool wasSymlink, Func<Interop.ErrorInfo, Interop.Sys.OpenFlags, string, Exception> createOpenException) in System.Private.CoreLib.dll
  at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(string fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, long preallocationSize, UnixFileMode openPermissions, out long fileLength, out UnixFileMode filePermissions, bool failForSymlink, out bool wasSymlink, Func<Interop.ErrorInfo, Interop.Sys.OpenFlags, string, Exception> createOpenException) in System.Private.CoreLib.dll
  at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(string fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, long preallocationSize, UnixFileMode? unixCreateMode, Func<Interop.ErrorInfo, Interop.Sys.OpenFlags, string, Exception> createOpenException) in System.Private.CoreLib.dll
  at System.IO.Strategies.OSFileStreamStrategy..ctor(string path, FileMode mode, FileAccess access, FileShare share, FileOptions options, long preallocationSize, UnixFileMode? unixCreateMode) in System.Private.CoreLib.dll
  at System.IO.Strategies.UnixFileStreamStrategy..ctor(string path, FileMode mode, FileAccess access, FileShare share, FileOptions options, long preallocationSize, UnixFileMode? unixCreateMode) in System.Private.CoreLib.dll
  at System.IO.Strategies.FileStreamHelpers.ChooseStrategyCore(string path, FileMode mode, FileAccess access, FileShare share, FileOptions options, long preallocationSize, UnixFileMode? unixCreateMode) in System.Private.CoreLib.dll
  at System.IO.Strategies.FileStreamHelpers.ChooseStrategy(FileStream fileStream, string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options, long preallocationSize, UnixFileMode? unixCreateMode) in System.Private.CoreLib.dll
  at System.IO.FileStream..ctor(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options, long preallocationSize) in System.Private.CoreLib.dll
  at System.IO.FileStream..ctor(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options) in System.Private.CoreLib.dll
  at System.IO.FileStream..ctor(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, bool useAsync) in System.Private.CoreLib.dll
  at System.IO.FileStream..ctor(string path, FileMode mode, FileAccess access, FileShare share) in System.Private.CoreLib.dll
  at System.IO.File.OpenRead(string path) in System.Private.CoreLib.dll
  at Sentry.Internal.FileSystemBase.OpenFileForReading(string path) in Sentry.dll
  at Sentry.Internal.Json.Load<PersistedSessionUpdate>(IFileSystem fileSystem, string filePath, Func<JsonElement, PersistedSessionUpdate> factory) in Sentry.dll
  at Sentry.GlobalSessionManager.<.ctor>b__11_0(string filePath) in Sentry.dll
  at Sentry.GlobalSessionManager.TryRecoverPersistedSession() in Sentry.dll
  at Sentry.Internal.Hub.StartSession() in Sentry.dll
  at Sentry.Integrations.AutoSessionTrackingIntegration.Register(IHub hub, SentryOptions options) in Sentry.dll
  at Sentry.Internal.Hub..ctor(SentryOptions options, ISentryClient client, ISessionManager sessionManager, ISystemClock clock, IInternalScopeManager scopeManager, RandomValuesFactory randomValuesFactory, IReplaySession replaySession, ISampleRandHelper sampleRandHelper, BackpressureMonitor backpressureMonitor) in Sentry.dll
  at Sentry.SentrySdk.InitHub(SentryOptions options) in Sentry.dll
  at Sentry.SentrySdk.Init(SentryOptions options) in Sentry.dll
  at Sentry.Maui.Internal.SentryMauiInitializer.Initialize(IServiceProvider services) in Sentry.Maui.dll
  at Microsoft.Maui.MauiContextExtensions.InitializeAppServices(MauiApp mauiApp) in //src/Core/src/MauiContextExtensions.cs:line 83
  at Microsoft.Maui.Hosting.MauiAppBuilder.Build() in //src/Core/src/Hosting/MauiAppBuilder.cs:line 198
  at CodeLogicMaster.MauiProgram.CreateMauiApp() in D:\AppsInStores\Maui\CodeLogicMaster\CodeLogicMaster\MauiProgram.cs:line 131
  at CodeLogicMaster.Platforms.Android.MainApplication.CreateMauiApp() in D:\AppsInStores\Maui\CodeLogicMaster\CodeLogicMaster\Platforms\Android\MainApplication.cs:line 14
  at Microsoft.Maui.MauiApplication.OnCreate() in /_/src/Core/src/Platform/Android/MauiApplication.cs:line 36
  at Android.App.Application.n_OnCreate(nint jnienv, nint native__this) in Mono.Android.dll

Originally posted by @BillyMartin1964 in #3653

Note

The stack trace clearly indicates a try..catch block isn't catching this exception. Apparently the when is an exception filter, that requires some special treatment in the runtime so there could be bugs around that. However we'd need a repro in order for Microsoft to be able to look into this further. If anyone is able to reliably reproduce this issue on Android, please let us know.

Activity

  1. linear-code commented on Mar 29, 2026

    @linear-code
  2. added
    Bug
    .NET
    on Mar 29, 2026
  3. changed the title [-]Possible regression: Failed to persist session on the file system. #3653[/-] [+]Possible regression: Failed to persist session on the file system[/+] on Mar 29, 2026
  4. jamescrosswell commented on Mar 29, 2026

    @jamescrosswell
    CollaboratorAuthor

    It is unusual that the try..catch block isn't working:

    catch (Exception ex) when (ex is FileNotFoundException or DirectoryNotFoundException)
    {
    // Not a notable error
    _options.LogDebug("A persisted session does not exist ({0}) at {1}.", ex.GetType().Name, filePath);
    return null;
    }

    However it looks like we could work around this easily enough simply by checking whether the file exists before trying to read it:

    _persistedSessionProvider = persistedSessionProvider
    ?? (filePath => Json.Load(_options.FileSystem, filePath, PersistedSessionUpdate.FromJson));

    var filePath = Path.Combine(_persistenceDirectoryPath, PersistedSessionFileName);
    try
    {
    var recoveredUpdate = _persistedSessionProvider(filePath);

    edit: I've asked in Discord about this.

  5. BillyMartin1964 commented on Mar 30, 2026

    @BillyMartin1964

    Thanks for getting to this so fast. I may have deleted the app because I was debugging and adding new models, so I'm not sure it's a persist error, but it definitely throws an exception instead of dying quietly if the file isn't there.

  6. moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Mar 30, 2026
  7. jamescrosswell commented on Mar 30, 2026

    @jamescrosswell
    CollaboratorAuthor

    I have no idea why that error is throwing (we clearly have a catch clause for it), but we should be able to work around it easily enough. See #5084

    edit: Actually @BillyMartin1964 are you using AOT compilation by any chance? If so, that could explain why the try/catch isn't working - potentially the Android/Native exception handling kicks in before the .NET handing in this case. @jpnurmi dose that seem feasible?

  8. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Mar 30, 2026
  9. BillyMartin1964 commented on Mar 30, 2026

    @BillyMartin1964

    AOT only in release, but this is a debug crash

  10. 27 remaining items

  11. jamescrosswell commented on Apr 2, 2026

    @jamescrosswell
    CollaboratorAuthor

    Not sure what you mean by reproduce it. It happens every time, but what do they need from me?

    That's the first step 😉

    Ideally it would be possible to demonstrate in a small sample app that could be shared with the Microsoft engineers... so it wouldn't contain any sensitive data or anything and would have minimal dependencies. There's a bit more info here on creating minimal repros.

    If you're only seeing it in a bigger or more complex application, one way is to copy the project somewhere and then slowly remove stuff until you've got the smallest set of things together that reproduce the problem. That can be a fair amount of work, so for more trivial scenarios you could start from a blank slate and add things to the app until you can recreate the behaviour.

  12. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Apr 2, 2026
  13. BillyMartin1964 commented on Apr 7, 2026

    @BillyMartin1964

    Not trying to give you a hard time, just wondering why you can't get a review so you can merge?

  14. moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Apr 7, 2026
  15. jamescrosswell commented on Apr 7, 2026

    @jamescrosswell
    CollaboratorAuthor

    @BillyMartin1964 that has been approved/merged now.

    There's one more minor issue to work around. One of the upstream repos we depend on for zlib in sentry-native is currently playing up (#5117). The sentry-native team are putting together a github clone/cache to workaround this. Once that's in place, we should be able to push out a new release including the fix from #5084.

    Apologies in advance for the delay on this.

  16. jamescrosswell commented on Apr 8, 2026

    @jamescrosswell
    CollaboratorAuthor

    @BillyMartin1964 that workaround should be included in the version 6.3.1 release that just went out.

  17. BillyMartin1964 commented on Apr 8, 2026

    @BillyMartin1964

    No longer crashing. Thanks for all your hard work.

    Does this seem like a good setup in .Net Maui 10 or is there a better way?

    			.UseSentry(options =>
    			{
    				try
    				{
    
    					// The DSN is the only required setting.
    					options.Dsn = Keys.CODE_LOGIC_SENTRY_DSN;
    
    					options.MaxBreadcrumbs = 1000;
    					options.IncludeTextInBreadcrumbs = true;
    					options.IncludeTitleInBreadcrumbs = true;
    					options.IncludeBackgroundingStateInBreadcrumbs = true;
    
    					options.AttachScreenshot = true;
    
    					// Use debug mode if you want to see what the SDK is doing.
    					// Debug messages are written to stdout with Console.Writeline,
    					// and are viewable in your IDE's debug console or with 'adb logcat', etc.
    					// This option is not recommended when deploying your application.
    					options.Debug = true;
    
    					// Enable performance monitoring
    					options.TracesSampleRate = 1.0;   // 1.0 = 100% sampling
    					options.AutoSessionTracking = true;
    
    					// Other Sentry options can be set here.
    

    #if ANDROID
    options.Environment = "Android";

                    var cacheDir = Android.App.Application.Context.CacheDir?.AbsolutePath;
                    if (!string.IsNullOrEmpty(cacheDir))
                    {
                        var sentryDir = Path.Combine(cacheDir, "Sentry");
                        Directory.CreateDirectory(sentryDir);
                        options.CacheDirectoryPath = cacheDir;
                    }
    

    #endif

    					options.SetBeforeSend((sentryEvent, hint) =>
    					{
    						if (sentryEvent.Exception is FileNotFoundException fnfe &&
    							fnfe.Message.Contains(".session"))
    							return null; // Suppress Sentry's own missing session file noise
    						return sentryEvent;
    					});
    				}
    				catch (Exception ex)
    				{
    					Console.WriteLine($"Error configuring Sentry: {ex}");
    				}
    
    			});
    
    	// Start a startup transaction as early as we can
    	var startupTransaction = SentrySdk.StartTransaction(
    		"app-startup",
    		"startup"
    	);
    
    	// Put it on the scope so we can retrieve/extend it later
    	SentrySdk.ConfigureScope(scope => scope.Transaction = startupTransaction);
    
  18. jamescrosswell commented on Apr 8, 2026

    @jamescrosswell
    CollaboratorAuthor

    Is there a reason you'r doing this?

                    var cacheDir = Android.App.Application.Context.CacheDir?.AbsolutePath;
                    if (!string.IsNullOrEmpty(cacheDir))
                    {
                        var sentryDir = Path.Combine(cacheDir, "Sentry");
                        Directory.CreateDirectory(sentryDir);
                        options.CacheDirectoryPath = cacheDir;
                    }
    

    On iOS/Android, you could just leave the CacheDirectoryPath in which case the SDK sets it automatically:

    CacheDirectoryPath = Microsoft.Maui.Storage.FileSystem.CacheDirectory;

    That may be related to the original error you were seeing.

    Otherwise looks OK 👍🏻

  19. BillyMartin1964 commented on Apr 9, 2026

    @BillyMartin1964

    Sorry. I saw it on some post somewhere. So I shouldn't need any platform specific preprocessor at all then?

  20. jamescrosswell commented on Apr 9, 2026

    @jamescrosswell
    CollaboratorAuthor

    Sorry. I saw it on some post somewhere. So I shouldn't need any platform specific preprocessor at all then?

    Not to set the cache directory no - we set that to a sensible default.

    There are sometimes platform specific options that you can use conditional compilation to set, but it doesn't look like you're using any of those so unnecessary in your case.

  21. BillyMartin1964 commented on Apr 10, 2026

    @BillyMartin1964

    Thanks so much . Btw,I sent you a Linked In invitation

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

Metadata

Metadata

Labels

.NETBug

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions