Skip to content

fix(calibration): bound and diagnose remote image fetches - #3483

Merged
Qubitium merged 2 commits into
mainfrom
fix/image-fetch-timeouts
Nov 2, 2026
Merged

Qubitium merged 2 commits into
mainfrom
fix/image-fetch-timeouts

Conversation

@ZX-ModelCloud

Copy link
Copy Markdown
Collaborator

Problem

Multimodal calibration data can reference images by URL. Three call sites fetched them with requests.get(url, stream=True) and no timeout, no size cap, no retry:

  • gptqmodel/utils/image.py:46 (fetch_image, shared by ~15 VL model definitions)
  • gptqmodel/models/definitions/interns1.py:125
  • gptqmodel/models/definitions/ovis2_5.py:99

requests defaults to timeout=None, so a server that accepts the connection and never responds hangs the whole quantization run forever.

Confirmed, not theoretical

Against main (f41b953), with a local server that accepts the connection and sends nothing:

old fetch_image  -> killed by external `timeout 15` (exit 124), never returned
new fetch_image  -> ImageFetchError after 30.03s (read timeout)

Reachability: BaseQModel.prepare_dataset → loop_processor.py:287 calibration path; fetch_image is the choke point for qwen2_vl/qwen3_vl/ovis2/minicpmv/internvl_chat/…, and interns1/ovis2_5 had their own duplicate copies.

Fix

New load_image_from_url() in gptqmodel/utils/image.py; all three sites go through it:

  • connect/read timeout defaults (5s, 30s), plus a 120 s wall-clock budget enforced by a watchdog that shuts the socket down, so slow-drip responses cannot stretch the download indefinitely;
  • 32 MiB download cap checked from Content-Length and while streaming;
  • 64 Mpx decode cap checked before Image.load() allocates, so decompression bombs fail instead of spiking RSS;
  • retries with exponential backoff for transient faults (timeouts, 408/425/429/5xx, truncated bodies); no retries for connection/URL/client errors;
  • ImageFetchError(RuntimeError) carrying URL + reason, so calibration fails diagnosably instead of hanging or raising opaque PIL errors;
  • limits overridable via GPTQMODEL_IMAGE_FETCH_MAX_BYTES / GPTQMODEL_IMAGE_FETCH_MAX_PIXELS.

interns1/ovis2_5 no longer duplicate the download logic or keep an unclosed streaming response alive via Image.open(response.raw).

Tests

tests/test_image_fetch.py — 21 tests against a local HTTP server: hanging server, slow drip, declared/streamed oversize, pixel bomb, retry classification, env overrides, and both model definitions.

21 passed, 1 warning in 12.66s

Related suite: tests/test_image_fetch.py tests/test_prepare_dataset.py tests/test_calibration_data_device.py tests/test_silent_exception_handling.py → 89 passed.

Out of scope (noted, not changed)

gptqmodel/utils/machete.py:217 — urllib.request.urlopen(url) has no timeout while downloading the pinned CUTLASS archive during JIT kernel build. Same bug class, but on the kernel-build path rather than calibration; left for a separate change.

Multimodal calibration data can reference images by URL. Three call sites
issued `requests.get(url, stream=True)` with no timeout and no size limit:

  - gptqmodel/utils/image.py (shared by ~15 VL model definitions)
  - gptqmodel/models/definitions/interns1.py
  - gptqmodel/models/definitions/ovis2_5.py

`requests` defaults to no timeout, so a server that accepts the connection and
never answers (or trickles bytes) hangs the whole quantization run forever; a
huge or malicious payload could also exhaust memory.

Add `load_image_from_url()` in gptqmodel/utils/image.py and route all three
sites through it:

  - connect/read timeout defaults (5s, 30s) plus a 120s wall-clock budget that
    survives slow-drip responses (watchdog shuts the socket down);
  - 32 MiB download cap enforced from Content-Length and while streaming;
  - 64 Mpx decode cap checked before `Image.load()` allocates, so decompression
    bombs fail instead of spiking RSS;
  - retries with exponential backoff for transient faults (timeouts, 408/425/
    429/5xx, truncated bodies), no retries for URL/HTTP client errors;
  - `ImageFetchError(RuntimeError)` carrying the URL and reason so calibration
    failures are diagnosable instead of hanging or raising opaque PIL errors;
  - byte/pixel limits overridable via GPTQMODEL_IMAGE_FETCH_MAX_BYTES and
    GPTQMODEL_IMAGE_FETCH_MAX_PIXELS.

interns1/ovis2_5 also stop duplicating the download logic and no longer keep
an unclosed streaming response alive via `Image.open(response.raw)`.

Tests: tests/test_image_fetch.py (21 tests) with a local HTTP server covering
hangs, slow drip, oversize declared/streamed bodies, pixel bombs, retry
classification and both model definitions.
Comment thread gptqmodel/utils/image.py Fixed
Comment thread gptqmodel/utils/image.py Fixed
Comment thread tests/test_image_fetch.py Fixed
Comment thread tests/test_image_fetch.py Fixed
github-code-quality flagged four `except ...: pass` blocks added by the
previous commit (CodeQL `py/empty-except`): two socket-probe/teardown paths in
`_abort_response` and the two expected client-disconnect handlers of the test
HTTP server. Behaviour is unchanged; each silent handler now explains why the
failure is intentionally ignored.

Also add `test_best_effort_except_blocks_are_documented`, which re-runs the
CodeQL check (AST, comment window between the `except` line and the last
statement of the body) over the files this PR touches, so the pattern cannot
regress silently. Verified the helper reports a bare handler and accepts a
commented one.
@Qubitium
Qubitium merged commit e4778c5 into main Nov 2, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/image-fetch-timeouts branch November 2, 2026 08:18
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.

2 participants