← Back to Pull requests

fix(trainer): make _inject_trainer_hf_home thread-safe for concurrent calls #279

Closed
unknown closed this pull request without merging on Sep 29, 2026 14:40 UTC
View on GitHub
unknown unknown commented on Sep 20, 2026 17:49 UTC
Author

Description

When concurrent training requests call _inject_trainer_hf_home, the SDK monkey-patch previously had two issues:
1. original was captured outside _hf_home_lock, allowing a thread to capture a prior _patched closure and permanently leak the patch into the global module upon exit.
2. _hf_home_lock was held across yield, unnecessarily serializing all fine_tune() calls during remote Kubernetes API network calls.

This PR addresses both issues by:

  • Using standard library contextvars.ContextVar to isolate HF_HOME injection per execution context without cross-thread contamination.

  • Tracking active invocations with a reference counter, holding _hf_home_lock only for microsecond state updates rather than across remote I/O.

  • Reverting k8s_utils.get_trainer_cr_from_builtin_trainer to the original function when all active contexts exit.

  • Adding concurrency unit tests with threading.Barrier to ensure deterministic execution and function identity restoration.
  • Related Issue

    Fixes #278

    Checklist

  • I have read the [CONTRIBUTING](../CONTRIBUTING.md) guide

  • Tests pass locally (make test-python)

  • Linting passes (make verify)

  • Documentation updated (if applicable)

  • My commits are signed off (git commit -s)
  • Testing

  • Unit tests

  • Integration tests

  • E2E tests

  • Manually tested (describe below)
  • Ran unit tests:

    pytest -q kubeflow_mcp/trainer/api/training_test.py -k "test_inject_trainer_hf_home"
    pytest -q kubeflow_mcp/trainer/api/sdk_contracts_test.py
    

    All tests pass cleanly.

    Diff: 199 lines (+149 / -50) across 2 files Reviewer: abhijeet-dhumal Review duration: 12771m
    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
    0.36
    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.317
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #279"
    general weight: 0.35
    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: 36.1% (MEDIUM) · Higher = larger blast radius
    36.1%
    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.3% · Safe to merge
    14.3%