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

Offen Anfängerfreundlich
#760 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Anfängerfreundlichkeit
77/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
python
Bereich
cli, networking

Rechercherichtung

Beginne in src/check_jsonschema/cachedownloader.py bei _get_request und verfolge, wie die vorhandene Wiederholungsschleife requests.RequestException behandelt. Füge für die Anfrage eine begrenzte Verbindungs- und Lesezeitüberschreitung hinzu und prüfe anschließend die Tests für Remote-Schema-Downloads und Wiederholungsfehler. Fertig ist es, wenn eine stockende Anfrage die Wiederholungs- und Fehlerbehandlung erreicht, statt unbegrenzt zu hängen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

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.

Vorherrschende Sprache
Python
Sterne
357
Forks
74
Ø Merge
8 T. 14 Std.
Gemergte PRs (30 T.)
3

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus python-jsonschema/check-jsonschema

Alle Issues in python-jsonschema/check-jsonschema

Ähnliche Issues

Weitere Issues zu Python

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.