Repository navigation
Fix crash when loading model files from Python file-like object - #2110
Merged
Merged
Conversation
jordimas
reviewed
Oct 10, 2026
Purfview
marked this pull request as draft
October 11, 2026 03:00
Purfview
marked this pull request as ready for review
October 11, 2026 03:50
Collaborator
|
Thanks so much! |
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.
When a file-like object is passed, CTranslate2 crashes with Windows error 0xC0000005 (access violation). [bytes object works]
Loading model files from memory (a file-like or bytes objects) was implemented here: #1030 [
filesarg documentation ]I tested the fix on Windows with both file-like and bytes objects - works fine.
Code to reproduce a file-like object crash:
AI generated explanation:
Problem
When loading model files from file-like objects,
create_model_reader()stores the dictionary value inauto content, which deduces to a non-owningpy::handle.When
content = read()assigns the result of a file-like object'sread()method, only the underlying Python object pointer is copied. The temporary owningpy::objectreturned byread()is destroyed at the end of the statement, potentially leavingcontentpointing to a deallocated object.For ordinary binary file handles, the returned
bytesobject is not otherwise retained. The subsequent conversion tostd::stringtherefore accesses a dangling Python object, causing a use-after-free that can crash the process.Fix
Store the dictionary value in an owning
py::object, created withpy::reinterpret_borrow<py::object>(). This ensures the contents returned byread()remain alive while being converted and registered with the in-memory model reader.