From d777571cb1828de2bae94bda69250b3df1d79a3b Mon Sep 17 00:00:00 2001 From: David Koski Date: Fri, 3 Oct 2025 10:52:30 -0700 Subject: [PATCH] ensure that the ErrorHandler is installed - this will force all fatal errors to go through the swift code rather than the mlx-c code - and produce a fatalError which will stop in the debugger --- Source/MLX/ErrorHandler.swift | 5 +++++ Source/MLX/MLXArray.swift | 3 +++ 2 files changed, 8 insertions(+) diff --git a/Source/MLX/ErrorHandler.swift b/Source/MLX/ErrorHandler.swift index f7d16d82..85faa1f2 100644 --- a/Source/MLX/ErrorHandler.swift +++ b/Source/MLX/ErrorHandler.swift @@ -282,6 +282,11 @@ private let errorHandler: ErrorHandler = { return ErrorHandler() }() +/// Ensure that the error handler is installed. +func initError() { + _ = errorHandler +} + /// Forward the error to the `ErrorHandler` singleton. See `errorHandler` (above) for how this is /// installed. private func errorHandlerTrampoline(message: UnsafePointer?, data: UnsafeMutableRawPointer?) diff --git a/Source/MLX/MLXArray.swift b/Source/MLX/MLXArray.swift index fe5dbeae..2fefccca 100644 --- a/Source/MLX/MLXArray.swift +++ b/Source/MLX/MLXArray.swift @@ -14,6 +14,9 @@ public final class MLXArray { /// /// This initializer is for `Cmlx` interoperation. public init(_ ctx: consuming mlx_array) { + // We don't have lifecycle control over the MLX system but all interesting + // paths will come through here -- make sure the error handler is installed. + initError() self.ctx = ctx }