Skip to content

fix(flutter): Fallback native DB path on Windows - #4156

Merged
buenaflor merged 7 commits into
mainfrom
lz/fix/win-ac-violation
Nov 16, 2026
Merged

buenaflor merged 7 commits into
mainfrom
lz/fix/win-ac-violation

Conversation

@lucas-zimerman

@lucas-zimerman lucas-zimerman commented Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator

📜 Description

On Windows, when nativeDatabasePath isn't set, sentry_flutter now makes sure sentry-native gets a writable database directory:

  1. It checks that sentry-native's default .sentry-native in 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-native already exists.
  2. If it fails, or the working directory can't be read, it passes %LOCALAPPDATA%\<executable name>\sentry-<DSN hash> to sentry-native instead and logs that at info level.
    • The hash is the 32-bit FNV-1a of the DSN, as 8 hex characters. It keeps two apps with the same executable name from sending each other's pending reports.
    • It stays the same across app updates. A hash of the install path wouldn't: packaged apps have their version in it.
  3. If LOCALAPPDATA isn't set, it keeps the previous behavior.

An explicitly configured nativeDatabasePath and 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 at nativeDatabasePath. The option's docs describe the fallback.

💡 Motivation and Context

Refs #4109. The report: the Microsoft Store shows ACCESS_VIOLATION crashes for a Windows app, but none of them reach Sentry.

  • Packaged (MSIX/Microsoft Store) apps start with C:\Windows\System32 as their working directory. I read it from the process of both the reporter's Store app and a packaged build of our example.
  • With no nativeDatabasePath, sentry-native uses .sentry-native relative to the working directory. That isn't writable there, so sentry_init returns 1 (failed to create database directory or there is no write access to this directory).
  • SentryNative.init swallows 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/Local on Windows). The Unity, Godot and Unreal SDKs always set one. Flutter left it to the working directory.

💚 How did you test it?

  • Unit tests (test/native/c/native_database_path_test.dart) cover:
    • the fallback when the working directory can't be written to:
      • with a blocking file;
      • with a mocked access-denied error (IOOverrides, OSError code 5);
      • with an existing .sentry-native where file creation is denied;
      • with a working directory that can't be read;
    • that only LOCALAPPDATA is read, and only when needed;
    • that the working directory isn't read when a path is configured or on non-Windows;
    • that the write-test file is removed;
    • that different DSNs get different paths, and a missing DSN;
    • writable working directory, configured path, missing LOCALAPPDATA, and non-Windows;
    • dsnHash against FNV-1a test vectors, so a future change can't silently move the directory.
  • Native FFI suite (test/native/c/sentry_native_test_ffi.dart), Windows only: real init with a blocked working directory and LOCALAPPDATA pointing to a temp folder, both when the app folder doesn't exist yet and when it already exists. Existing content is kept.
  • Mutation checks:
    • Making the fallback ignore LOCALAPPDATA breaks 3 unit tests and both native tests.
    • Dropping the write-test file breaks the read-only test.
  • Results:
    • Windows: 100% unit and 100% native. The one skipped native test comes from main and is skipped on Windows.
    • Linux (WSL2): 100% unit and 100% native. The 2 Windows-only cases are skipped. This run was before merging main.
    • Breakpad backend.
  • End to end:
    • I registered the example's Windows build as a packaged app and launched it through shell:AppsFolder.
    • Before the change, no database was created anywhere.
    • After it, MSIX redirected the write into the package's LocalCache\Local\sentry_flutter_example\sentry, which has an active run, lock and session, so init succeeded.
    • This was before the DSN hash was added to the folder name. The FFI tests cover the hashed path.

Not tested: the Crashpad backend, a signed .msix, and a native crash reaching Sentry after relaunch.

📝 Checklist

  • I reviewed submitted code
  • I added tests to verify changes
  • No new PII added or SDK only sends newly added PII if sendDefaultPii is enabled
  • I updated the docs if needed
  • All tests passing
  • Public API changes reviewed by another Mobile SDK team member or implemented according to the develop docs spec
  • No breaking changes

🔮 Next steps

  • Linux may have the same problem when the working directory isn't writable (e.g. XDG_CACHE_HOME as a fallback). Not in this PR.
  • Apps that already had .sentry-native in 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 nativeDatabasePath isn't set, so native crashes from packaged (MSIX/Microsoft Store) apps are captured.

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]>
@github-actions

github-actions Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Semver Impact of This PR

🟢 Patch (bug fixes)

📋 Changelog Preview

This is how your changes will appear in the changelog.
Entries from this PR are highlighted with a left border (blockquote style).


Fixes

  • Use a writable native database directory on Windows when nativeDatabasePath isn't set, so native crashes from packaged (MSIX/Microsoft Store) apps are captured. by lucas-zimerman and claude in #4156

🤖 This preview updates automatically when you update the PR.

@codecov

codecov Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.83333% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 93.14%. Comparing base (525de66) to head (41e3009).
⚠️ Report is 21 commits behind head on main.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
...ckages/flutter/lib/src/native/c/sentry_native.dart 91.66% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4156      +/-   ##
==========================================
+ Coverage   88.87%   93.14%   +4.27%     
==========================================
  Files         353      120     -233     
  Lines       12714     4832    -7882     
==========================================
- Hits        11299     4501    -6798     
+ Misses       1415      331    -1084     
Flag Coverage Δ
sentry ?
sentry_dio ?
sentry_drift ?
sentry_file ?
sentry_firebase_remote_config 100.00% <ø> (ø)
sentry_flutter 92.89% <95.83%> (+0.06%) ⬆️
sentry_grpc ?
sentry_hive ?
sentry_isar ?
sentry_link ?
sentry_logging ?
sentry_sqflite ?
sentry_supabase 97.46% <ø> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@buenaflor buenaflor left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

lucas-zimerman and others added 3 commits November 12, 2026 19:46
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]>
@lucas-zimerman

Copy link
Copy Markdown
Collaborator Author

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.

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

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ 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.

lucas-zimerman and others added 2 commits November 13, 2026 16:16
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]>
@buenaflor
buenaflor merged commit 53535f4 into main Nov 16, 2026
72 checks passed
@buenaflor
buenaflor deleted the lz/fix/win-ac-violation branch November 16, 2026 09:38
buenaflor pushed a commit that referenced this pull request Nov 19, 2026
* 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants