Guard diffuse() against unbounded allocations and steps loop (#1267)#1268
Merged
brendancol merged 1 commit intomainfrom Apr 26, 2026
Merged
Guard diffuse() against unbounded allocations and steps loop (#1267)#1268brendancol merged 1 commit intomainfrom
brendancol merged 1 commit intomainfrom
Conversation
- Add _check_memory / _check_gpu_memory helpers (cost_distance pattern, ~32 B/pixel budget) and call them from _diffuse_numpy / _diffuse_cupy before per-step buffers are allocated. - Defer the np.full alpha allocation: dask + scalar diffusivity now passes the scalar straight through, and the eager paths only build the full raster after the memory guard runs. - Teach _diffuse_dask_cupy to handle scalar alpha lazily via cp.full per chunk, mirroring _diffuse_dask_numpy. - Cap steps at _MAX_STEPS (100,000) so a single call cannot pin a CPU. - Add tests covering oversize raster -> MemoryError, scalar diffusivity on dask -> no full-raster np.full, dask scalar matches dask array, and steps above the cap -> ValueError.
This was referenced Apr 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1267.
Summary
_check_memory/_check_gpu_memory, cost_distance pattern, ~32 B/pixel) that fire before the eager numpy / cupy paths allocate per-step buffers.np.full(agg.shape, alpha)up front for scalar diffusivity. Dask paths now keep the scalar through to the per-chunk worker; eager paths only build the full alpha raster after the memory guard runs._diffuse_dask_cupyto handle scalar alpha lazily viacp.fullper chunk, matching_diffuse_dask_numpy.stepsat_MAX_STEPS = 100_000via_validate_scalar(max_val=...)so a single call can't pin a CPU forever.Test plan
MemoryError(with_available_memory_bytesmonkeypatched)np.full(agg.shape)steps=10**12raisesValueErrornp.fullruns on the oversize input