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

未关闭 适合新手
#760 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
77/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
python
领域
cli, networking

调研方向

从 src/check_jsonschema/cachedownloader.py 中的 _get_request 开始,追踪现有重试循环如何处理 requests.RequestException。为请求添加有界的连接和读取超时,然后检查涵盖远程 schema 下载和重试失败的测试。完成标准是:停滞的请求会进入重试和失败处理,而不是无限期挂起。

由索引模型根据 Issue 内容生成。

描述

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.

主要语言
Python
星标
357
派生
74
平均合并
8 天 14 小时
30 天内合并 PR
3

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

python-jsonschema/check-jsonschema 的其他 Issue

查看 python-jsonschema/check-jsonschema 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。