Skip to content

permission_handler_windows: lazily create Geolocator - #1546

Merged
mvanbeusekom merged 1 commit into
Baseflow:mainfrom
prismplural:fix/windows-lazy-geolocator
Nov 6, 2026
Merged

mvanbeusekom merged 1 commit into
Baseflow:mainfrom
prismplural:fix/windows-lazy-geolocator

Conversation

@skylartaylor

@skylartaylor skylartaylor commented Jul 3, 2026 •

Copy link
Copy Markdown
Contributor

permission_handler_windows currently creates a Windows Geolocator and subscribes to PositionChanged while the plugin is registering. That can make apps appear to access location even when they only depend on permission_handler and never request location.

This PR removes the eager Geolocator member and PositionChanged subscription. Geolocator is now created only inside IsLocationServiceEnabled(), so location service-status checks still work without touching the Windows location API during plugin registration.

Fixes #983.
Fixes #1394.
Related to #1470 and #1389.

Validation after rebasing onto current main (2026-11-06):

  • flutter pub get (workspace resolution)
  • dart format --output=none --set-exit-if-changed permission_handler_windows (no changes)
  • flutter analyze --no-pub in permission_handler_windows
  • flutter analyze --no-pub in permission_handler_windows/example
  • flutter test --no-pub in permission_handler (20 tests passed)
  • git diff --check (clean)

The Windows package has no unit test suite. A fresh native Windows build is left to this PR's CI; it cannot run on this macOS host. The original C++ fix is unchanged by the rebase, and the package version/changelog now target 0.2.4.

Earlier Windows runtime smoke evidence, recorded before this rebase:

