Remote schema download has no timeout, so a stalled connection hangs the run indefinitely

Aperta Adatta ai principianti
#760 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
77/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
cli, networking

Direzione di ricerca

Inizia in src/check_jsonschema/cachedownloader.py, in _get_request, e segui il modo in cui il ciclo di tentativi esistente gestisce requests.RequestException. Aggiungi alla richiesta un timeout limitato per la connessione e la lettura, poi controlla i test che coprono i download di schemi remoti e gli errori dei tentativi. Il lavoro è completato quando una richiesta bloccata raggiunge la gestione dei tentativi e degli errori invece di restare in attesa all’infinito.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Summary

_get_request in src/check_jsonschema/cachedownloader.py calls requests.get(file_url, stream=True) without a timeout. When the server accepts the TCP/TLS connection but then stops sending, the read blocks forever: no exception is raised, so the existing retry loop (num_retries = 2) never gets a chance to run, and the process (and any pre-commit run that includes the hook) hangs until it is killed.

Because the download runs on every invocation with a remote --schemafile (the cache is validated against the response's Last-Modified), a single stalled connection to the schema host is enough to hang a pre-commit run, even when a cached copy of the schema exists.

What I saw

check-jsonschema 0.38.0, run as a pre-commit hook:

- id: check-jsonschema
  name: check taskfiles
  args: [--schemafile, 'https://taskfile.dev/schema.json']
  files: ^(Taskfile\.yaml|\.config/taskfiles/[^/]+\.yaml)$

The hook sat for over 10 minutes at 0% CPU, blocked in a socket read (/proc/<pid>/wchan was wait_woken), with one established connection to one of the host's two addresses. The same URL fetched with curl answered in about 0.25 s at that moment, so the stall was transient on one server connection, not an outage. Killing the process was the only way out.

The code on main (0.40.0) is unchanged here:

for _attempt in range(num_retries + 1):
    try:
        r = requests.get(file_url, stream=True)
    except requests.RequestException as e:
        ...

Suggested fix

Pass a timeout to requests.get, for example timeout=(connect, read) such as (10, 30). A stalled read then raises requests.exceptions.ReadTimeout (a RequestException), which the existing loop already retries and, once retries are exhausted, turns into FailedDownloadError. The requests docs recommend setting a timeout on nearly all production requests for this reason: https://requests.readthedocs.io/en/latest/user/advanced/#timeouts

Optionally, the value could be configurable (a CLI flag or an environment variable) for slow networks, and on a download failure with an existing cache file the cached schema could be used with a warning, but the timeout alone would turn an indefinite hang into a bounded failure.

Happy to open a PR if that helps.

Lingua principale
Python
Stelle
357
Fork
74
Merge medio
8g 14h
PR unite (30g)
3

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di python-jsonschema/check-jsonschema

Tutte le issue di python-jsonschema/check-jsonschema

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.