Skip to content

fix: avoid shared CIContext in media processing - #268

Merged
davidkoski merged 2 commits into
ml-explore:mainfrom
Meapri:fix/ci-context-sendable
May 11, 2026
Merged

davidkoski merged 2 commits into
ml-explore:mainfrom
Meapri:fix/ci-context-sendable

Conversation

@Meapri

@Meapri Meapri commented May 7, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Removes the shared file-level CIContext from MediaProcessing.
  • Creates a local CIContext inside asMLXArray(...) before rendering.
  • Avoids Swift 6.1 strict concurrency failures caused by a non-Sendable global CIContext while preserving existing rendering behavior.

Test Plan

  • Verified downstream SwiftPM build from Meapri/mlx-vlm against this fork branch.
  • The previous strict-concurrency error was:
    • let context is not concurrency-safe because non-Sendable type CIContext may have shared mutable state.

Copilot AI review requested due to automatic review settings May 7, 2026 06:14

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 updates MediaProcessing to avoid Swift 6.1 strict-concurrency failures by removing the file-level shared CIContext and instantiating a CIContext locally inside asMLXArray(...) for rendering.

Changes:

  • Removed the file-scope shared CIContext.
  • Created a local CIContext inside MediaProcessing.asMLXArray(...) and used it for render(...).
  • Updated cache-clearing calls to target the new local render context.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 169 to 171
var data = Data(count: w * h * bytesPerPixel)
let renderContext = CIContext()
data.withUnsafeMutableBytes { ptr in
Comment thread Libraries/MLXVLM/MediaProcessing.swift Outdated
image, toBitmap: ptr.baseAddress!, rowBytes: bytesPerRow, bounds: image.extent,
format: format, colorSpace: colorSpace)
context.clearCaches()
renderContext.clearCaches()
@davidkoski

Copy link
Copy Markdown
Member

I am curious about this one: CIContext is Sendable:

What issue are you seeing here? This is already building using swift 6.1 and I haven't seen an error.

@Meapri

Meapri commented May 8, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for checking this.

You're right that the current Apple documentation lists CIContext as conforming to Sendable.

The reason I opened this was a downstream SwiftPM build that depends on mlx-swift-lm. In that environment, the file-scope shared context triggered Swift's strict-concurrency checking with a diagnostic like:

MediaProcessing.swift:16:13: error: let 'context' is not concurrency-safe because non-'Sendable' type 'CIContext' may have shared mutable state

That downstream workflow runs on a macos-15 GitHub Actions runner and reports:

Apple Swift version 6.1.2 (swiftlang-6.1.2.1.2 clang-1700.0.13.5)

So this may be SDK/toolchain-version dependent, or specific to how the package is being built as a dependency. I agree the docs make this less straightforward than I initially thought. The intent here was just to avoid the file-scope shared value that triggered the diagnostic downstream, without changing the public API.

If this does not reproduce in this repo's supported build setup, I'm happy to close or rework the PR.

@davidkoski

Copy link
Copy Markdown
Member

Ah, that makes sense -- the older SDK may not have it marked as Sendable. Huh, that is an interesting one. We could mark the import as preconcurrency. I think that would fix it for the older SDK, but I wonder if that hides issues.

Our runners are newer SDKs, so I can't verify it there. I think I might have a mac with an older macOS where I could try it.

@Meapri

Meapri commented May 8, 2026

Copy link
Copy Markdown
Contributor Author

That sounds reasonable. I can try the @preconcurrency import CoreImage approach as an alternative if you prefer that over moving the CIContext into function scope.

My only concern is similar to yours: @preconcurrency would silence concurrency checking for the CoreImage import more broadly, while the current patch keeps the workaround local to the shared file-scope value that triggered the downstream diagnostic.

If you're able to test on an older SDK, that would be very helpful. I'm also happy to update this PR to whichever approach you think fits the project better.

@davidkoski

Copy link
Copy Markdown
Member

It would be great if there were something like #if sdk(> 26) but there isn't. I think the best we can do is:

@preconcurrency import CoreImage.CIFilterBuiltins

I verified that it worked with Xcode 16.4 and current Xcode seems fine with it too.

@Meapri

Meapri commented May 9, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for checking this on Xcode 16.4 and the current Xcode.

That approach makes sense to me. I updated the PR to use:

@preconcurrency import CoreImage.CIFilterBuiltins

and restored the shared CIContext, so the change now targets the SDK compatibility issue directly rather than changing the render context lifetime.

I could not run a local Swift build in this environment because swift is not installed here, but the patch is intentionally limited to the import/workaround you suggested.

@davidkoski davidkoski 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.

Thank you!

@davidkoski
davidkoski merged commit c2b20d8 into ml-explore:main May 11, 2026
3 checks passed
@davidkoski davidkoski mentioned this pull request Jun 21, 2026
3 of 4 tasks
davidtai pushed a commit to davidtai/mlx-swift-lm that referenced this pull request Oct 6, 2026
…ml-explore#268)

Merge the prepared correction after green CI, signed-head verification and combined integration validation. No provider pin or deployment changes.
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.

3 participants