Commit c702e56
authored
Do not retry requests with a consumed streaming body (#872)
## Summary
Requests with a streaming body (e.g. `files().upload()`) are backed by a
single-use
`InputStream`. When such a request received a retriable HTTP response
(429/501/503), the
retry loop in `ApiClient.executeInner` re-sent the *same* `InputStream`.
Because the first
attempt had already consumed the stream, the retry transmitted an
**empty body**, which
manifested as either:
- a **0-byte file** written to the target despite an HTTP 204, or
- a confusing downstream error (e.g. `InternalError`) surfaced from
`FilesImpl.upload`.
This only occurs when a retriable status interrupts a streaming upload,
so it is rare and
intermittent, but when it happens the upload silently corrupts data.
## Root cause
`Request` holds the streaming body as a single `InputStream` reference.
On retry,
`executeInner` reuses the same `Request`, and `CommonsHttpClient` wraps
that already-consumed
stream in a fresh `InputStreamEntity` — sending 0 bytes. The upload
`PUT` is non-idempotent,
and `NonIdempotentRequestRetryStrategy` treats 429/501/503 as retriable,
so a transient 503
triggers the empty-body retry.
An `InputStream`-backed body cannot be rewound, so retrying is
inherently unsafe once the body
has been sent.
## Fix
In `ApiClient.executeInner`, skip the retry when the request has a
streaming body **and** an
HTTP response was received (which proves the body was already
transmitted), and surface the
original error to the caller so it can retry with a fresh stream.
The guard is deliberately narrow:
- **String / no-body requests** (the vast majority of API calls) are
repeatable — a new entity
is built per attempt — so their retry behavior is unchanged.
- **Transport-level `IOError`s** (no response received, e.g. a pre-send
`ConnectException`)
still retry as before, since in that case the stream may not have been
read yet.
The only behavior removed is the retry of an already-consumed stream,
which never worked. This
lives in the hand-written core (`ApiClient`), so it applies to any
streaming upload rather than
being specific to the Files API.
## Tests
- `doesNotRetryStreamingBodyAfterResponse` — a streaming `PUT` that gets
a 503 now throws the
typed `TemporarilyUnavailable` (503) instead of silently retrying with
an empty body.
- `retriesNonStreamingBodyOn503` — a string-bodied request with the same
503 still retries and
succeeds, confirming the guard does not regress ordinary requests.
Full module test suite passes (1404 tests, 0 failures).1 parent 23b0be2 commit c702e56
3 files changed
Lines changed: 122 additions & 0 deletions
File tree
- databricks-sdk-java/src
- main/java/com/databricks/sdk/core
- test/java/com/databricks/sdk/core
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
| 12 | + | |
11 | 13 | | |
12 | 14 | | |
13 | 15 | | |
| |||
Lines changed: 14 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
275 | 275 | | |
276 | 276 | | |
277 | 277 | | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
278 | 292 | | |
279 | 293 | | |
280 | 294 | | |
| |||
Lines changed: 106 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| 9 | + | |
9 | 10 | | |
| 11 | + | |
10 | 12 | | |
11 | 13 | | |
12 | 14 | | |
| |||
15 | 17 | | |
16 | 18 | | |
17 | 19 | | |
| 20 | + | |
18 | 21 | | |
| 22 | + | |
19 | 23 | | |
20 | 24 | | |
21 | 25 | | |
| 26 | + | |
22 | 27 | | |
23 | 28 | | |
24 | 29 | | |
| |||
484 | 489 | | |
485 | 490 | | |
486 | 491 | | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
487 | 593 | | |
488 | 594 | | |
489 | 595 | | |
| |||
0 commit comments