Skip to content

[Bug]: OGGM can't detect if cached downloads are truncated #1947

Open

Description

@gampnico

Overview

One interesting thing I've run into when working on #1900 is that OGGM doesn't seem to check if a cached file was truncated by an interrupted download. This leads to non-obvious errors because re-running a test or using reset=True doesn't refresh the download cache.

Expected Behaviour

A cache hit should only return for a file that is verified as complete. If the cached file is incomplete, it should re-download this.

Actual Behaviour

_cached_download_helper returns the cached path as soon as os.path.isfile(cache_path) is true, with no checks. Downloads are written directly to cache_path, and cleanup only removes a partial file if the download function itself raises an exception. Rerunning a command does nothing, so the file sticks around until the user clears the cache manually (so never). This affects any download (prepro bundles, DEMs, climate data, RGI files), and is more likely when multiprocessing is enabled and a worker is torn down mid-write.

Possible Solutions

  1. Atomic writes: download to cache_path + '.tmp' then os.replace() into place once a transfer completes.
  2. Size validation on cache hit: check the expected size against Content-Length, which is read in _requests_urlretrieve. I'm still mulling over how this will work with streaming.
  3. Optional integrity check: an opt-in checksum or HEAD size check for stricter verification. Maybe this is fine for smaller files, but checksums for each gdir would just be a waste of compute time.

Maybe a mix of 1 and 2? I'll probably get back to this once I start work on streaming for #1896.

Activity

  1. fmaussion commented on Jul 6, 2026

    @fmaussion
    Member

    good one. In the early days of OGGM we mainained a hash database of all teh files OGGM would serve. Users would first download the hash list and teh file_downloader would check hashes. It was a maintainance hell, esp. when the number of files we are servring exploded. this bit has been removed recently: #1844

    If size-validation is a possibility that might be good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions