Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
61c73a8
Fix race condition in raster slot allocation and release (#6616)
Ankitkumarthakur0 Dec 11, 2025
566779a
Merge branch 'main' into fix-6616-race-condition
Ankitkumarthakur0 Dec 11, 2025
fbd0c0b
Update lib/raster/close.c
Ankitkumarthakur0 Dec 11, 2025
91b4bf8
Update lib/raster/close.c
Ankitkumarthakur0 Dec 11, 2025
83f49bb
Update lib/raster/close.c
Ankitkumarthakur0 Dec 11, 2025
979b78c
Update lib/raster/open.c
Ankitkumarthakur0 Dec 11, 2025
7c123d8
Update lib/raster/open.c
Ankitkumarthakur0 Dec 11, 2025
ea30a8c
Update lib/raster/open.c
Ankitkumarthakur0 Dec 11, 2025
d11fe9a
Update lib/raster/close.c
Ankitkumarthakur0 Dec 11, 2025
47359e0
Update lib/raster/close.c
Ankitkumarthakur0 Dec 12, 2025
4702539
Update lib/raster/open.c
Ankitkumarthakur0 Dec 12, 2025
7e58cd0
Update lib/raster/open.c
Ankitkumarthakur0 Dec 12, 2025
7e47761
Update lib/raster/open.c
Ankitkumarthakur0 Dec 15, 2025
03fd953
Update lib/raster/open.c
Ankitkumarthakur0 Dec 15, 2025
9d046e0
Update lib/raster/open.c
Ankitkumarthakur0 Dec 15, 2025
03bd453
Update lib/raster/open.c
Ankitkumarthakur0 Dec 15, 2025
26218ba
Update lib/raster/close.c
Ankitkumarthakur0 Dec 15, 2025
0b480f2
Update lib/raster/close.c
Ankitkumarthakur0 Dec 15, 2025
e6c9cbf
Update lib/raster/close.c
Ankitkumarthakur0 Dec 15, 2025
63ad679
Update lib/raster/close.c
Ankitkumarthakur0 Dec 15, 2025
7273c22
Update lib/raster/close.c
Ankitkumarthakur0 Dec 15, 2025
5e62ab7
Update lib/raster/close.c
Ankitkumarthakur0 Dec 15, 2025
916a315
Merge branch 'main' into fix-6616-race-condition
echoix Dec 15, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions lib/raster/close.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
*
* \author USACERL and many others
*/

#ifdef _WIN32
#include <windows.h>
#endif
Expand Down Expand Up @@ -82,7 +81,6 @@ static void write_fp_format(int fd);
* overwrite the support files. See \ref
* Raster_Map_Layer_Support_Routines for routines which write raster
* support files.
*
* If the map is a new floating point, move the <tt>.tmp</tt> file
* into the <tt>fcell</tt> element, create an empty file in the
* <tt>cell</tt> directory; write the floating-point range file; write
Expand All @@ -91,7 +89,6 @@ static void write_fp_format(int fd);
* file, with max cat = max value (for backwards compatibility). Move
* the <tt>.tmp</tt> NULL-value bitmap file to the <tt>cell_misc</tt>
* directory.
*
* \param fd file descriptor
*
* \return void
Expand Down Expand Up @@ -124,9 +121,7 @@ void Rast_close(int fd)
* eventually remove the temporary file, but the file can be quite
* large and will take up disk space until GRASS does remove it. Use
* this routine as a courtesy to the user.
*
* \param fd file descriptor
*
* \return void
*/
void Rast_unopen(int fd)
Expand All @@ -144,13 +139,11 @@ void Rast_unopen(int fd)

/*!
* \brief Unopen all raster maps
*
* Unopen all raster maps opened for write. Memory allocated for
* raster processing is freed, and the temporary file created when the
* raster map was opened is removed (see \ref
* Creating_and_Opening_New_Raster_Files). This routine is useful when
* errors are detected and it is desired to remove temporary files.
*
* \return void
*/
void Rast__unopen_all(void)
Expand Down Expand Up @@ -198,14 +191,16 @@ static int close_old(int fd)
G_free(fcb->name);
if (fcb->reclass_flag)
Rast_free_reclass(&fcb->reclass);
fcb->open_mode = -1;
// fcb->open_mode = -1;

if (fcb->map_type != CELL_TYPE) {
Rast_quant_free(&fcb->quant);
}
if (fcb->data_fd >= 0)
close(fcb->data_fd);

#pragma omp critical(R_RASTER_OPEN)
fcb->open_mode = -1;
return 1;
}

Expand Down Expand Up @@ -347,7 +342,7 @@ static int close_new_gdal(int fd, int ok)
Rast_close_gdal_link(fcb->gdal);
}

fcb->open_mode = -1;
/* fcb->open_mode = -1; */

if (fcb->data != NULL)
G_free(fcb->data);
Expand All @@ -361,6 +356,9 @@ static int close_new_gdal(int fd, int ok)
if (fcb->map_type != CELL_TYPE)
Rast_quant_free(&fcb->quant);

#pragma omp critical(R_RASTER_OPEN)
fcb->open_mode = -1;

return stat;
}

Expand Down Expand Up @@ -477,7 +475,7 @@ static int close_new(int fd, int ok)

sync_and_close(fcb->data_fd,
(fcb->map_type == CELL_TYPE ? "cell" : "fcell"), fcb->name);
fcb->open_mode = -1;
/* fcb->open_mode = -1; */

