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

Đang mở Phù hợp với người mới
#760 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
77/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python
Lĩnh vực
cli, networking

Hướng nghiên cứu

Bắt đầu tại src/check_jsonschema/cachedownloader.py, ở _get_request, rồi theo dõi cách vòng lặp thử lại hiện có xử lý requests.RequestException. Thêm thời gian chờ có giới hạn cho kết nối và đọc vào yêu cầu, sau đó kiểm tra các bài kiểm tra về tải xuống schema từ xa và lỗi khi thử lại. Công việc hoàn tất khi một yêu cầu bị đình trệ đi đến bước xử lý thử lại và lỗi thay vì bị treo vô thời hạn.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
Python
Star
357
Fork
74
Merge trung bình
8 ngày 14 giờ
Pull request đã merge (30 ngày)
3

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của python-jsonschema/check-jsonschema

Tất cả issue của python-jsonschema/check-jsonschema

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.