Skip to content

Fix connect camera cancellation#3172

Open
zhitaop wants to merge 5 commits intoCommunityToolkit:mainfrom
zhitaop:fix/connect-camera-cancellation
Open

Fix connect camera cancellation#3172
zhitaop wants to merge 5 commits intoCommunityToolkit:mainfrom
zhitaop:fix/connect-camera-cancellation

Conversation

@zhitaop
Copy link
Copy Markdown
Contributor

@zhitaop zhitaop commented Mar 24, 2026

Description of Change

This PR improves CameraViewHandler lifecycle by making camera connection cancellable, preventing failures when the handler is disconnected or disposed while ConnectCamera() is still running.

Changes:

  • Added cancellationTokenSource to track and cancel camera connection.
  • Updated ConnectHandler() to:
    • cancel and dispose any previous token source
    • create a fresh token source for the new connection
    • safely ignore OperationCanceledException when cancellation was triggered by the handler itself
    • call DisconnectHandler() if camera connection fails for any other reason
  • Updated Dispose() to cancel the active token source. This ensures ongoing camera connection is cancelled if handler is disconnected

Linked Issues

PR Checklist

  • Has a linked Issue, and the Issue has been approved(bug) or Championed (feature/proposal)
  • Has tests (if omitted, state reason in description)
  • Has samples (if omitted, state reason in description)
  • Rebased on top of main at time of PR
  • Changes adhere to coding standard
  • Documentation created or updated: https://github.com/MicrosoftDocs/CommunityToolkit/pulls

Additional information

Copilot AI review requested due to automatic review settings March 24, 2026 05:20
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR aims to prevent Windows crashes when navigating away from CameraView while the camera connection is still in progress, by making ConnectCamera() cancellable and guarding against double-dispose.

Changes:

  • Added a handler-level CancellationTokenSource to cancel in-flight camera connection work.
  • Added an isDisposed guard to prevent repeated disposal.
  • Updated ConnectHandler() to cancel any prior connection attempt and ignore self-triggered cancellation.

catch (Exception)
{
DisconnectHandler(platformView);
throw;
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.

we don't throw here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed the throw statement

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.

Comment on lines +100 to +103
catch (Exception)
{
DisconnectHandler(platformView);
}
Copy link

Copilot AI Mar 24, 2026

Choose a reason for hiding this comment

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

ConnectHandler catches all exceptions and unconditionally calls DisconnectHandler(platformView). If the handler is already disconnecting/disposing, DisconnectHandler can throw (e.g., CameraManager field already set to null and the property throws), which defeats the goal of preventing crashes. Consider guarding this path (e.g., if cancellation was requested / handler is disposed / cameraManager is null, just return), and log the exception (Trace) for non-cancellation failures before disconnecting.

Copilot uses AI. Check for mistakes.
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] CameraView crashes if exited too quickly on Windows

3 participants