Skip to content

fix(ios): read notification permission status without blocking the main thread - #1573

Merged
mvanbeusekom merged 2 commits into
Baseflow:mainfrom
efim0v:fix/ios-notification-status-deadlock
Oct 23, 2026
Merged

mvanbeusekom merged 2 commits into
Baseflow:mainfrom
efim0v:fix/ios-notification-status-deadlock

Conversation

@efim0v

@efim0v efim0v commented Oct 16, 2026 •

Copy link
Copy Markdown

Fixes #1420

Permission.notification.status on iOS ends up in +[NotificationPermissionStrategy permissionStatus], which calls getNotificationSettingsWithCompletionHandler: and then blocks the main thread on a semaphore with DISPATCH_TIME_FOREVER. If the settings callback needs the main queue, it never returns.

On the iOS 27 simulator this is 100% reproducible for us: our app reads the notification status at startup and stays on the launch screen forever after a cold start (9.6.2). Main thread of the hung process:

semaphore_wait_trap
dispatch_semaphore_wait
+[NotificationPermissionStrategy permissionStatus]        (NotificationPermissionStrategy.m:66)
-[NotificationPermissionStrategy checkPermissionStatus:]  (NotificationPermissionStrategy.m:15)
+[PermissionManager checkPermissionStatus:result:]        (PermissionManager.m:23)
-[PermissionHandlerPlugin handleMethodCall:result:]       (PermissionHandlerPlugin.m:30)

requestPermission: reads the status the same way first, and CriticalAlertsPermissionStrategy has the identical pattern. No other strategy uses a semaphore.

Changes

Went with the approach suggested in the comment on #1420, same as #1100 did for checkServiceStatus:

  • PermissionStrategy gets an optional checkPermissionStatus:completionHandler:. Other strategies are untouched.
  • PermissionManager uses it when the strategy implements it and calls result on the main queue.
  • Notification and critical alerts strategies implement it on top of getNotificationSettingsWithCompletionHandler: without the semaphore, and requestPermission: uses it too.

The settings → PermissionStatus mapping didn't change, I just moved it into +permissionStatusFromSettings: so both paths share it.

Tests

The example app had no native test target, so I added RunnerTests with one XCTest that calls the plugin on the main thread and expects the result to come back asynchronously. It fails (or hangs) on 9.6.2 and passes with the fix on the iOS 27.0 simulator, both with SwiftPM and CocoaPods:

cd permission_handler_apple/example
dart run permission_handler_apple:select debug
flutter build ios --simulator --debug --config-only
xcodebuild test -workspace ios/Runner.xcworkspace -scheme Runner \
  -destination "platform=iOS Simulator,name=<simulator>" -only-testing:RunnerTests

Also checked in our app on the iOS 27 simulator: it gets past the launch screen now.

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 ///).
  • 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.

efim0v and others added 2 commits October 16, 2026 14:53
…in thread

`NotificationPermissionStrategy` and `CriticalAlertsPermissionStrategy` waited
for `getNotificationSettingsWithCompletionHandler:` with
`dispatch_semaphore_wait(DISPATCH_TIME_FOREVER)` on the platform (main)
thread. When the settings are delivered on the main queue (iOS 27) the wait
never ends and the app freezes, typically on the launch screen.

Following the approach of Baseflow#1100 for `checkServiceStatus`:

- `PermissionStrategy` gains an optional asynchronous
  `checkPermissionStatus:completionHandler:`; every other strategy keeps its
  synchronous path.
- `+[PermissionManager checkPermissionStatus:result:]` uses it when the
  strategy responds to the selector and calls `result` on the main queue.
- Both strategies implement it on top of
  `getNotificationSettingsWithCompletionHandler:` without a semaphore and use
  the same asynchronous check in `requestPermission:`. The status mapping is
  unchanged.

Adds a `RunnerTests` target to the example with a test that calls the plugin
on the main thread and fails when the status is resolved synchronously.

Fixes Baseflow#1420

@mvanbeusekom mvanbeusekom left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This looks great, thank you very much for this contribution.

@mvanbeusekom
mvanbeusekom merged commit 0ea2555 into Baseflow:main Oct 23, 2026
1 check passed
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]: Crash iOS [NotificationPermissionStrategy permissionStatus]

2 participants