Skip to content

thread safety -- hold lock while calling stream sync#323

Merged
davidkoski merged 1 commit intomainfrom
sync
Dec 18, 2025
Merged

thread safety -- hold lock while calling stream sync#323
davidkoski merged 1 commit intomainfrom
sync

Conversation

@davidkoski
Copy link
Collaborator

Proposed changes

Observed: stream synchronization is not thread safe in roughly the same way that eval is not (if this were to be called while asyncEval was running it can manipulate the same global state in the command encoders). Hold the eval lock while calling.

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)

@davidkoski davidkoski requested a review from awni December 18, 2025 17:47
mlx_synchronize(ctx)
_ = evalLock.withLock {
mlx_synchronize(ctx)
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is the same pattern we use for all calls into the mlx runtime.

Copy link
Member

@awni awni left a comment

Choose a reason for hiding this comment

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

Looks good.. some day MLX will be thread safe 🤞

@davidkoski davidkoski merged commit 15b6ade into main Dec 18, 2025
6 checks passed
@davidkoski davidkoski deleted the sync branch December 18, 2025 21:57
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.

2 participants