Skip to content

fix(deck.gl-geotiff): abort a stale GeoTIFF parse when the source changes - #770

Merged
kylebarron merged 2 commits into
developmentseed:mainfrom
charlieforward9:fix/cog-layer-parse-race
Oct 22, 2026
Merged

kylebarron merged 2 commits into
developmentseed:mainfrom
charlieforward9:fix/cog-layer-parse-race

Conversation

@charlieforward9

Copy link
Copy Markdown
Contributor

Note

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

Changing geotiff on a COGLayer while the old file was still opening could leave the layer drawing the old file and call onGeoTIFFLoad with it. The old parse was never aborted, so if it finished last it overwrote the new file's state. If it finished first, isLoaded turned true early.

updateState now aborts the previous parse and gives each parse its own AbortController. MultiCOGLayer had the same race on sources and gets the same fix.

The new tests in cog-layer.test.ts and multi-cog-layer.test.ts fail without this change.

charlieforward9 and others added 2 commits October 21, 2026 22:44
COGLayer started a new parse on every `geotiff` change, but its one
AbortController lived as long as the layer and was only aborted in
finalizeState. An old parse that finished last overwrote the new
GeoTIFF's state and called onGeoTIFFLoad with the old file. One that
finished first cleared loadingMetadata while the new parse was still
running.

updateState now aborts the previous parse and gives each parse its own
controller, and only a parse that wasn't aborted clears loadingMetadata.

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

MultiCOGLayer had the same race as COGLayer. An old parse that finished
last overwrote the new sources' state and called onGeoTIFFLoad with the
old sources. One that finished first cleared loadingMetadata while the
new parse was still running. This applies the same fix.

The onError test now builds its layer with makeBareLayer, like COGLayer's
version of that test, because updateState now reads this.state.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added the fix label Oct 22, 2026

@kylebarron kylebarron left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks! I'm thinking we may rework this for 0.10, but this is good for now.

@kylebarron
kylebarron merged commit c65e951 into developmentseed:main Oct 22, 2026
3 checks passed
@charlieforward9
charlieforward9 deleted the fix/cog-layer-parse-race branch October 22, 2026 19:24
@charlieforward9

Copy link
Copy Markdown
Contributor Author

Happy to learn more and help test, any clue when this will become a new release?

@kylebarron

Copy link
Copy Markdown
Member

I published 0.9.0-beta.3, and I'd like to get 0.9 out tomorrow or Monday

kylebarron added a commit that referenced this pull request Oct 26, 2026
> [!NOTE]
> This PR was written by Claude (Claude Code), not by @kylebarron.

Stage 1 of the Sources design, which this PR adds as
`dev-docs/specs/2026-10-22-sources-design.md`.

COGLayer, MultiCOGLayer and ZarrLayer each load their metadata from
`updateState` and store the result in `state`. deck.gl hands that state
object to every newer instance of the layer, so a load started by an
earlier render can land after a newer one. #770 fixed this for the two
COG layers by hand; ZarrLayer still had it.

Now `RasterTileLayer` runs these loads, mirroring deck.gl's own
`shouldUpdateState`, `updateState` and `_update()`:

- **`shouldLoadMetadata(params)`:** subclasses override it to return
`true` when a prop their metadata depends on has changed.
- **`loadMetadata(signal)`:** subclasses override it to load the
metadata and store it in state. After its last `await`, it calls
`signal.throwIfAborted()` before `setState` or callbacks.
- **`_loadMetadata()`:** `RasterTileLayer.updateState` calls this
protected method when `shouldLoadMetadata` returns `true`. It:
  - aborts the previous load;
  - sets and clears `loadingMetadata`;
- raises only the latest load's errors through `onError`, prefixed with
the layer's name, as in "Metadata loading failed in COGLayer: …";
  - aborts the load in flight when the layer is removed.

Because every instance shares one `state` object, `setState` from the
instance that started a load lands where the newest instance reads. The
three layers drop their own `updateState`, and type their state as
`RasterTileLayer<…>["state"] & {…}`, as deck.gl's MVTLayer does with
TileLayer's.

Fixes:

- **ZarrLayer:** changing `node` or `variable` while the previous store
is still loading no longer lets the old load overwrite the new one or
mark the layer loaded early. ZarrLayer also passes the signal to
zarrita's `open`, so a replaced load stops reading.
- **MultiCOGLayer:**
- It now runs `super.updateState`, which its own `updateState` skipped.
- A replaced load's errors no longer reach `onError`, even when they're
thrown while building its tile grid.

Metadata errors now name the layer: "loading GeoTIFF: …", "loading COG
sources: …" and "loading Zarr metadata: …" become "Metadata loading
failed in COGLayer: …", "… in MultiCOGLayer: …" and "… in ZarrLayer: …".

`shouldLoadMetadata` and `loadMetadata` are new public methods on
`RasterTileLayer` and its subclasses; nothing new is exported. Notes for
authors of `RasterTileLayer` subclasses:

- An override of `updateState` must call `super.updateState`, which
starts the loads.
- An override of `finalizeState` must call `super.finalizeState`, which
aborts the load in flight.
- `state.loadingMetadata` and `state.metadataAbortController` belong to
the base layer, and the default `_isLoadingMetadata()` reads
`state.loadingMetadata`.

Tests:

- The race tests use real fixtures: COG files for the two COG layers,
and a GeoZarr store for ZarrLayer.
- They fail if the base doesn't abort the previous load, or if any
layer's `loadMetadata` skips `throwIfAborted()`.
- The Zarr tests also fail if either `open` call drops the signal.
- The base tests also check that a re-render whose `shouldLoadMetadata`
returns `false` starts no load.

🤖 Written by [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

2 participants