Skip to content

Commit 049bfa9

Browse files
lesnik512claude
andcommitted
fix(retry): scope streaming-body refusal note to its actual blocker site
The early-out branch for method ineligibility AND non-retryable status also attached the streaming-body refusal note whenever `retryable_status and request.extensions.get(STREAMING_BODY_MARKER)` — telling users their stream couldn't replay when the actual reason for not retrying was method exclusion (e.g. POST not in retry_methods). The same misrouting existed on the NetworkError/TimeoutError arm. Drop the misleading add_note() inside the early-out branches in both AsyncRetry and Retry. The note still fires at the existing dedicated streaming-refusal site (the `# ---- retryable failure path` block) where streaming is the actual blocker. Also update existing tests that exercised POST (method-ineligible) to use PUT (method-eligible) so streaming IS the actual reason for refusal, and flip the two non-idempotent sync tests to assert the note is NOT attached. Closes audit Nit finding (retry.py:111 + :242/:249) from planning/audit/2026-06-07-deep-audit.md. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 777d927 commit 049bfa9

3 files changed

Lines changed: 93 additions & 21 deletions

File tree

‎src/httpware/middleware/resilience/retry.py‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,11 @@ async def __call__(self, request: httpx2.Request, next: AsyncNext) -> httpx2.Res
111111
except StatusError as exc:
112112
retryable_status = exc.response.status_code in self.retry_status_codes
113113
if not method_eligible or not retryable_status:
114-
if retryable_status and request.extensions.get(STREAMING_BODY_MARKER):
115-
exc.add_note(_STREAMING_BODY_REFUSAL_NOTE)
116114
raise
117115
last_exc = exc
118116
last_response = exc.response
119117
except (NetworkError, TimeoutError) as exc:
120118
if not method_eligible:
121-
if request.extensions.get(STREAMING_BODY_MARKER):
122-
exc.add_note(_STREAMING_BODY_REFUSAL_NOTE)
123119
raise
124120
last_exc = exc
125121
last_response = None
@@ -253,15 +249,11 @@ def __call__(self, request: httpx2.Request, next: Next) -> httpx2.Response: # n
253249
except StatusError as exc:
254250
retryable_status = exc.response.status_code in self.retry_status_codes
255251
if not method_eligible or not retryable_status:
256-
if retryable_status and request.extensions.get(STREAMING_BODY_MARKER):
257-
exc.add_note(_STREAMING_BODY_REFUSAL_NOTE)
258252
raise
259253
last_exc = exc
260254
last_response = exc.response
261255
except (NetworkError, TimeoutError) as exc:
262256
if not method_eligible:
263-
if request.extensions.get(STREAMING_BODY_MARKER):
264-
exc.add_note(_STREAMING_BODY_REFUSAL_NOTE)
265257
raise
266258
last_exc = exc
267259
last_response = None

‎tests/test_retry.py‎

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,11 @@ async def streamed_files() -> typing.AsyncIterator[bytes]:
466466

467467

468468
async def test_retry_refuses_streamed_body_request() -> None:
469-
"""AsyncRetry must not replay a request with a streaming body — re-raise with a PEP-678 note."""
469+
"""AsyncRetry must not replay a request with a streaming body — re-raise with a PEP-678 note.
470+
471+
Uses PUT (idempotent, in retry_methods) so that method eligibility is satisfied and the streaming
472+
body is the actual blocker. The note must appear only when streaming IS the reason for refusal.
473+
"""
470474
sleeper = _SleepRecorder()
471475
call_count = {"n": 0}
472476

@@ -484,7 +488,7 @@ async def streamed_body() -> typing.AsyncIterator[bytes]:
484488
)
485489

486490
with pytest.raises(ServiceUnavailableError) as info:
487-
await client.post("https://example.test/upload", content=streamed_body())
491+
await client.put("https://example.test/upload", content=streamed_body())
488492

489493
assert call_count["n"] == 1
490494
assert sleeper.calls == [] # no retry attempted
@@ -493,7 +497,11 @@ async def streamed_body() -> typing.AsyncIterator[bytes]:
493497

494498

