From 047d2ee1517f52a390c1feb7903e4eed15821596 Mon Sep 17 00:00:00 2001 From: Ofek Lev Date: Sat, 25 May 2024 14:21:26 -0400 Subject: [PATCH] Add default timeout for network requests --- docs/history/hatch.md | 1 + src/hatch/utils/network.py | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/docs/history/hatch.md b/docs/history/hatch.md index 7e0c1d208..1b675ab8a 100644 --- a/docs/history/hatch.md +++ b/docs/history/hatch.md @@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ***Fixed:*** - The `fmt` command no longer hides the commands that are being executed +- Add default timeout for network requests, useful when installing Python distributions ## [1.11.1](https://github.com/pypa/hatch/releases/tag/hatch-v1.11.1) - 2024-05-23 ## {: #hatch-v1.11.1 } diff --git a/src/hatch/utils/network.py b/src/hatch/utils/network.py index 70b9b901b..1bbb52ff4 100644 --- a/src/hatch/utils/network.py +++ b/src/hatch/utils/network.py @@ -12,6 +12,10 @@ MINIMUM_SLEEP = 2 MAXIMUM_SLEEP = 20 +# The timeout should be slightly larger than a multiple of 3, +# which is the default TCP packet retransmission window. See: +# https://tools.ietf.org/html/rfc2988 +DEFAULT_TIMEOUT = 10 @contextmanager @@ -34,6 +38,8 @@ def streaming_response(*args: Any, **kwargs: Any) -> Generator[httpx.Response, N def download_file(path: Path, *args: Any, **kwargs: Any) -> None: + kwargs.setdefault('timeout', DEFAULT_TIMEOUT) + with path.open(mode='wb', buffering=0) as f, streaming_response('GET', *args, **kwargs) as response: for chunk in response.iter_bytes(16384): f.write(chunk)