Skip to content

to_geotiff(..., gpu=True) swallows all GPU writer exceptions and silently falls back to CPU #1674

@brendancol

Description

@brendancol

Describe the bug

xrspatial/geotiff/__init__.py:1161-1175:

```python
try:
write_geotiff_gpu(data, path, ..., streaming_buffer_bytes=streaming_buffer_bytes)
return
except (ImportError, Exception):
pass # fall through to CPU path
```

except (ImportError, Exception) is equivalent to except Exception (ImportError is a subclass). Any failure inside write_geotiff_gpu (a real bug in the nvCOMP path, a CRS encoding error, a RuntimeError from a CuPy mismatch) disappears silently and the data is written via the CPU pipeline.

The user explicitly asked for gpu=True. The GPU and CPU writers do not guarantee bit-identical output (different compression libraries, different tile-encoding paths), so the file contents change without notice when the GPU path errors.

Expected behavior

With gpu=True explicitly passed:

  • ImportError and the specific "GPU not available" failure path fall back to CPU with a GeoTIFFFallbackWarning.
  • Any other exception propagates.

With gpu auto-detected from _is_gpu_data(data), the same fallback applies but the warning fires every time so users learn their GPU-resident data was copied to host.

XRSPATIAL_GEOTIFF_STRICT=1 upgrades every fallback to a re-raise.

Suggested fix

Replace the broad catch with except ImportError. For RuntimeError, only fall back when the message names a GPU-availability signal ("nvCOMP", "CUDA", "no device", etc.); let any other RuntimeError propagate. Always emit GeoTIFFFallbackWarning on fallback.

Test: monkeypatch write_geotiff_gpu to raise RuntimeError("synthetic") and assert to_geotiff(..., gpu=True) propagates rather than silently writes a CPU file.

Additional context

Reported during a code review of the geotiff module. Same direction as #1662 (typed warnings + strict mode).

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggpuCuPy / CUDA GPU supportinput-validationInput validation and error messages

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions