CommunityToolkit / CommunityToolkit/Maui
[BUG] CameraView on Windows crashes the app when permission is removed
- Dominant language
- C#
- Stars
- 2.7k
- Forks
- 500
- Avg merge
- 2d 14h
- Merged PRs (30d)
- 7
Description
### Is there an existing issue for this?
- [x] I have searched the existing issues
### Did you read the "Reporting a bug" section on Contributing file?
- [x] I have read the "Reporting a bug" section on Contributing file: https://github.com/CommunityToolkit/Maui/blob/main/CONTRIBUTING.md#reporting-a-bug
### Current Behavior
Starting a page with the `CameraView` on it crashes the whole app in certain circumstances, e.g. when the app permission is removed again, with no way to catch the `UnauthorizedAccessException`.
### Expected Behavior
The `CameraView` must not crash the app or there must be a way to handle the error.
### Steps To Reproduce
1. Clone the CommunityToolkit.Maui repository, at the latest `main` branch
2. Open the `CommunityToolkit.Maui.Sample.slnx` solution
3. Set the `CommunityToolkit.Maui.Sample` project as startup project
4. In the `CommunityToolkit.Maui.Sample.csproj` file, change the `WindowsPackageType` property to `MSIX`
5. In the `launchSettings.json` change `commandName` to `MsixPackage`
6. Run the sample app in debug configuration
7. Start the CameraView page for the first time and agree to using the camera and microphone permission
8. Open the App Settings from Windows, e.g. by searching for the "CommunityToolkit.Maui.Sample"-App, right-clicking the app entry and selecting "app settings"
9. Remove the camera permission
10. Open the CameraView page again (perhaps after restarting the app)
11. Observe the `UnhandledException` event handler of the app to be called, with `e.Exception` containing an `UnauthorizedAccessException` with message "Access is denied.\r\n\r\nAccess is denied.\r\n"
As the exception occurrs in a call to `CameraViewHandler.ConnectHandler()`, see stack trace below, the exception can't be caught by the app's code. When adding a catch block in `CameraViewExtensions.InitializeCameraForCameraView()` for this specific exception, the exception is thrown further down the line in the for loop in `PlatformRefreshAvailableCameras()`. When calling `continue` when this sort of exception is caught, the `availableCameras` list ends with no entries (I only have one camera), and eventually throws a `CameraException` in `CameraManager.ConnectCamera()`, which can't be caught either, since it is called from `CameraViewHandler.ConnectHandler()` by the MAUI handler implementation.
### Link to public reproduction project repository
https://github.com/CommunityToolkit/Maui/
### Environment
```markdown
- .NET MAUI CommunityToolkit: 15.0.0 + latest `main` branch
- OS: Windows 11 Build 26200.9106
- .NET MAUI: 10.0.90
```
### Anything else?
The full `UnauthorizedAccessException` stack trace is:
```
at CommunityToolkit.Maui.Extensions.CameraViewExtensions.d__1.MoveNext() in C:\Development\git\toolkit\src\CommunityToolkit.Maui.Camera\Extensions\CameraViewExtensions.windows.cs:line 46
at CommunityToolkit.Maui.Core.CameraProvider.d__13.MoveNext() in C:\Development\git\toolkit\src\CommunityToolkit.Maui.Camera\Providers\CameraProvider.windows.cs:line 23
at CommunityToolkit.Maui.Core.CameraProvider.d__11.MoveNext() in C:\Development\git\toolkit\src\CommunityToolkit.Maui.Camera\Providers\CameraProvider.shared.cs:line 55
at CommunityToolkit.Maui.Core.CameraManager.d__5.MoveNext() in C:\Development\git\toolkit\src\CommunityToolkit.Maui.Camera\CameraManager.shared.cs:line 33
at CommunityToolkit.Maui.Core.Handlers.CameraViewHandler.d__10.MoveNext() in C:\Development\git\toolkit\src\CommunityToolkit.Maui.Camera\Handlers\CameraViewHandler.shared.cs:line 89
at System.Threading.Tasks.Task.<>c.b__124_0(Object state) in System.Threading.Tasks\Task.cs:line 2028
at Microsoft.UI.Dispatching.DispatcherQueueSynchronizationContext.<>c__DisplayClass2_0.b__0()
```
Contributor guide
Research direction
Reproduce the Windows permission-removal case, then start with src/CommunityToolkit.Maui.Camera/Extensions/CameraViewExtensions.windows.cs and follow the stack through Providers/CameraProvider.windows.cs, CameraProvider.shared.cs, CameraManager.shared.cs, and Handlers/CameraViewHandler.shared.cs. Trace how UnauthorizedAccessException and an empty available-cameras list reach ConnectHandler. Done means reopening CameraView after camera permission removal no longer produces an unhandled crash, or exposes a usable way for the app to handle the error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- desktop
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100