495499
async def test_retry_refuses_streamed_body_does_not_consume_budget() -> None:
496-
"""When AsyncRetry refuses for streaming-body reasons, no budget token is withdrawn."""
500+
"""When AsyncRetry refuses for streaming-body reasons, no budget token is withdrawn.
501+
502+
Uses PUT (idempotent, in retry_methods) so the streaming body is the actual blocker,
503+
not method ineligibility.
504+
"""
497505
sleeper = _SleepRecorder()
498506
budget = RetryBudget(ttl=10.0, min_retries_per_sec=10.0, percent_can_retry=0.2)
499507

@@ -510,15 +518,19 @@ async def streamed_body() -> typing.AsyncIterator[bytes]:
510518
)
511519

512520
with pytest.raises(ServiceUnavailableError):
513-
await client.post("https://example.test/upload", content=streamed_body())
521+
await client.put("https://example.test/upload", content=streamed_body())
514522

515523
# Budget should be untouched: deposits OK (every attempt deposits), but no withdrawals.
516524
# Check via _withdrawn deque emptiness.
517525
assert len(budget._withdrawn) == 0 # noqa: SLF001 — implementation-detail access for invariant
518526

519527

520-
async def test_retry_refuses_streamed_body_network_error_non_idempotent() -> None:
521-
"""Streaming POST that hits a NetworkError gets the PEP-678 note."""
528+
async def test_retry_refuses_streamed_body_network_error_idempotent() -> None:
529+
"""Streaming PUT that hits a NetworkError gets the PEP-678 note.
530+
531+
Uses PUT (idempotent, in retry_methods) so streaming IS the actual blocker —
532+
the note should appear at the dedicated retryable-failure-path site, not the early-out.
533+
"""
522534
sleeper = _SleepRecorder()
523535

524536
def handler(request: httpx2.Request) -> httpx2.Response: # noqa: ARG001
@@ -535,7 +547,7 @@ async def streamed_body() -> typing.AsyncIterator[bytes]:
535547
)
536548

537549
with pytest.raises(NetworkError) as info:
538-
await client.post("https://example.test/upload", content=streamed_body())
550+
await client.put("https://example.test/upload", content=streamed_body())
539551

540552
assert sleeper.calls == [] # no retry attempted
541553
notes = getattr(info.value, "__notes__", [])
@@ -660,3 +672,32 @@ async def test_deposit_fires_once_per_call_not_per_attempt() -> None:
660672
assert response.status_code == HTTPStatus.OK
661673
assert handler.calls == 3 # noqa: PLR2004 — "3" is intentional literal in test (max_attempts=3)
662674
assert budget.deposit_calls == 1, f"expected 1 deposit per request, got {budget.deposit_calls}"
675+
676+
677+
async def test_method_ineligible_with_streaming_body_does_not_attach_streaming_note() -> None:
678+
"""POST with a streaming body that gets a 503 raises ServiceUnavailableError WITHOUT the streaming-note.
679+
680+
The blocker is method ineligibility (POST not in retry_methods by default), not the streaming body.
681+
The previous code wrongly attached the streaming-note here, misleadingly suggesting the stream was
682+
the blocker.
683+
"""
684+
sleeper = _SleepRecorder()
685+
handler = _ResponseSequence([HTTPStatus.SERVICE_UNAVAILABLE])
686+
687+
async def _streaming_body() -> typing.AsyncIterator[bytes]:
688+
yield b"chunk"
689+
690+
transport = httpx2.MockTransport(handler)
691+
async with AsyncClient(
692+
httpx2_client=httpx2.AsyncClient(transport=transport),
693+
middleware=[AsyncRetry(_sleep=sleeper, base_delay=0.01, max_delay=0.02)],
694+
) as client:
695+
# Use client.post() so the STREAMING_BODY_MARKER is set in request.extensions (build_request alone
696+
# does not set it — the marker is set by the _request() helper that also calls send()).
697+
with pytest.raises(ServiceUnavailableError) as exc_info:
698+
await client.post("https://example.test/x", content=_streaming_body())
699+
notes = getattr(exc_info.value, "__notes__", [])
700+
assert not any("stream that cannot replay" in n for n in notes), (
701+
f"streaming-note was incorrectly attached when method ineligibility was the blocker: {notes!r}"
702+
)
703+
assert sleeper.calls == []