if (fcb->null_fd >= 0) {
sync_and_close(fcb->null_fd,
Expand Down Expand Up @@ -531,6 +529,8 @@ static int close_new(int fd, int ok)
if (fcb->map_type != CELL_TYPE)
Rast_quant_free(&fcb->quant);

#pragma omp critical(R_RASTER_OPEN)
fcb->open_mode = -1;
return stat;
}

Expand Down Expand Up @@ -577,6 +577,8 @@ void Rast__close_null(int fd)

G_free(fcb->null_bits);

/* fcb->open_mode = -1; */
#pragma omp critical(R_RASTER_OPEN)
fcb->open_mode = -1;
}

Expand Down Expand Up @@ -607,4 +609,4 @@ static void write_fp_format(int fd)
G_write_key_value_file(path, format_kv);

G_free_key_value(format_kv);
}
}
69 changes: 41 additions & 28 deletions lib/raster/open.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
*
* \author USACERL and many others
*/

#include <unistd.h>
#include <string.h>
#include <sys/types.h>
Expand All @@ -27,43 +26,59 @@
#include "R.h"
#define FORMAT_FILE "f_format"
#define NULL_FILE "null"
/* cmpressed null file */
// cmpressed null file
#define NULLC_FILE "nullcmpr"

static int new_fileinfo(void)
{
int oldsize = R__.fileinfo_count;
int newsize = oldsize;
int oldsize;
int newsize;
int i;
int fd = -1;

for (i = 0; i < oldsize; i++)
if (R__.fileinfo[i].open_mode <= 0) {
memset(&R__.fileinfo[i], 0, sizeof(struct fileinfo));
R__.fileinfo[i].open_mode = -1;
return i;
#pragma omp critical(R_RASTER_OPEN)
{
oldsize = R__.fileinfo_count;
newsize = oldsize;

for (i = 0; i < oldsize; i++) {
if (R__.fileinfo[i].open_mode <= 0) {
memset(&R__.fileinfo[i], 0, sizeof(struct fileinfo));
// mark as reserved
R__.fileinfo[i].open_mode = 200;
fd = i;
break;
}
}

if (newsize < 20)
newsize += 20;
else
newsize *= 2;
if (fd < 0) {
if (newsize < 20)
newsize += 20;
else
newsize *= 2;

R__.fileinfo = G_realloc(R__.fileinfo, newsize * sizeof(struct fileinfo));
R__.fileinfo =
G_realloc(R__.fileinfo, newsize * sizeof(struct fileinfo));

/* Mark all cell files as closed */
for (i = oldsize; i < newsize; i++) {
memset(&R__.fileinfo[i], 0, sizeof(struct fileinfo));
R__.fileinfo[i].open_mode = -1;
}
// Mark all cell files as closed
for (i = oldsize; i < newsize; i++) {
memset(&R__.fileinfo[i], 0, sizeof(struct fileinfo));
R__.fileinfo[i].open_mode = -1;
}

R__.fileinfo_count = newsize;
R__.fileinfo_count = newsize;

return oldsize;
// mark as reserved
R__.fileinfo[oldsize].open_mode = 200;
fd = oldsize;
}
}

return fd;
}

/*!
* \brief Open raster file
*
* Arrange for the NULL-value bitmap to be read as well as the raster
* map. If no NULL-value bitmap exists, arrange for the production of
* NULL-values based on zeros in the raster map. If the map is
Expand All @@ -73,11 +88,9 @@ static int new_fileinfo(void)
* the floating point map using uing quant rules other than the ones
* stored in map's quant file, he/she should call Rast_set_quant_rules()
* after the call to Rast_open_old().
*
* \param name map name
* \param open_mode mode
* \param map_type map type (CELL, FCELL, DCELL)
*
* \return open file descriptor ( >= 0) if successful
*/
static int open_raster_new(const char *name, int open_mode,
Expand Down Expand Up @@ -302,7 +315,7 @@ int Rast__open_old(const char *name, const char *mapset)
fcb->null_bits = Rast__allocate_null_bits(cellhd.cols);

/* mark closed */
fcb->open_mode = -1;
/* fcb->open_mode = -1; */

/* save name and mapset */
fcb->name = G_store(name);
Expand Down Expand Up @@ -528,7 +541,7 @@ static int open_raster_new_gdal(char *map, char *mapset,

/* mark closed */
fcb->map_type = map_type;
fcb->open_mode = -1;
/* fcb->open_mode = -1; */

fcb->gdal = Rast_create_gdal_link(map, map_type);
if (!fcb->gdal)
Expand Down Expand Up @@ -644,7 +657,7 @@ static int open_raster_new(const char *name, int open_mode,

/* mark closed */
fcb->map_type = map_type;
fcb->open_mode = -1;
/* fcb->open_mode = -1; */
fcb->gdal = NULL;
fcb->vrt = NULL;

Expand Down Expand Up @@ -1057,4 +1070,4 @@ void Rast_set_quant_rules(int fd, struct Quant *q)
Rast_quant_set_neg_infinite_rule(&fcb->quant, dcell, cell);
if (Rast_quant_get_pos_infinite_rule(q, &dcell, &cell) > 0)
Rast_quant_set_pos_infinite_rule(&fcb->quant, dcell, cell);
}
}
Loading