Skip to content

fix(calibration): enforce remote image total_timeout across headers and redirects - #3848

Merged
Qubitium merged 2 commits into
mainfrom
fix/image-total-timeout
Nov 13, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/image-total-timeout

Conversation

@ZX-ModelCloud

@ZX-ModelCloud ZX-ModelCloud commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

TL;DR

load_image_from_url's total_timeout is now an absolute deadline that covers the request/redirect/header phase, body streaming, and decoding. Previously the watchdog started only after the final response headers arrived, so delayed headers, header drip, and chains of individually fast redirects could exceed the budget and still return success. Net: +283/-4 across 4 files.

Fixes #3832.

1. The problem

The deadline started inside the body loop, after requests.get(...) had already returned the final headers:

flowchart TD
    A[load_image_from_url] --> B[deadline = monotonic + total_timeout]
    B --> C[requests.get stream=True timeout=per-read]
    C --> D[redirect chain resolved inside requests]
    D --> E[final headers arrive]
    E --> F[fresh body deadline = monotonic + remaining]
    F --> G[body watchdog]
    G --> H[Image.open + image.load]
    H --> I[return image]

    classDef fail fill:#f8d7da,stroke:#b02a37,color:#000;
    class C,D,E fail;
Loading
Mechanism Where it was
Blocking request/redirect/header phase before any watchdog gptqmodel/utils/image.py:157 (pre-fix main)
Fresh body deadline created after headers gptqmodel/utils/image.py:189-193
Per-read timeout passed through unclamped gptqmodel/utils/image.py:157
Decode returned without rechecking the budget gptqmodel/utils/image.py:326

2. The new flow

One absolute deadline is computed per load_image_from_url call and shared by every attempt, the request phase, the body stream, and decoding:

flowchart TD
    A[load_image_from_url] --> B[deadline = monotonic + total_timeout]
    B --> C{remaining > 0}
    C -- no --> Z[ImageFetchError: total time limit]
    C -- yes --> D[_get_response: daemon thread + deadline wait]
    D -- deadline --> Z
    D -- response --> E[deadline check after headers]
    E -- expired --> Z
    E --> F[body read with watchdog scheduled from deadline]
    F -- expired --> Z
    F --> G[deadline check after body]
    G -- expired --> Z
    G --> H[Image.open + pixel limit]
    H --> I[image.load]
    I --> J{deadline expired}
    J -- yes --> K[image.close + ImageFetchError]
    J -- no --> L[return image]

    classDef fail fill:#f8d7da,stroke:#b02a37,color:#000;
    class Z,K fail;
Loading

_clamp_timeout caps the connect/read values at the remaining budget, so requests cannot wait past the deadline even when the caller supplied a larger timeout. When the caller gives up, the helper thread is cancelled: an already-arrived response is closed immediately, and a request still blocked inside requests is released as soon as the server completes or closes the connection.

3. Failure and ambiguity contract

Situation Before After
Budget expires during connect, headers, or redirects Success if the final response eventually arrived ImageFetchError with total time limit; not retried
Budget expires during body streaming Aborted by the watchdog Unchanged; watchdog now uses the shared deadline
Budget expires after the body or during decoding Returned successfully ImageFetchError; image closed
Fast response inside the budget Returned Returned
Retryable HTTP/transport failure before the deadline Retried with backoff Unchanged; backoff still bounded by the same deadline

4. Change inventory

Area File Purpose
Fetch helper gptqmodel/utils/image.py absolute deadline, deadline-bounded request phase, timeout clamping, post-body and post-decode checks
Tests tests/test_image_fetch.py slow headers, redirect chain, header drip, slow decoding
Docs docs/remote-image-fetch-bounds.md flow, before/after, failure contract, test map
README README.md link to the reference page

5. Tests

tests/test_image_fetch.py (26 passed):

Test Invariant
test_slow_headers_are_bounded_by_total_timeout delayed headers cannot exceed the budget
test_redirect_chain_is_bounded_by_total_timeout individually fast redirects share one deadline
test_header_drip_is_bounded_by_total_timeout trickled headers cannot reset the budget
test_decode_time_is_bounded_by_total_timeout decoding is inside the budget
test_slow_drip_body_is_bounded_by_total_timeout body streaming is inside the budget
existing fetch/retry/limit tests behavior unchanged

6. Validation

python -m pytest tests/test_image_fetch.py -q
# 26 passed

python -m ruff check gptqmodel/utils/image.py tests/test_image_fetch.py
# All checks passed!

The scaled budgets used by the tests exercise the same deadline path as the default 120-second configuration; no model, kernel, or checkpoint code is touched.

7. Reviewer notes

  • The logic-flow reference page is docs/remote-image-fetch-bounds.md, linked from README.md.
  • The helper thread exists because requests applies timeout per socket read; a trickling header stream can reset that timer indefinitely. The thread is a daemon and is cancelled when the caller's deadline expires.
  • total_timeout remains a caller-visible argument; no environment-variable switch is introduced.

total_timeout only started once the final response headers had arrived:
requests.get(stream=True, timeout=...) was called before the watchdog, so
delayed headers, header drip, and chains of individually fast redirects
could exceed the budget and still return success. The deadline is now
absolute and covers the request/redirect/header phase, the body read, and
decode; per-attempt timeouts are clamped to the remaining budget.

Adds regression tests for slow headers, a redirect chain, header drip, and
slow decoding.
@ZX-ModelCloud
ZX-ModelCloud force-pushed the fix/image-total-timeout branch from e6eccd1 to 7c08f9d Compare November 13, 2026 09:26
Comment thread gptqmodel/utils/image.py Dismissed
@Qubitium
Qubitium merged commit e310049 into main Nov 13, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/image-total-timeout branch November 13, 2026 09:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Remote-image total_timeout excludes response headers and automatic redirects

2 participants