fix(trainer): make _inject_trainer_hf_home thread-safe for concurrent calls #279
All Vouch review confidence checks passed
Residual risk 0.14 = Change Risk (0.36) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.36 · 199 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.317
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #279"
general
weight: 0.35
abhijeet-dhumal
Description
When concurrent training requests call
_inject_trainer_hf_home, the SDK monkey-patch previously had two issues:1.
originalwas captured outside_hf_home_lock, allowing a thread to capture a prior_patchedclosure and permanently leak the patch into the global module upon exit.2.
_hf_home_lockwas held acrossyield, unnecessarily serializing allfine_tune()calls during remote Kubernetes API network calls.This PR addresses both issues by:
contextvars.ContextVarto isolateHF_HOMEinjection per execution context without cross-thread contamination._hf_home_lockonly for microsecond state updates rather than across remote I/O.k8s_utils.get_trainer_cr_from_builtin_trainerto the original function when all active contexts exit.threading.Barrierto ensure deterministic execution and function identity restoration.Related Issue
Fixes #278
Checklist
make test-python)make verify)git commit -s)Testing
Ran unit tests:
All tests pass cleanly.