← Back to Pull requests

Fix crash when loading model files from Python file-like object #2110

Closed
unknown closed this pull request without merging on Oct 11, 2026 05:36 UTC
View on GitHub
unknown unknown commented on Oct 10, 2026 12:48 UTC
Author

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 files arg [documentation ]
I tested the fix on Windows with both file-like and bytes objects - works fine.

Code to reproduce a file-like object crash:

import os
import ctranslate2

model_dir = r"D:\faster-whisper-tiny"

files = {
    filename: open(os.path.join(model_dir, filename), "rb")
    for filename in os.listdir(model_dir)
    if os.path.isfile(os.path.join(model_dir, filename))
}

model = ctranslate2.models.Whisper("test-tiny", files=files)

print("No crash!", flush=True)

AI generated explanation:

Problem

When loading model files from file-like objects, create_model_reader() stores the dictionary value in auto content, which deduces to a non-owning py::handle.

When content = read() assigns the result of a file-like object's read() method, only the underlying Python object pointer is copied. The temporary owning py::object returned by read() is destroyed at the end of the statement, potentially leaving content pointing to a deallocated object.

For ordinary binary file handles, the returned bytes object is not otherwise retained. The subsequent conversion to std::string therefore 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 with py::reinterpret_borrow<py::object>(). This ensures the contents returned by read() remain alive while being converted and registered with the in-memory model reader.

Diff: 231 lines (+173 / -58) across 2 files Reviewer: jordimas Review duration: 1007m
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
0.37
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
0.17
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
60%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
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
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 60.5% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 37% (MEDIUM) · Higher = larger blast radius
37%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 60.5% · Lower = rushed or shallow review
60.5%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 14.6% · Safe to merge
14.6%