Repository navigation
fix: avoid shared CIContext in media processing - #268
Conversation
There was a problem hiding this comment.
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
CIContextinsideMediaProcessing.asMLXArray(...)and used it forrender(...). - 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.
| var data = Data(count: w * h * bytesPerPixel) | ||
| let renderContext = CIContext() | ||
| data.withUnsafeMutableBytes { ptr in |
| image, toBitmap: ptr.baseAddress!, rowBytes: bytesPerRow, bounds: image.extent, | ||
| format: format, colorSpace: colorSpace) | ||
| context.clearCaches() | ||
| renderContext.clearCaches() |
|
I am curious about this one: What issue are you seeing here? This is already building using swift 6.1 and I haven't seen an error. |
|
Thanks for checking this. You're right that the current Apple documentation lists The reason I opened this was a downstream SwiftPM build that depends on That downstream workflow runs on a 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. |
|
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. |
|
That sounds reasonable. I can try the My only concern is similar to yours: 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. |
|
It would be great if there were something like @preconcurrency import CoreImage.CIFilterBuiltinsI verified that it worked with Xcode 16.4 and current Xcode seems fine with it too. |
|
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.CIFilterBuiltinsand restored the shared I could not run a local Swift build in this environment because |
…ml-explore#268) Merge the prepared correction after green CI, signed-head verification and combined integration validation. No provider pin or deployment changes.
Summary
CIContextfromMediaProcessing.CIContextinsideasMLXArray(...)before rendering.CIContextwhile preserving existing rendering behavior.Test Plan
Meapri/mlx-vlmagainst this fork branch.let context is not concurrency-safe because non-Sendable type CIContext may have shared mutable state.