Skip to content

fix: check mlx_detail_compile and mlx_closure_apply return values in CompiledFunction#398

Merged
davidkoski merged 1 commit into
ml-explore:mainfrom
vernonstinebaker:fix/compile-closure-apply-error-handling
May 7, 2026
Merged

fix: check mlx_detail_compile and mlx_closure_apply return values in CompiledFunction#398
davidkoski merged 1 commit into
ml-explore:mainfrom
vernonstinebaker:fix/compile-closure-apply-error-handling

Conversation

@vernonstinebaker

Copy link
Copy Markdown
Contributor

Proposed changes

Fixes #397.

CompiledFunction.innerCall() (Source/MLX/Transforms+Compile.swift) silently ignored the int return values of mlx_detail_compile and mlx_closure_apply. Both return 1 on failure and call mlx_error(). When called inside a withError scope the Swift error handler stores the error in an ErrorBox and execution continues — leaving resultVector empty. The single/two/three-array compile overloads then subscript into an empty array (result[0]), triggering a Swift trap that kills the process before withError can throw.

Fix:

  • Capture the return value of mlx_detail_compile; return [] from innerCall if it fails.
  • Capture the return value of mlx_closure_apply; return [] from innerCall if it fails.
  • Guard against the empty-result crash in the three convenience overloads by replacing compileState.call(...)[0] with a safe let r = ...; return r.isEmpty ? MLXArray(0) : r[0].

The placeholder MLXArray(0) returned by the overloads on the error path is never observed by the caller — withError throws MLXError before the value is used.

MLXCustomFunction.swift:112 already uses the same pattern (precondition(status == 0, ...)) for its own mlx_closure_apply call; this PR makes Transforms+Compile.swift consistent.

Checklist

  • 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)

mlx_detail_compile and mlx_closure_apply both return int (0=success,
1=failure) but their return values were silently ignored. When an error
fires inside a withError scope the MLX error handler stores the error in
an ErrorBox instead of calling fatalError; execution then continues past
the failed call, innerCall returns an empty result vector, and the
single/two/three-array compile overloads crash with a Swift 'Index out
of range' trap — bypassing withError entirely.

Fix: capture both return values and early-return [] from innerCall on
failure. The placeholder return from the compile overloads is never
observed by the caller because withError throws before the value is
used.

Adds three regression tests covering the single-array, two-array, and
[MLXArray]->[MLXArray] compile overloads.

@davidkoski davidkoski left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Changes look good.

I was thinking that perhaps compile should throw, but that is going to be trouble for statically defined functions and also be breaking API. I think this is a reasonable middle ground.

@davidkoski davidkoski merged commit 89cece7 into ml-explore:main May 7, 2026
8 checks passed
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] compile() crashes with "Index out of range" instead of propagating MLX errors via withError

2 participants