Skip to content

Conversation

@davidkoski
Copy link
Collaborator

Proposed changes

Please include a description of the problem or feature this PR is addressing. If there is a corresponding issue, include the issue #.

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)

- these mutate global state and are not thread safe
- fix #337
@davidkoski davidkoski requested a review from awni January 16, 2026 16:49
// but will be able to re-evaluate with fresh state if needed
evalLock.lock()
var compiled = mlx_closure_new()
mlx_detail_compile(&compiled, innerClosure, id, shapeless, [], 0)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Of course this is mutating global state so it needs to be protected -- it is infrequent enough that it escaped notice for so long.

evalLock.withLock {
let closure = new_mlx_closure(f)
_ = inAxes32.withUnsafeBufferPointer { inAxesBuf in
mlx_detail_vmap_trace(
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

vmap has a similar call

assertEqual(gv(x), x * 2)
}

func testCompileThreadSafety() async throws {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Repro cases from the issue -- these crash reliably (maybe not 100% because it is a race, but mostly) without the fix.

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.

Thanks! We should really fix that in MLX core as well.

@davidkoski davidkoski merged commit b058eda into main Jan 22, 2026
7 checks passed
@davidkoski davidkoski deleted the compile-threadsafety branch January 22, 2026 19:22
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] SIGABRT for multiple inference requests concurrently on GPT-oss (vector::back on empty)

2 participants