Skip to content

fix(geotiff): stop one aborted or failed header read from failing other tiles - #709

Merged
kylebarron merged 4 commits into
mainfrom
kyle/header-cache-abort
Oct 14, 2026
Merged

kylebarron merged 4 commits into
mainfrom
kyle/header-cache-abort

Conversation

@kylebarron

@kylebarron kylebarron commented Oct 14, 2026 •

Copy link
Copy Markdown
Member

Note

This PR was written by Claude (Claude Code), not by @kylebarron.

On a COG whose TileOffsets/TileByteCounts tables extend past the first 64 KiB header chunk, each tile fetch reads its two table entries lazily through the cached header source, so every tile with entries in one chunk shares one request. Two problems let one tile break that shared request for every other tile, for the life of the GeoTIFF:

  1. Failed reads stayed cached. chunkd's SourceCache keeps a read's promise even when it rejects (SourceCache keeps a rejected promise, so one aborted or failed read is replayed to every later reader blacha/chunkd#1733), so one aborted or failed request was replayed to every later tile in that chunk, with no new request.
  2. The tile's signal was forwarded into the shared read. Aborting one tile, e.g. by panning it out of view, rejected the request for every other tile already waiting on it. Evicting failed reads doesn't fix this case on its own.

deck.gl marks a tile whose read fails, without the tile itself being cancelled, as loaded with no content and never retries it, so these tiles stayed blank.

Changes:

  • GeoTIFF.fromUrl caches header reads with a new LRUCacheMiddleware in place of SourceCache. It uses the same approximate LRU scheme as SourceCache, but drops a read that fails so the next reader retries it.
  • fetchTile, fetchTiles and the band-separate path look up byte ranges with a new getTileByteRange. The shared read runs without the caller's signal; the signal only ends that caller's wait, rejecting with signal.reason at once (previously a SourceError wrapping it).
  • Reads during open keep their signal. Their cache belongs to that one open, so nothing else shares its reads, and an aborted open (e.g. a mosaic panning past many COGs) still drops its queued reads from the limiter.

Trade-off: a header read whose tiles all aborted now still completes and holds its limiter slot until it does. That happens at most once per chunk per file, and neighboring tiles usually need those bytes. Refcounted cancellation would avoid it, but would mean replacing chunkd's chunking and caching to save a handful of small reads.

Tests: shared-header-reads.test.ts covers each failure mode from the issue: an abort mid-request, a tile joining an in-flight read (through each of the three call sites), an abort while queued in the limiter, a transient 503, and the abort reason. It builds the GeoTIFF in memory, since no existing fixture has tile tables past the first chunk. cache.test.ts covers the cache's eviction.

Closes #690


Written by Claude on behalf of @kylebarron.

🤖 Written by Claude Code

kylebarron and others added 2 commits October 14, 2026 13:30
GeoTIFF.fromUrl cached header reads with chunkd's SourceCache, which
keeps a read's promise even when it rejects and returns that rejection
to every later read of the same range (blacha/chunkd#1733). So one
aborted or failed request for a chunk of the TileOffsets/TileByteCounts
tables failed every later tile whose entries sit in that chunk, for the
life of the GeoTIFF.

Replace it with CacheMiddleware, adapted from SourceCache, which evicts
a read that fails so the next reader retries it. Eviction checks the
entry's identity, so a stale failure never removes a newer read of the
same range.

Part of #690.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…r read

Each tile fetch looks up its byte range with image.getTileSize, which on
a large COG reads its TileOffsets/TileByteCounts entries lazily through
the cached header source, so all tiles with entries in one chunk share
one request. The tile's signal was forwarded into that shared request,
so aborting one tile (e.g. panning it out of view) rejected the request
for every other tile waiting on it, even with failed reads evicted.

Look up byte ranges through getTileByteRange instead, in fetchTile,
fetchTiles, and the band-separate path. The shared read runs without the
caller's signal; the signal only ends that caller's wait, rejecting with
signal.reason at once. Reads during open keep their signal: that cache
belongs to one open, so nothing else shares its reads.

The trade-off: a header read whose tiles all aborted still completes and
holds its limiter slot until it does. That happens at most once per
chunk, and neighboring tiles usually need those bytes.

The regression tests build a GeoTIFF in memory whose tile tables run
past the first 64 KiB chunk, since no fixture has a header that large.

Part of #690.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added the fix label Oct 14, 2026
Name the cache by its eviction strategy: an approximate LRU bounded by
bytes, using the two-generation scheme of hashlru and quick-lru (the
same one chunkd's SourceCache uses). The docstring now leads with that
strategy and what the approximation means in practice.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Comment on lines +16 to +18
* chunkd middleware that caches each read by its byte range in an
* approximate LRU cache bounded by bytes, so concurrent and later reads of a
* range share one request.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* chunkd middleware that caches each read by its byte range in an
* approximate LRU cache bounded by bytes, so concurrent and later reads of a
* range share one request.
* Middleware that caches each read by its byte range in an
* approximate LRU cache. Concurrent and later reads of a
* range share one request.

@kylebarron kylebarron left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm overall

options?: { signal?: AbortSignal },
): Promise<ByteRange> {
const signal = options?.signal;
if (signal?.aborted) {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should have a comment here saying that this is the main change from upstream, IIUC. If the signal was aborted, we reject the promise, right?

Comment thread packages/geotiff/src/fetch.ts Outdated
* Look up a tile's byte range: its `TileOffsets` and `TileByteCounts`
* entries.
*
* When those tables don't fit in the bytes read at open, cogeotiff reads each

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* When those tables don't fit in the bytes read at open, cogeotiff reads each
* When those tables don't fit in the bytes read at open, TiffImage reads each

Mark where the lookup departs from upstream cogeotiff, whose getTile
forwards the caller's signal into the shared read, and comment the
already-aborted check and the race. Link TiffImage in the docstring.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@kylebarron
kylebarron enabled auto-merge (squash) October 14, 2026 18:25
@kylebarron
kylebarron merged commit effe142 into main Oct 14, 2026
3 checks passed
@kylebarron
kylebarron deleted the kyle/header-cache-abort branch October 14, 2026 18:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

geotiff: fromUrl passes a per-read signal into the shared header SourceCache, so one aborted tile read poisons later reads

1 participant