torchgeo icon indicating copy to clipboard operation
torchgeo copied to clipboard

download_url hangs forever when a download stalls

Open calebrob6 opened this issue 3 months ago • 3 comments

download_url calls urlopen without a timeout, so a connection that stops sending data blocks forever instead of raising.

https://github.com/torchgeo/torchgeo/blob/main/torchgeo/datasets/utils.py#L434

Python's default socket timeout is None, so nothing interrupts the read.

I hit this downloading WorldStrat from Zenodo. The transfer stopped at 285 MB of 38 GB and stayed there. The progress bar kept updating the elapsed time while the byte count never moved:

hr_dataset.zip:  1%|  | 285M/38.0G [03:23<9:09:46, 1.23MB/s]
hr_dataset.zip:  1%|  | 285M/38.0G [7:24:01<9:09:46, 1.23MB/s]

After seven hours there was still no error and no progress. A user running this in a script would wait indefinitely with no indication that anything is wrong.

Passing a timeout to urlopen would turn this into a normal error. The timeout applies to individual socket reads rather than the whole transfer, so a slow download that is still making progress is unaffected, and only a stalled one is interrupted.

This is related to the Content-Length check added in #4053. That catches a download that ends early, but a download that never ends at all still hangs.

calebrob6 avatar Aug 28 '26 17:08 calebrob6

At least with Spack (which also does a shit ton of downloading), it seems to be possible to restore a dropped internet connection. Do you have any idea what caused the download failure in this case? As long as we set a reasonable timeout, I'm fine with this.

adamjstewart avatar Aug 28 '26 17:08 adamjstewart

Thought I would take a look at this:

I can't identify the original cause from the report alone because there aren't logs that distinguish a stalled server, proxy, or half-open connection. I verified that the timeout passed to urlopen also applies while reading the response body, so a stalled socket read eventually raises a timeout. It is not an overall download deadline and won't interrupt a slow connection that continues delivering data. A conservative inactivity timeout such as 60 seconds therefore seems like a minimal safeguard.

I also checked the Spack comparison. Its urllib path uses a socket timeout plus retries and backoff, but removes the partial file after a failed attempt. Its curl path uses resume support, but also removes the partial file when the invocation fails. Thus, timeout/retry is the directly transferable behavior; robust cross-attempt resume is a separate feature.

The current Zenodo object supports byte ranges, but safe resume would require preserving the partial file and validating 206, Content-Range, and object identity, including handling servers that ignore range requests. I would keep that outside this minimal timeout change.

quinnarnold avatar Aug 30 '26 14:08 quinnarnold

I'd like to work on this if nobody is already working on it.

SreyasM24 avatar Sep 01 '26 03:09 SreyasM24