Skip to content

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

Open
vernonstinebaker wants to merge 1 commit intoml-explore:mainfrom
vernonstinebaker:fix/compile-closure-apply-error-handling
Open

fix: check mlx_detail_compile and mlx_closure_apply return values in CompiledFunction#398
vernonstinebaker wants to merge 1 commit intoml-explore:mainfrom
vernonstinebaker:fix/compile-closure-apply-error-handling

Conversation

@vernonstinebaker
Copy link
Copy Markdown

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.
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

1 participant