Skip to content

grass.pygrass: Close RasterRow via context manager in region tests - #7902

Merged
echoix merged 1 commit into
OSGeo:mainfrom
echoix:claude/raster2numpy-region-timing-fix
Sep 6, 2026
Merged

echoix merged 1 commit into
OSGeo:mainfrom
echoix:claude/raster2numpy-region-timing-fix

Conversation

@echoix

@echoix echoix commented Sep 5, 2026

Copy link
Copy Markdown
Member

This is to address a file that completely crashes instead of failing a single test (affected the others in the file). Part of the cleanup before randomizing tests order (for at least Windows).

Note that now, this allows 3 tests to fail on Windows (still under the threshold), instead of crashing (so hidden). I have another PR stacked up for that.

The rest below is AI explaining using context managers instead of not executing close() when the code had an exception.

Description

test_raster_region.py's test_resampling_1 and test_resampling_2 called
rast.close() as a bare statement after their assertions. If an assertion
(or anything else) raised, that close() call was skipped and the
RasterRow stayed open for the rest of the process.

Region.set_raster_region() documents exactly why that matters:

Attention: All raster objects must be closed or the process will be
terminated.

test_resampling_to_numpy, the next test in this file, calls
region.set_raster_region() on every resampling step. So a raster left open
by an earlier test doesn't just fail that earlier test — it crashes the
next test with an unrelated-looking C-level FERROR: Input window changed while maps are open for read, instead of whatever the real original failure
was.

Fixed by using with rast: (RasterRow already documents and supports this)
instead of a manual open()/close() pair, so the raster is always closed,
assertion failure or not.

Known follow-up

Windows CI on this branch shows the fatal, whole-file-crashing FERROR is
gone, but the file still doesn't fully pass: three individual failures show
up instead (count/value mismatches in test_resampling_1 and
test_resampling_2, and an oversized array allocation in
test_resampling_to_numpy). These point to a separate, pre-existing bug
where the small test region doesn't appear to actually restrict what gets
read back on Windows. I haven't been able to reproduce it without a Windows
machine, so I'm not attempting a fix for it here — this PR only fixes the
resource leak that was masking it.

Motivation and context

Found while investigating Windows CI test-order-dependency:
test_raster_region.py was one of a small set of consistently-failing
Windows gunittest files.

How has this been tested?

Windows CI on this branch (see the follow-up note above) — I couldn't
reproduce or build GRASS on Windows locally.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Checklist

  • PR title provides summary of the changes and starts with one of the
    pre-defined prefixes
  • My code follows the code style
    of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.

AI wrote the message, and researched a bit.

test_resampling_1 and test_resampling_2 called rast.close() as a bare
statement after their assertions, so a failing assertion (or any other
exception) would skip it, leaving the RasterRow open. This repo's own
Region.set_raster_region() documents the consequence: "All raster
objects must be closed or the process will be terminated." The next
test in this file, test_resampling_to_numpy, calls exactly that method
on every resampling step, so an open handle carried over from an
earlier test crashes it with an unrelated-looking C-level FERROR
instead of the real assertion failure ever being reported.

Use `with rast:` instead, matching RasterRow's documented context
manager support, so the raster is always closed, including when an
assertion fails.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@github-actions github-actions Bot added Python Related code is in Python libraries tests Related to Test Suite labels Sep 6, 2026
@echoix
echoix requested a review from ninsbl September 6, 2026 10:50
@echoix
echoix merged commit 9c62793 into OSGeo:main Sep 6, 2026
29 of 34 checks passed
@github-actions github-actions Bot added this to the 8.6.0 milestone Sep 6, 2026
echoix added a commit to echoix/grass that referenced this pull request Sep 6, 2026
…SGeo#7902)

test_resampling_1 and test_resampling_2 called rast.close() as a bare
statement after their assertions, so a failing assertion (or any other
exception) would skip it, leaving the RasterRow open. This repo's own
Region.set_raster_region() documents the consequence: "All raster
objects must be closed or the process will be terminated." The next
test in this file, test_resampling_to_numpy, calls exactly that method
on every resampling step, so an open handle carried over from an
earlier test crashes it with an unrelated-looking C-level FERROR
instead of the real assertion failure ever being reported.

Use `with rast:` instead, matching RasterRow's documented context
manager support, so the raster is always closed, including when an
assertion fails.

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
echoix added a commit to echoix/grass that referenced this pull request Sep 6, 2026
Follow-up to OSGeo#7902: fixing the resource leak there turned a fatal,
whole-file-crashing FERROR into three individually diagnosable
failures instead. The remaining issue is real but distinct from the
leak: the small region these tests set up isn't actually restricting
what RasterRow reads back (rast[0] comes back with roughly 100000
elements instead of 10).

Built GRASS locally and confirmed this is genuinely Windows-specific,
not a state leak reproducible here: the file passes reliably on Linux
both run in isolation and as part of the full 359-file gunittest suite
in sequence. Marking xfail on Windows per the existing convention
rather than guessing at a fix that can't be verified; needs
investigation with actual Windows access.
echoix added a commit that referenced this pull request Sep 8, 2026
…ows (#7905)

Follow-up to #7902: fixing the resource leak there turned a fatal,
whole-file-crashing FERROR into three individually diagnosable
failures instead. The remaining issue is real but distinct from the
leak: the small region these tests set up isn't actually restricting
what RasterRow reads back (rast[0] comes back with roughly 100000
elements instead of 10).

Built GRASS locally and confirmed this is genuinely Windows-specific,
not a state leak reproducible here: the file passes reliably on Linux
both run in isolation and as part of the full 359-file gunittest suite
in sequence. Marking xfail on Windows per the existing convention
rather than guessing at a fix that can't be verified; needs
investigation with actual Windows access.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

libraries Python Related code is in Python tests Related to Test Suite

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants