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

Open Beginner friendly
#760 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
77/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
cli, networking

Research direction

Start in src/check_jsonschema/cachedownloader.py at _get_request and trace how its existing retry loop handles requests.RequestException. Add a bounded connect and read timeout to the request, then check the tests covering remote schema downloads and retry failures. Done means a stalled request reaches the retry and failure handling instead of hanging indefinitely.

Written by the indexing model from the issue text.

Description

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.

Dominant language
Python
Stars
357
Forks
74
Avg merge
8d 14h
Merged PRs (30d)
3

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from python-jsonschema/check-jsonschema

All issues in python-jsonschema/check-jsonschema

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.