Fix crash when loading model files from Python file-like object #2110
All Vouch review confidence checks passed
Residual risk 0.15 = Change Risk (0.37) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.37 · 231 lines diff across 2 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.326
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #2110"
general
weight: 0.35
jordimas
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: https://github.com/OpenNMT/CTranslate2/pull/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.