-
-
Notifications
You must be signed in to change notification settings - Fork 392
lib/raster: Fix race condition in fileinfo slot allocation and release (#6616) #6716
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
lib/raster: Fix race condition in fileinfo slot allocation and release (#6616) #6716
Conversation
|
I don't really have enough expertise to assess the changes here, but it may be better idea for a new contributor to pick up something easier, than modifying the core raster library. |
|
Thanks a lot for the feedback! This PR was a learning attempt for me as a new contributor, and I’m happy to pick up something simpler next. Thanks again for your support! |
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
|
Please see https://github.com/OSGeo/grass/blob/main/doc/development/style_guide.md#using-pre-commit for formal issues |
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
|
Thanks a lot for your contribution! Two comments:
This makes it easier for others to concentrate on the important changes introduced with a PR. |
Overview
This pull request provides a complete fix for the race condition described in Issue #6616, where concurrent raster operations could incorrectly allocate the same file descriptor slot.
The update ensures that both slot allocation and slot release occur atomically, preventing double assignment and maintaining consistency in multi-threaded execution.
Root Cause Analysis
The function new_fileinfo() was not thread-safe:
Multiple threads could identify the same free slot before it was reserved
-The reserved state was not applied early enough
-Certain code paths prematurely reset open_mode = -1, exposing a slot as “free” while still in use
Under parallel execution, these conditions triggered:
-Double slot allocation
-Crashes or undefined behavior
-Data corruption during raster operations
Solution Implemented
1. Atomic Slot Allocation
Allocation logic is now wrapped in:
#pragma omp critical (R_RASTER_OPEN)
This guarantees exclusive ownership from the moment a slot is selected.
2. Removal of Premature Resets
These resets have been removed to ensure reservation is not undone before the open operation completes.
3. Atomic Slot Release
Files Modified
lib/raster/open.c
lib/raster/close.c
Testing & Validation
Python Concurrency Simulation (simulation_test.py)
Executed with 50 threads to model high-contention scenarios.
-Before fix: frequent collisions, inconsistent ownership
-After fix: 0 collisions across multiple runs
C Reproduction (repro_issue_6616.c)
OpenMP-based reproduction confirms:
-No repeated allocation of the same slot
-No corruption of slot state
-Stable behavior under multi-threaded load
Outcome
This fix brings:
-Fully thread-safe raster slot management
-No premature frees
-No double assignment
-Consistent behavior across all parallel raster workflows
This resolves Issue #6616 fully and makes the raster subsystem significantly more robust in concurrent environments.
Issue Reference
Fixes: #6616
Request for Review
Feedback from maintainers is welcome.
Thank you for reviewing this contribution — I am happy to iterate if needed.
cc @metzm @neteler @grassgis