Propagate IO errors from custom readers and writers - #126
Open
aleroot wants to merge 1 commit into
Open
Conversation
The read/write functions of `mlx_io_vtable` have no return value, so a failed IO operation can only be reported through `good()`. `CReader` and `CWriter` forwarded the call and returned, which silently dropped the error: the caller gets a buffer filled with garbage and no exception is ever raised. Check `good()` after each call and throw, mirroring what the readers and writers implemented in mlx do (`ParallelFileReader::read` and `FileWriter::write`). For readers this is what makes the error propagation added in ml-explore/mlx#3742 usable with a custom `mlx_io_reader`: the exception thrown from the read task of `Load::eval_cpu` now poisons the pending events of the stream and is re-thrown when the lazily loaded arrays are evaluated, instead of the load silently succeeding with truncated data. For writers the error surfaces directly from `mlx_save_writer()` / `mlx_save_safetensors_writer()`. Behavior is unchanged for vtables that never report a failure through `good()`.
This was referenced Aug 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
• The mlx_io_vtable read/write callbacks return void , so good() is the only error channel and CReader / CWriter never checked it, so failures were silently dropped and the caller got garbage data.
• Now mirrors mlx::core::io::ParallelFileReader::read / FileWriter::write , which already throw on short reads/writes.
Motivation
This is the missing first link for ml-explore/mlx#3742. With that fix, an exception from Load::eval_cpu 's read task poisons the stream's events and is re-thrown at eval time but only mlx's own file reader ever threw. Custom readers (e.g. mlx-swift's, see ml-explore/mlx-swift#427) could not report a truncated/failed read at all.