Skip to content

Failed to persist session on the file system. #3653

Closed

Description

@pulmuone

Environment

SaaS (https://sentry.io/)

Steps to Reproduce

When I start my app on Maui.iOS, I get an Exception error from Sentry.

2024-10-04 20:35:37.251 ExpiryDateScanner.iOS[1520:378218] Info: Envelope queued up: ''
Debug: Created directory for installation ID file (/var/mobile/Containers/Data/Application/F2E510BB-A8AA-4283-9F53-B5191AA45840/Library/Caches/Sentry/4D9C78D8B2C334529A61CC489DAB4D5ED749FBFA).
Debug: Resolved installation ID '4fff81ed-63d1-41a4-809b-22abb303548d'.
Info: Started new session (SID: 03e879f3c47b477ea0179ad05feecff9; DID: 4fff81ed-63d1-41a4-809b-22abb303548d).
Debug: Storing file /var/mobile/Containers/Data/Application/F2E510BB-A8AA-4283-9F53-B5191AA45840/Library/Caches/Sentry/4D9C78D8B2C334529A61CC489DAB4D5ED749FBFA/1728041737_684__1114545838.envelope.
Debug: Persisting session (SID: '03e879f3c47b477ea0179ad05feecff9') to a file.
Debug: Creating persistence directory for session file at '/var/mobile/Containers/Data/Application/F2E510BB-A8AA-4283-9F53-B5191AA45840/Library/Caches/Sentry/4D9C78D8B2C334529A61CC489DAB4D5ED749FBFA'.
2024-10-04 20:35:37.253 ExpiryDateScanner.iOS[1520:378218] Debug: Created directory for installation ID file (/var/mobile/Containers/Data/Application/F2E510BB-A8AA-4283-9F53-B5191AA45840/Library/Caches/Sentry/4D9C78D8B2C334529A61CC489DAB4D5ED749FBFA).
2024-10-04 20:35:37.255 ExpiryDateScanner.iOS[1520:378218] Debug: Resolved installation ID '4fff81ed-63d1-41a4-809b-22abb303548d'.
2024-10-04 20:35:37.256 ExpiryDateScanner.iOS[1520:378218] Info: Started new session (SID: 03e879f3c47b477ea0179ad05feecff9; DID: 4fff81ed-63d1-41a4-809b-22abb303548d).
2024-10-04 20:35:37.257 ExpiryDateScanner.iOS[1520:378259] Debug: Storing file /var/mobile/Containers/Data/Application/F2E510BB-A8AA-4283-9F53-B5191AA45840/Library/Caches/Sentry/4D9C78D8B2C334529A61CC489DAB4D5ED749FBFA/1728041737_684__1114545838.envelope.
2024-10-04 20:35:37.258 ExpiryDateScanner.iOS[1520:378218] Debug: Persisting session (SID: '03e879f3c47b477ea0179ad05feecff9') to a file.
2024-10-04 20:35:37.258 ExpiryDateScanner.iOS[1520:378218] Debug: Creating persistence directory for session file at '/var/mobile/Containers/Data/Application/F2E510BB-A8AA-4283-9F53-B5191AA45840/Library/Caches/Sentry/4D9C78D8B2C334529A61CC489DAB4D5ED749FBFA'.
Debug: Persisted session to a file '/var/mobile/Containers/Data/Application/F2E510BB-A8AA-4283-9F53-B5191AA45840/Library/Caches/Sentry/4D9C78D8B2C334529A61CC489DAB4D5ED749FBFA/.session'.
Debug: Envelope handed off to transport (event ID: ''). 1 items in queue.
2024-10-04 20:35:37.271 ExpiryDateScanner.iOS[1520:378218] Debug: Persisted session to a file '/var/mobile/Containers/Data/Application/F2E510BB-A8AA-4283-9F53-B5191AA45840/Library/Caches/Sentry/4D9C78D8B2C334529A61CC489DAB4D5ED749FBFA/.session'.
2024-10-04 20:35:37.273 ExpiryDateScanner.iOS[1520:378259] Debug: Envelope handed off to transport (event ID: ''). 1 items in queue.
2024-10-04 20:35:37.282 ExpiryDateScanner.iOS[1520:378218] Error: Failed to persist session on the file system.
System.ObjectDisposedException: Cannot access a closed file.
at System.IO.FileStream.Flush(Boolean flushToDisk)
at System.IO.FileStream.Flush()
at System.Text.Json.Utf8JsonWriter.Flush()
2024-10-04 20:35:37.282 ExpiryDateScanner.iOS[1520:378218] at System.Text.Json.Utf8JsonWriter.Dispose()
at Sentry.GlobalSessionManager.PersistSession(SessionUpdate update, Nullable1 pauseTimestamp) Error: Failed to persist session on the file system. System.ObjectDisposedException: Cannot access a closed file. at System.IO.FileStream.Flush(Boolean flushToDisk) at System.IO.FileStream.Flush() at System.Text.Json.Utf8JsonWriter.Flush() at System.Text.Json.Utf8JsonWriter.Dispose() at Sentry.GlobalSessionManager.PersistSession(SessionUpdate update, Nullable1 pauseTimestamp)

Activity

  1. getsantry commented on Oct 4, 2024

    @getsantry

    Assigning to @getsentry/support for routing ⏲️

  2. transferred this issue fromgetsentry/sentryon Oct 4, 2024
  3. moved this from Waiting for: Support to Waiting for: Product Owner in GitHub Issues with 👀 3on Oct 4, 2024
  4. jamescrosswell commented on Oct 6, 2024

    @jamescrosswell
    Collaborator

    @pulmuone which version of the Sentry.Maui NuGet package are you referencing in your project?

  5. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Oct 6, 2024
  6. jamescrosswell commented on Oct 11, 2024

    @jamescrosswell
    Collaborator

    @pulmuone this should be fixed in the 4.12.1 release. Let us know if you still have issues after upgrading to that version though.

  7. BillyMartin1964 commented on Mar 29, 2026

    @BillyMartin1964

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

  8. BillyMartin1964 commented on Mar 29, 2026

    @BillyMartin1964

    I've had to do this to get it to run:

    	```
    .UseSentry(options =>
    		{
    			try
    			{
    
    				// The DSN is the only required setting.
    				options.Dsn = Keys.MASTERMIND_SENTRY_DSN;
    

    #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;

    var sessionFile = Path.Combine(sentryDir, ".session");
    if (File.Exists(sessionFile))
    {
        options.AutoSessionTracking = true;
    }
    else
    {
        options.AutoSessionTracking = false;
    }
    

    }
    else
    {
    options.AutoSessionTracking = false;
    }
    #endif

    				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
    
    				// Other Sentry options can be set here.
    
    
    				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);
    
  9. jamescrosswell commented on Mar 29, 2026

    @jamescrosswell
    Collaborator

    Quite a lot has changed since v4 so I've opened a new issue to track this possible regression. I'll lock this issue down so we can keep all further discussions in one place. Follow ups on :

  10. locked and limited conversation to collaborators on Mar 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions