Skip to content

OpenAI TLS faults (BadRecordMac) fail immediately; retry classifier blinded by anyhow .context() masking #346

Closed as not planned

Description

@1jehuang

Summary

Transient TLS transport faults (received fatal alert: BadRecordMac, broken pipe, connection reset, etc.) on the OpenAI path fail immediately instead of being retried, and the claude / copilot / openrouter / openai retry classifiers can be blinded to such causes when an error is wrapped behind anyhow's .context(...).

Reported by a user (v0.24.0) who repeatedly saw:

Stream error: IO error: received fatal alert: BadRecordMac

A BadRecordMac is a TLS record-authentication failure, typically caused by a corrupting middlebox / flaky VPN / proxy / NIC, or a corrupted reused TLS connection. It is transient: a fresh connection on retry almost always succeeds, which is exactly how every other provider already treats it.

Root cause

Two related defects in the provider retry-classification path:

1. OpenAI maintained a separate allowlist with no TLS terms

crates/jcode-base/src/provider/openai_stream_runtime.rs::is_retryable_error was a standalone substring list that did not call the shared is_transient_transport_error and omitted every TLS term (badrecordmac, received fatal alert, DNS errors, connection aborted, HTTP/2 stream error, etc.).

Every other provider (anthropic, claude, copilot, openrouter) delegates to the shared is_transient_transport_error (routing.rs), which already handles these. OpenAI was the lone outlier, so a BadRecordMac on the websocket path surfaced to the user at attempt=1:

[ERROR] EVENT event=PROVIDER_STREAM_LIFECYCLE attempt=1 ... error="Stream error: IO error: received fatal alert: BadRecordMac" phase=attempt_failed provider=openai will_retry=false

(broken pipe happened to be in OpenAI's list, so those retried 3x and only gave up after exhausting attempts; BadRecordMac was not, so it gave up immediately.)

2. Classifier fed the masked top-level error, not the source chain

claude, copilot, openrouter, and openai classify on e.to_string(). When a transport cause is wrapped via anyhow's .context("Failed to send request to ... API"), to_string() returns only the top-level context and masks the underlying cause. Verified with reqwest 0.12.28 + rustls (the shipped config):

e.to_string()        = "Failed to send request to Anthropic API"            // BadRecordMac hidden
format!("{e:#}", e)  = "Failed to send request to ...: ... received fatal alert: BadRecordMac"  // visible

So even though the shared classifier knows badrecordmac, it never sees it because the input string is masked. (The anthropic provider already worked around this by classifying on format!("{e:#}"); the others did not.)

This second defect also explains why the masked string "Failed to send request to Anthropic API" is itself non-retryable, while the user still saw retries on the anthropic path: anthropic already uses the {e:#} form.

Reproduction

Added a standalone, deterministic fault-injection harness: scripts/repro/tls-bad-record-mac/.

It wires reqwest (rustls) -> corrupting TCP proxy -> real rustls TLS server. The proxy flips one byte inside the first large client->server TLS application_data record, so the server's AEAD check fails and it emits a real bad_record_mac fatal alert, producing the exact user-facing error. It then runs the verbatim shipping classifier (and the old vs new OpenAI list) and asserts the old logic misses it while the fix retries.

cd scripts/repro/tls-bad-record-mac && cargo run
# REPRODUCED BadRecordMac: true
# OLD is_retryable = false   NEW is_retryable = true
# to_string (masked) -> classify = false ; {:#} chain -> classify = true
# SUCCESS: realistic BadRecordMac reproduced and both fixes validated.

Stable across 15/15 runs.

Fix

  • OpenAI is_retryable_error now delegates to the shared is_transient_transport_error (keeps its extra OpenAI-specific markers).
  • claude / copilot / openrouter / openai now classify on format!("{e:#}") (full anyhow source chain) so a .context(...)-wrapped transport cause is visible.
  • Regression tests for the TLS terms + the masked-context case. Existing negative auth tests still pass.

PR: branch fix/transport-retry-classification.

Follow-up (not in this fix, for discussion)

  • Retry budgets are MAX_RETRIES=3 (claude CLI=5) with exponential backoff 1s/2s/4s and no jitter. During a sustained bad-network window these exhaust in seconds. Consider modest bumps and jitter (the latter helps swarm thundering-herd), traded off against longer hangs when a network is genuinely down.

Activity

  1. 1jehuang commented on Jun 9, 2026

    @1jehuang
    OwnerAuthor

    Duplicate of #338, which predates this and is broader. Moved the unique findings (anyhow .context() masking root cause + reproduction harness) and the PR link to #338. PR #347 now references #338.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions