← Back to Pull requests

Fail local Transformers turns cleanly when generate() raises #856

Merged
omkar-334 merged 2 files (379 lines) on Nov 30, 2026 12:33 UTC
View on GitHub
omkar-334 omkar-334 commented on Nov 19, 2026 10:53 UTC
Author

Refs #850. When generate() raises on the worker thread, the TextIteratorStreamer never gets its end marker, so the local Transformers handlers keep waiting and the response never finishes. This stores the error, ends the streamer, and re-raises after streaming so the existing error path ends the response.

Changes

  • Add _start_transformers_generation on BaseLanguageModelHandler. Both the text and vision handlers use it, replacing the duplicated _locked_pipe and _locked_generate.

  • If generation raises, the helper stores the error and ends the streamer so the reader stops waiting.

  • If streamer.end() itself fails (it decodes the cached tokens, which can be what failed), the helper queues the stop marker directly, so the reader is still released.

  • _finish_transformers_generation re-raises the stored error as a RuntimeError. The existing handler in process() turns that into an EndOfResponse with an error.

  • If the response was already cancelled or the server is shutting down, the error is only logged and not reported.

  • The MLX paths are unchanged.
  • Testing

    tests/test_transformers_generation_failure.py runs both handlers:

  • failure before any tokens ends the response with the error, and the next turn works with the lock released

  • failure after some tokens keeps the sent text and reports the error

  • failure after a cancel is not reported

  • the streamer expects a new prompt again after a failure

  • a failure inside the streamer's own decoding still ends the response
  • Against the unmodified code 8 of these 10 fail by hanging. The 2 that pass are the cancel cases, which never hang.

    Real model check with a tiny random Llama built locally and the trigger from the issue (do_sample=True, temperature=0.0):

    | | Failing turn | Next turn |
    |---|---|---|
    | Before | never finished (15 s timeout) | not reached |
    | After | ended in 0.97 s with the temperature (=0.0) error | works |

    Checks: full suite 4037 passed, 13 skipped, 1 failed. The one failure is tests/openai_realtime/test_webrtc.py::TestWebRTCLoopback::test_close_awaits_pending_ice_checks (aioice did not start a connectivity check), which fails the same way on untouched main in my environment. ruff check, ruff format --check and mypy src are clean.

    Not covered: a real downloaded model, GPU runs, and the MLX paths.

    Behavior note

    On a mid stream failure, sentences already sent stay sent. A partial sentence still buffered is dropped and the response ends with the error.

    Diff: 379 lines (+346 / -33) across 2 files Reviewer: andimarafioti Review duration: 15939m
    All Vouch review confidence checks passed
    Residual risk 0.13 = Change Risk (0.40) × (1 − Review Confidence (0.68)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.40 · 379 lines diff across 2 files
    0.40
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.35 · 1 substantive comment(s)
    0.35
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    67%
    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.355
    Real PR Conversation Timeline
    2 items
    andimarafioti andimarafioti approved these changes on Nov 30, 2026 12:33 UTC
    Approved Reviewer
    Reviewers
    Assignees
    omkar-334 omkar-334
    Labels
    risk: low confidence: 67.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: 40.0% (MEDIUM) · Higher = larger blast radius
    40.0%
    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: 67.5% · Lower = rushed or shallow review
    67.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: 13% · Safe to merge
    13%