Pre-launch Checklist

  • I made sure the project builds.
  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I updated pubspec.yaml with an appropriate new version according to the pub versioning philosophy, or this PR does not need version changes.
  • I updated CHANGELOG.md to add a description of the change.
  • I updated/added relevant documentation (doc comments with ///), or this PR does not need documentation changes.
  • I rebased onto main.
  • I added new tests to check the change I am making, or this PR does not need tests.
  • I made sure all existing and new tests are passing.
  • I ran dart format . and committed any changes.
  • I ran flutter analyze and fixed any errors.

@guyue88

guyue88 commented Oct 23, 2026

Copy link
Copy Markdown

Independent verification of this fix — including MSIX packaged builds

We hit the same problem in a password manager, where "this app is using your location" is especially damaging to user trust. We independently arrived at the same root cause and essentially the same fix as this PR before finding it, so treating this as a second data point.

Verified on Windows 11 (26300), against a cleared baseline. We first deleted our entries under HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\CapabilityAccessManager\ConsentStore\location, so that any entry appearing afterwards is necessarily newly created:

Build ConsentStore\location entry after launch
MSIX packaged (installed via Add-AppxPackage, runs from WindowsApps) none
Non-packaged Release .exe none

Before the change, the MSIX build had an entry with Value = Allow reported as currently in use, and the app showed up in Settings → Privacy & security → Location alongside weather apps.

As a control, three stale entries from earlier builds kept their original timestamps (Sep 23 / Sep 30) and were not refreshed by the post-fix launches — so the absence of new entries isn't just the registry failing to update.

The MSIX case specifically was raised in #1394 but I couldn't find it verified anywhere, so hopefully this closes that gap.

On the safety of removing the subscription

Confirming from our side as well: IsLocationServiceEnabled() only reads geolocator.LocationStatus(), which is a property read and does not require an active PositionChanged subscription. Location permission queries continue to work after the change — the empty subscription was doing nothing except starting a location session.

Context

There are currently four open PRs touching this (#1389, #1470, this one, #1568), the oldest from September 2024, and the bug report #1394 has been open since October 2024. This PR looks like the cleanest of them. We're shipping a vendored override in the meantime and would much rather drop it — happy to help with testing if that's useful.

@luoqiang-wm

Copy link
Copy Markdown

Friendly follow-up now that 0.2.3 is out (2026-11-03): the plugin constructor still subscribes to Geolocator::PositionChanged with an empty handler, so every Windows app using permission_handler keeps showing up under Settings → Privacy & security → Location as soon as it starts, even if it never requests location (#1394).

This PR is small and self-contained, and we've been shipping the equivalent change in production since October — verified on both MSIX-packaged and unpackaged builds (see the comment above). Could it be considered for the next release? If a rebase onto main would help, we can open one.

@skylartaylor
skylartaylor force-pushed the fix/windows-lazy-geolocator branch from 721669c to 7bcb94e Compare November 6, 2026 08:37
@skylartaylor

Copy link
Copy Markdown
Contributor Author

Agreed, this still needs fixing. Current main / permission_handler_windows 0.2.3 still creates a Geolocator and subscribes to PositionChanged during plugin registration, so apps can appear to use location without ever requesting it. Thanks for the independent MSIX and unpackaged-build verification above.

I've rebased this PR onto current main, resolved the version/changelog conflicts, and updated the Windows package to 0.2.4. The original C++ fix is unchanged. Formatting, analysis of the Windows package and example, and all 20 app-facing tests pass locally; CI is now running fresh Windows and app-facing builds.

@mvanbeusekom, could you and the other maintainers review this for the next Windows release? Apps should not appear to access location simply because they depend on permission_handler.

@codecov

codecov Bot commented Nov 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 71.42%. Comparing base (0241e81) to head (7bcb94e).
⚠️ Report is 1 commits behind head on main.

❗ There is a different number of reports uploaded between BASE (0241e81) and HEAD (7bcb94e). Click for more details.

HEAD has 1 upload less than BASE
Flag BASE (0241e81) HEAD (7bcb94e)
unittests 2 1
Additional details and impacted files
@@             Coverage Diff             @@
##             main    #1546       +/-   ##
===========================================
- Coverage   89.84%   71.42%   -18.42%     
===========================================
  Files           7        1        -6     
  Lines         128       35       -93     
===========================================
- Hits          115       25       -90     
+ Misses         13       10        -3     
Flag Coverage Δ
unittests 71.42% <ø> (-18.42%) ⬇️

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.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@skylartaylor

Copy link
Copy Markdown
Contributor Author

The codecov/project failure comes from incomplete coverage uploads. This PR changes none of the measured Dart code: CI skipped the unchanged platform-interface package, so Codecov received one unittests upload instead of the two on the baseline. It compares the app-facing package's 25/35 covered lines (71.42%) with both packages' 115/128 (89.84%). I reran both suites: all 68 tests passed, and their combined coverage is still 89.84%. The Windows build, app-facing build and ci-result also passed.

I've opened #1584 to give each tested package its own coverage flag and carry forward its coverage when CI skips that package. Coverage configuration changes will run both suites to establish a complete baseline. Once that fix lands and establishes the baseline on main, this PR can be rebased to pick it up.

@mvanbeusekom
mvanbeusekom merged commit dd50555 into Baseflow:main Nov 6, 2026
8 of 9 checks passed
@MSOB7YY

MSOB7YY commented Nov 6, 2026

Copy link
Copy Markdown

thanks for the work, should i close #1470 now?

@krll-kov

krll-kov commented Nov 6, 2026

Copy link
Copy Markdown

Related to #1470 and #1389.

Geolocator is now constructed in IsLocationServiceEnabled (permission_handler_windows_plugin.cpp:144) and still throws winrt::hresult_class_not_registered on Windows 10 1607 LTSB, so the app dies on the first Permission.location.serviceStatus call instead of at startup. IsBluetoothServiceEnabled activates BluetoothAdapter and Radio the same way without a catch, and as a fire_and_forget coroutine any exception there goes straight to winrt::terminate()

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.

[Bug]: Windows shows app as constantly using the device's location Bug: location tracking in windows

6 participants