Skip to content

test(mlx): trip on the cross-thread eval break that starts at core v0.31.2 - #115

Merged
magicnight merged 2 commits into
mainfrom
chore/cross-thread-eval-tripwire
Aug 28, 2026
Merged

magicnight merged 2 commits into
mainfrom
chore/cross-thread-eval-tripwire

Conversation

@magicnight

@magicnight magicnight commented Aug 28, 2026 •

Copy link
Copy Markdown
Owner

The fork has a second job nobody wrote down

We pin mlx-core v0.31.1 to carry nine correctness cherry-picks. It turns out that pin is also holding us on the last core release whose concurrency model matches mlx-swift's Swift layer — and that was luck, not design.

ml-explore/mlx#3348 made CommandEncoder and default_stream() thread-local, first released in core v0.31.2. Every OS thread that asks for a default stream now gets its own, whose command encoder is registered only in that thread's registry. mlx-swift predates this:

// Source/MLX/Stream.swift
public static let gpu = Stream(mlx_default_gpu_stream_new())
public static let cpu = Stream(mlx_default_cpu_stream_new())

Process-global statics, materialized on whichever thread first touches MLX, handed to every op via Device.defaultStream. From v0.31.2 onward, evaluating from any other OS thread gives:

Fatal error: There is no Stream(gpu, 0) in current thread.

A process abort, not a throw.

We are squarely in the blast radius. A Hummingbird server answering concurrent requests, a SwiftUI app, and Swift Concurrency tasks that hop cooperative-pool threads all evaluate off the thread that booted MLX. This would not be a corner case for us; it would be total.

Upstream: ml-explore/mlx-swift#457, open since 2026-08-18 with no maintainer response. The fix belongs on the mlx-c side — point the two default-stream entry points at new_thread_unsafe_stream, which registers in the global registry the Swift statics were built for. That binding landed in ml-explore/mlx-c#122.

What this PR adds

CrossThreadEvalTripwireTests — a transcription of the upstream reproduction: materialize Stream.gpu on the test thread, then evaluate from a detached one.

It has never been observed to fail, and it says so in its own header. Demonstrating a red would mean building against core >= 0.31.2, which is the entire bump this guards. Unlike the parity suites, this is not an empirically inverted guard — it is a faithful transcription held green against the current pin. A failure will most likely arrive as a crashed run rather than a red assertion, since the abort takes the runner with it; the timeout branch only catches the milder hang.

Also: what a move off v0.31.1 actually costs

.claude/distribution.md gains a section, drawn from a working 0.32.1 bump published on mlx-swift#450:

what where it bites
qmv() passes 6 template args to 5-param kernel templates JIT-only builds — ours. First quantized matvec aborts. mlx#4350 → mlx#4372, already in core v0.32.2
generated *_nax.cpp reference symbols the header gates out below deployment target 26.2 we target macOS 14
tools/update-mlx.sh kernel make-list missing gemv / searchsorted at 0.32.x our regeneration workflow — regenerated sources fail to link
mlx-c compile-cache calls need the 0.32.x handle mlx-c main now pins core v0.32.2
FFT C API grew a norm parameter ~20 call sites

Upstream state as of this PR: mlx-c woke up on 2026-08-27 and now pins core v0.32.2, so the blocker davidkoski named on #441 is cleared. mlx-swift's own bump, #450, is open but conflicted (172 files, +20501/-7758).

Verification

check result
CrossThreadEvalTripwireTests passes; xcodebuild exit 0
MacMLXCore gated xcodebuild test 868 tests / 100 suites, TEST SUCCEEDED

No production code changes.


Note

Low Risk
Documentation and a gated regression test only; no runtime or vendored MLX changes.

Overview
Adds CrossThreadEvalTripwireTests, which reproduces the mlx-swift#457 scenario: touch MLX on the test thread, then MLX.eval on a detached thread. It is meant to catch a future mlx-core bump to ≥ v0.31.2 that ships thread-local default streams without the mlx-c new_thread_unsafe_stream fix—failures may show up as a process abort rather than a failed assertion. The suite uses the same mlxMetallibIsAvailable gate as other Engine MLX tests.

