Skip to content

hold evalLock during mlx_save_safetensors etc#379

Open
davidkoski wants to merge 1 commit intomainfrom
save-unsafe
Open

hold evalLock during mlx_save_safetensors etc#379
davidkoski wants to merge 1 commit intomainfrom
save-unsafe

Conversation

@davidkoski
Copy link
Copy Markdown
Collaborator

@davidkoski davidkoski commented Mar 27, 2026

Proposed changes

Verified via concurrent tests in mlx-swift-lm.

Checklist

Put an x in the boxes that apply.

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed)

- fix #378
- save functions call eval internally and must be run under evalLock
@davidkoski davidkoski requested a review from angeloskath March 27, 2026 22:52
_ = try withError {
mlx_save_safetensors(path.cString(using: .utf8), mlx_arrays, mlx_metadata)
_ = evalLock.withLock {
mlx_save_safetensors(path.cString(using: .utf8), mlx_arrays, mlx_metadata)
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

internally these functions prepare contiguous arrays and use eval to realize them. eval is not thread safe so the entire thing has to be run under the evalLock.

See #378 for the use case.

davidkoski added a commit to ml-explore/mlx-swift-lm that referenced this pull request Mar 27, 2026
- some concurrency issues snuck in for test support
- fix and prevent more from coming in by switching Package to swift 6
- depends on ml-explore/mlx-swift#379
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] mlx_save_safetensors is not thread safe

1 participant