‎tests/test_retry_sync.py‎

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,12 @@ def test_streaming_body_refusal_emits_log_event(caplog: pytest.LogCaptureFixture
178178
assert any("retry refused" in r.getMessage() for r in caplog.records)
179179

180180

181-
def test_streaming_body_refusal_on_non_idempotent_method() -> None:
182-
"""Streaming-body marker added to exception even when method isn't idempotent."""
181+
def test_streaming_body_refusal_on_non_idempotent_method_does_not_attach_note() -> None:
182+
"""Method-ineligible POST + NetworkError + streaming marker does NOT get the streaming note.
183+
184+
The reason for not retrying is method ineligibility, not the streaming body.
185+
The streaming note must not be attached in the early-out branch.
186+
"""
183187
sleeper = _SleepRecorder()
184188

185189
def handler(request: httpx2.Request) -> httpx2.Response: # noqa: ARG001
@@ -192,11 +196,17 @@ def handler(request: httpx2.Request) -> httpx2.Response: # noqa: ARG001
192196
with pytest.raises(NetworkError) as info:
193197
client.send(request)
194198
notes = getattr(info.value, "__notes__", [])
195-
assert any("stream that cannot replay" in note for note in notes)
199+
assert not any("stream that cannot replay" in note for note in notes), (
200+
f"streaming-note was incorrectly attached when method ineligibility was the blocker: {notes!r}"
201+
)
196202

197203

198-
def test_streaming_body_refusal_status_error_on_non_idempotent_method() -> None:
199-
"""Status-error path: non-idempotent + retryable status + streaming marker -> note added."""
204+
def test_streaming_body_refusal_status_error_on_non_idempotent_method_does_not_attach_note() -> None:
205+
"""Method-ineligible POST + retryable status + streaming marker does NOT get the streaming note.
206+
207+
The reason for not retrying is method ineligibility, not the streaming body.
208+
The streaming note must not be attached in the early-out branch.
209+
"""
200210
sleeper = _SleepRecorder()
201211
handler = _ResponseSequence([HTTPStatus.SERVICE_UNAVAILABLE])
202212
client = _client(handler, retry=Retry(_sleep=sleeper))
@@ -205,7 +215,9 @@ def test_streaming_body_refusal_status_error_on_non_idempotent_method() -> None:
205215
with pytest.raises(ServiceUnavailableError) as info:
206216
client.send(request)
207217
notes = getattr(info.value, "__notes__", [])
208-
assert any("stream that cannot replay" in note for note in notes)
218+
assert not any("stream that cannot replay" in note for note in notes), (
219+
f"streaming-note was incorrectly attached when method ineligibility was the blocker: {notes!r}"
220+
)
209221

210222

211223
def test_client_post_with_sync_generator_content_marks_extensions() -> None:
@@ -534,3 +546,30 @@ def test_retry_after_equal_to_max_delay_still_retries() -> None:
534546
client.get("https://example.test/x")
535547
assert sleeper.calls == [2.0]
536548
assert handler.calls == 2 # noqa: PLR2004 — initial attempt + 1 retry
549+
550+
551+
def test_method_ineligible_with_streaming_body_does_not_attach_streaming_note() -> None:
552+
"""POST with a streaming body that gets a 503 raises WITHOUT the streaming-note (sync).
553+
554+
The blocker is method ineligibility, not streaming. Mirror of the async test.
555+
"""
556+
sleeper = _SleepRecorder()
557+
handler = _ResponseSequence([HTTPStatus.SERVICE_UNAVAILABLE])
558+
559+
def _streaming_body() -> typing.Iterator[bytes]:
560+
yield b"chunk"
561+
562+
transport = httpx2.MockTransport(handler)
563+
with (
564+
Client(
565+
httpx2_client=httpx2.Client(transport=transport),
566+
middleware=[Retry(_sleep=sleeper, base_delay=0.01, max_delay=0.02)],
567+
) as client,
568+
pytest.raises(ServiceUnavailableError) as exc_info,
569+
):
570+
client.post("https://example.test/x", content=_streaming_body())
571+
notes = getattr(exc_info.value, "__notes__", [])
572+
assert not any("stream that cannot replay" in n for n in notes), (
573+
f"streaming-note was incorrectly attached when method ineligibility was the blocker: {notes!r}"
574+
)
575+
assert sleeper.calls == []

0 commit comments

Comments
 (0)