.claude/distribution.md gains “Moving off v0.31.1 is not a version bump”, documenting that pin exit is more than a one-line change: the cross-thread stream issue first, plus a checklist table (quantized qmv, NAX generated sources on macOS 14, update-mlx.sh kernel list, mlx-c compile cache, FFT C API) drawn from upstream bump work.

No production or dependency pin changes in this PR.

Reviewed by Cursor Bugbot for commit 36ea800. Configure here.

….31.2

Our pin, mlx-core v0.31.1, is the last release before ml-explore/mlx#3348 made
CommandEncoder and default_stream() thread-local. mlx-swift's Stream.gpu and
Stream.cpu are process-global statics materialized on whichever thread first
touches MLX, so from v0.31.2 onward an eval on any other OS thread dies with

    Fatal error: There is no Stream(gpu, 0) in current thread.

That is a process abort, not a throw, and we are squarely in its path: a
Hummingbird server answering concurrent requests, a SwiftUI app, and Swift
Concurrency tasks that hop cooperative-pool threads all evaluate off the thread
that booted MLX. Filed upstream as ml-explore/mlx-swift#457, open with no
maintainer response; the fix needs mlx-c's two default-stream entry points to
use new_thread_unsafe_stream, which was bound in ml-explore/mlx-c#122.

So the fork is doing two jobs, and only one of them was written down. It
carries nine correctness cherry-picks, and it also holds us on the last core
whose concurrency model matches the Swift layer. This test guards the second.

What it does not do is fail. Demonstrating that would mean building against
core >= 0.31.2, which is the whole of the bump it guards, so unlike the parity
suites it has never been observed red. It is a transcription of the upstream
reproduction, held green against the current pin, and the header says so.

Also documents what a move off v0.31.1 actually costs, from a working 0.32.1
bump published on mlx-swift#450: the qmv template-arity abort that hits
JIT-only builds like ours (fixed in core v0.32.2), NAX sources referencing
symbols the header gates out below deployment target 26.2, update-mlx.sh
missing gemv and searchsorted at 0.32.x, the mlx-c compile-cache handle, and
the new FFT norm parameter.
The 45-day freshness guard on site content started failing every build on
2026-08-24: the competitor entries were last verified 2026-07-10. That guard
exists because the public site names each competitor's exact version, and four
of the five had genuinely moved — this was not a date technicality.

  Ollama      v0.31.2        -> v0.33.1        (two minors)
  oMLX        v0.4.4         -> v0.6.3         (two minors)
  Swama       v2.2.0         -> v2.3.0
  SwiftLM     b648           -> b704
  LM Studio   0.4.19 Build 2 -> Bionic 1.1.0   (new version scheme)

Each claim was re-read against the source rather than the version string alone:

  - oMLX is still Python-cored at 0.6.3 — 76.5% Python, requires Python
    3.11-3.13, still a FastAPI server behind the SwiftUI menu-bar app — so our
    positioning holds. It now requires macOS 15.0+, and its focus has grown to
    continuous batching, prefix-shared paged caching, multi-token prediction,
    and distributed serving.
  - LM Studio still ships as app + llmster headless daemon + lms CLI, so only
    the version string changed. Its Python-bundling wording is now
    version-neutral, since the bundled version is no longer stated.
  - Ollama's MLX runner gained structured output in v0.33.1.
  - Swama gained prompt and KV-cache reuse on the chat completions path.
  - SwiftLM b704 is a version-display fix; nothing descriptive changed.

The test dates move with the content, and they are coupled in three ways that
each surfaced as its own failure:

  - the pinned `today` in 27 assertions, which must sit at or after the newest
    snapshot date and before the facts expire (2026-09-03), so 2026-08-28;
  - the negative assertion that staleness IS detected, which must sit after
    the facts expire and after every snapshot, so 2026-09-05;
  - the five future-date fixtures written as literals one day past the old
    `today`, and the snapshot of exact verified versions.

Facts, releases, and pages are untouched: at 40 days they are still inside the
window, and v0.8.0 remains the current release. They expire 2026-09-03.
@magicnight
magicnight merged commit 39df4ec into main Aug 28, 2026
4 checks passed
@magicnight
magicnight deleted the chore/cross-thread-eval-tripwire branch August 28, 2026 15:21
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.

1 participant