Repository navigation
fix(flutter): Fallback native DB path on Windows - #4156
Conversation
Packaged (MSIX/Microsoft Store) Windows apps start with C:\Windows\System32 as their working directory. When nativeDatabasePath isn't set, sentry-native defaults to .sentry-native in the working directory, which isn't writable there, so sentry_init fails silently and native crashes are never reported. On Windows, when no path is configured, try to create the default database directory first and keep it if that works. Otherwise fall back to %LOCALAPPDATA%\<executable name>\sentry. Also make the init failure message say native crashes won't be captured. Refs #4109 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Semver Impact of This PR🟢 Patch (bug fixes) 📋 Changelog PreviewThis is how your changes will appear in the changelog. Fixes
🤖 This preview updates automatically when you update the PR. |
Codecov Report❌ Patch coverage is
Additional details and impacted files
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
buenaflor
left a comment
There was a problem hiding this comment.
Could we tighten up the fallback before merging?
- %LOCALAPPDATA%<exe name>\sentry can be shared by two unpackaged apps with the same exe name. Since sentry-native picks up pending reports from that directory on startup, one app could send the other’s reports to its own DSN. Can we include a stable app identifier in the path?
- createSync succeeds if .sentry-native already exists, even when the app can’t write inside it. I reproduced that with a read-only directory. The resolver kept it, then creating a lock file failed. Could we probe by creating a temp file inside the directory and add a test for that case?
I’d also move the configured-path and platform checks ahead of the Directory.current read. That read can throw if the working directory was deleted, even when the app already supplied a valid absolute database path.
Address review feedback on the LOCALAPPDATA fallback: - Add an FNV-1a hash of the DSN to the fallback directory so apps sharing an executable name don't send each other's reports. The hash stays stable across updates, unlike packaged install paths. - Probe writability by creating a file inside .sentry-native, since creating an existing read-only directory succeeds. - Read the working directory only on Windows when no path is set, and fall back if it can't be read. Refs #4109 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…/fix/win-ac-violation
I opted by adding a hashed dsn on it, this way it will only link events from the given dsn and not mix with other app using another dsn. The other cases were sorted on d1f66dd |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 35661fd. Configure here.
Creating the probe file already proves the default database is writable. A failed delete, e.g. while a virus scanner holds the file, no longer moves the database to LOCALAPPDATA. Refs #4109 Co-Authored-By: Claude Opus 5.5 <[email protected]>
* fix(native): Fall back to XDG cache dir on Linux When nativeDatabasePath isn't set and the working directory isn't writable, sentry-native fails to init and native crash handling is off. Like the Windows fallback in #4156, use $XDG_CACHE_HOME/<exe>/sentry-<DSN hash>, or $HOME/.cache/... when XDG_CACHE_HOME is unset, empty or relative. Fixes #4168 * test(native): Cover the Linux DB fallback in FFI tests Run the unwritable-working-directory FFI group on Linux as well as Windows, using XDG_CACHE_HOME there. Add unit tests for an empty HOME and for Linux without a DSN, move getLinuxSut onto Fixture, and say when the Linux fallback applies in the nativeDatabasePath docs.

📜 Description
On Windows, when
nativeDatabasePathisn't set,sentry_flutternow makes sure sentry-native gets a writable database directory:.sentry-nativein the working directory is writable, by creating the directory and a temporary file inside it. If that works, nothing changes. Creating the directory alone isn't enough, because that succeeds when a read-only.sentry-nativealready exists.%LOCALAPPDATA%\<executable name>\sentry-<DSN hash>to sentry-native instead and logs that at info level.LOCALAPPDATAisn't set, it keeps the previous behavior.An explicitly configured
nativeDatabasePathand Linux are unchanged, and the working directory isn't read for them. The init-failure error now also says that native crashes won't be captured and points atnativeDatabasePath. The option's docs describe the fallback.💡 Motivation and Context
Refs #4109. The report: the Microsoft Store shows
ACCESS_VIOLATIONcrashes for a Windows app, but none of them reach Sentry.C:\Windows\System32as their working directory. I read it from the process of both the reporter's Store app and a packaged build of our example.nativeDatabasePath, sentry-native uses.sentry-nativerelative to the working directory. That isn't writable there, sosentry_initreturns1(failed to create database directory or there is no write access to this directory).SentryNative.initswallows that failure, so the app runs with native crash handling silently disabled.The sentry-native README already recommends an explicit absolute path under the app's cache directory (
AppData/Localon Windows). The Unity, Godot and Unreal SDKs always set one. Flutter left it to the working directory.💚 How did you test it?
test/native/c/native_database_path_test.dart) cover:IOOverrides,OSErrorcode 5);.sentry-nativewhere file creation is denied;LOCALAPPDATAis read, and only when needed;LOCALAPPDATA, and non-Windows;dsnHashagainst FNV-1a test vectors, so a future change can't silently move the directory.test/native/c/sentry_native_test_ffi.dart), Windows only: real init with a blocked working directory andLOCALAPPDATApointing to a temp folder, both when the app folder doesn't exist yet and when it already exists. Existing content is kept.LOCALAPPDATAbreaks 3 unit tests and both native tests.mainand is skipped on Windows.main.shell:AppsFolder.LocalCache\Local\sentry_flutter_example\sentry, which has an active run, lock and session, so init succeeded.Not tested: the Crashpad backend, a signed
.msix, and a native crash reaching Sentry after relaunch.📝 Checklist
sendDefaultPiiis enabled🔮 Next steps
XDG_CACHE_HOMEas a fallback). Not in this PR..sentry-nativein a writable working directory keep using it, so no reports are lost on upgrade.Changelog Entry
Use a writable native database directory on Windows when
nativeDatabasePathisn't set, so native crashes from packaged (MSIX/Microsoft Store) apps are captured.