Skip to content

[camera_avfoundation] Fix Swift 6 Sendability warning in CaptureDevice protocol#11634

Open
teclas-gif wants to merge 2 commits intoflutter:mainfrom
teclas-gif:fix/camera-avfoundation-sendability-swift6
Open

[camera_avfoundation] Fix Swift 6 Sendability warning in CaptureDevice protocol#11634
teclas-gif wants to merge 2 commits intoflutter:mainfrom
teclas-gif:fix/camera-avfoundation-sendability-swift6

Conversation

@teclas-gif
Copy link
Copy Markdown

@teclas-gif teclas-gif commented May 2, 2026

Description

Fixes a Swift 6 Sendability warning produced when building camera_avfoundation with SWIFT_STRICT_CONCURRENCY=targeted or higher (the Xcode 16+ default).

Root cause

CaptureDevice.swift declares:

func setExposureTargetBias(
  _ bias: Float, completionHandler handler: ((CMTime) -> Void)?)

AVCaptureDevice (the Apple system type that conforms to this protocol) exposes the same method with a @Sendable closure:

func setExposureTargetBias(
  _ bias: Float, completionHandler handler: (@Sendable (CMTime) -> Void)?)

The missing @Sendable annotation on the protocol requirement creates a Sendability mismatch. Under SWIFT_STRICT_CONCURRENCY=targeted this is a warning; in Swift 6 language mode it is a hard error.

Fix

Three files changed:

  1. CaptureDevice.swift — adds @Sendable to the completion handler in the protocol declaration to match AVFoundation's signature.
  2. MockCaptureDevice.swift — updates the mock to match the new protocol signature (both the stub property type and the method).
  3. CameraExposureTests.swift — adds testSetExposureTargetBias_completionHandlerIsInvoked, which:
    • Exercises the previously-untested non-nil completion handler path
    • Serves as a compile-time regression guard: MockCaptureDevice conforms to CaptureDevice, so if @Sendable is removed from the protocol the mock conformance breaks at compile time

Tests

  • Added testSetExposureTargetBias_completionHandlerIsInvoked in CameraExposureTests.swift
  • Updated MockCaptureDevice stub and method signatures to use @Sendable

Checklist

…e protocol

The `setExposureTargetBias` completion handler in the `CaptureDevice`
protocol was declared as `((CMTime) -> Void)?`, while the underlying
`AVCaptureDevice` conformance expects `(@sendable (CMTime) -> Void)?`.
This mismatch produces a warning under `SWIFT_STRICT_CONCURRENCY=targeted`
and is a hard error in Swift 6 language mode.

Adds `@Sendable` to the closure parameter in the protocol declaration to
match the AVFoundation signature.
@flutter-dashboard
Copy link
Copy Markdown

It looks like this pull request may not have tests. Please make sure to add tests or get an explicit test exemption before merging.

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing. If you believe this PR qualifies for a test exemption, contact "@test-exemption-reviewer" in the #hackers channel in Discord (don't just cc them here, they won't see it!). The test exemption team is a small volunteer group, so all reviewers should feel empowered to ask for tests, without delegating that responsibility entirely to the test exemption group.

@google-cla
Copy link
Copy Markdown

google-cla Bot commented May 2, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request updates the CaptureDevice protocol in camera_avfoundation by adding the @Sendable attribute to the setExposureTargetBias completion handler, along with a version bump to 0.10.1+1. Feedback indicates that this change will cause compilation failures in mock implementations that have not been updated to match the new protocol signature.

var maxExposureTargetBias: Float { get }
func setExposureTargetBias(
_ bias: Float, completionHandler handler: ((CMTime) -> Void)?)
_ bias: Float, completionHandler handler: (@Sendable (CMTime) -> Void)?)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

This change to the protocol requirement will cause a compilation error in MockCaptureDevice.swift (and any other implementations of this protocol) because the method signature must match the protocol requirement exactly, including the @Sendable attribute. The PR description's claim that mocks are not affected is incorrect; they must be updated to include @Sendable in both the method signature and the setExposureTargetBiasStub property type to maintain protocol conformance.

- Updates MockCaptureDevice to use @sendable on setExposureTargetBias,
  keeping it in sync with the CaptureDevice protocol requirement.
- Adds testSetExposureTargetBias_completionHandlerIsInvoked to exercise
  the non-nil handler path and serve as a compile-time regression guard:
  if @sendable is removed from the protocol the mock conformance breaks.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant