Skip to content

fix(apple): deliver requestPermissions result on the main queue - #1587

Open
mansurrrr wants to merge 3 commits into
Baseflow:mainfrom
mansurrrr:fix/ios-request-result-main-queue
Open

mansurrrr wants to merge 3 commits into
Baseflow:mainfrom
mansurrrr:fix/ios-request-result-main-queue

Conversation

@mansurrrr

@mansurrrr mansurrrr commented Nov 16, 2026 •

Copy link
Copy Markdown

requestPermissions on iOS sometimes fails with ERROR_ALREADY_REQUESTING_PERMISSIONS when it is called right after a previous request has completed.

Cause. PermissionHandlerPlugin tracks the in-flight request with _methodResult and rejects a new request while it is set. Some strategies call their completion handler on a background queue. For example, NotificationPermissionStrategy calls completionHandler(PermissionStatusPermanentlyDenied) directly from the requestAuthorizationWithOptions: callback when the user denies. The plugin then delivers the result to Dart and clears _methodResult on that background queue. Dart can receive the result and send the next requestPermissions call before _methodResult = nil has run, so the main thread still sees a pending request and rejects the call.

We see this in production after a notification denial followed immediately by an App Tracking Transparency request:

  • 22 of 23 events in the last 90 days are on iOS 27. There, presenting a system alert no longer makes the app inactive, so code that waits for the app to resume before requesting ATT continues without waiting and sends the request within milliseconds.
  • The ATT prompt is never shown to these users.

We reproduced the failure by adding a short delay before _methodResult = nil: the Dart side has already received the result, yet the next request fails.

Change. In PermissionHandlerPlugin.m, the requestPermissions completion and error handlers now run on the main queue. They run inline when already on the main thread. Clearing _methodResult and checking it for the next request now happen on the same thread, in order. This fixes all strategies in one place rather than in each one.

Testing. No unit tests exist for permission_handler_apple. Verified on an iPhone 16 Pro running iOS 27.2, with a 1 s delay injected before _methodResult = nil to make the race deterministic:

  • Without this change: the denied notification completion arrives on a background thread, the ATT request sent ~50 ms later is rejected with ERROR_ALREADY_REQUESTING_PERMISSIONS, and the ATT prompt is not shown.
  • With this change: the ATT request is handled after _methodResult is cleared, and the ATT prompt is shown.

Related: #1511 addresses a different path to the same error (state not reset when the app goes to the background) and does not touch this race.

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 is does not need version changes.
  • I updated CHANGELOG.md to add a description of the change.
  • I updated/added relevant documentation (doc comments with ///). (Objective-C only; no public API change.)
  • I rebased onto main.
  • I added new tests to check the change I am making, or this PR does not need tests. (No test target exists for this package.)
  • I made sure all existing and new tests are passing.
  • I ran dart format . and committed any changes. (No Dart changes.)
  • I ran flutter analyze and fixed any errors. (No Dart changes.)

@mansurrrr

Copy link
Copy Markdown
Author

The CI failures are unrelated to this change. flutter analyze fails on the deprecated package:flutter/material.dart import in example/lib/main.dart with the new Flutter stable (3.50.0). The same failure shows up on other recent PRs (e.g. the Windows clang-cl fix). This PR only touches Objective-C, pubspec.yaml and CHANGELOG.md, and the format step passes.

@mansurrrr
mansurrrr marked this pull request as ready for review November 16, 2026 07:45
@mansurrrr
mansurrrr force-pushed the fix/ios-request-result-main-queue branch from c6ea66d to b45a9cc Compare December 2, 2026 07:03
@codecov

codecov Bot commented Dec 2, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 71.42%. Comparing base (b0ad13b) to head (b45a9cc).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1587   +/-   ##
=======================================
  Coverage   71.42%   71.42%           
=======================================
  Files           1        1           
  Lines          35       35           
=======================================
  Hits           25       25           
  Misses         10       10           
Flag Coverage Δ
unittests 71.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.

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.

1 participant