Skip to content

Commit

Permalink
Protect access to ConvergedStats.csv.gz with a lock
Browse files Browse the repository at this point in the history
StoreStats in the scraper, which rewrites ConvergedStats.csv.gz, can be called
from at least two different threads. The file access should be protected by a lock.
  • Loading branch information
jamescowens committed Oct 11, 2024
1 parent 1081d53 commit 1f388c3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/gridcoin/scraper/scraper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ CCriticalSection cs_ScraperGlobals;
* file side.
*/
CCriticalSection cs_StructScraperFileManifest;
/**
* @brief Protects access to the ConvergedStats.csv.gz compressed statistics map output file.
*/
CCriticalSection cs_ConvergedStats;
/**
* @brief Protects the global converged scraper stats cache, which is populated by periodic runs of the scraper and/or
* subscriber loop, and is used to validate superblocks.
Expand Down Expand Up @@ -3452,6 +3456,8 @@ bool StoreScraperFileManifest(const fs::path& file)

bool StoreStats(const fs::path& file, const ScraperStats& mScraperStats)
{
LOCK(cs_ConvergedStats);

if (fs::exists(file))
fs::remove(file);

Expand Down Expand Up @@ -6069,13 +6075,13 @@ UniValue testnewsb(const UniValue& params, bool fHelp)
{
LOCK(cs_ConvergedScraperStatsCache);

if (!ConvergedScraperStatsCache.NewFormatSuperblock.WellFormed())
{
UniValue error(UniValue::VOBJ);
error.pushKV("Error:", "Wait until a convergence is formed.");
if (!ConvergedScraperStatsCache.NewFormatSuperblock.WellFormed())
{
UniValue error(UniValue::VOBJ);
error.pushKV("Error:", "Wait until a convergence is formed.");

return error;
}
return error;
}

_log(logattribute::INFO, "testnewsb",
"Size of the PastConvergences map = " + ToString(ConvergedScraperStatsCache.PastConvergences.size()));
Expand Down
1 change: 1 addition & 0 deletions src/gridcoin/scraper/scraper.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
extern CCriticalSection cs_Scraper;
extern CCriticalSection cs_ScraperGlobals;
extern CCriticalSection cs_StructScraperFileManifest;
extern CCriticalSection cs_ConvergedStats;
extern CCriticalSection cs_ConvergedScraperStatsCache;
extern CCriticalSection cs_TeamIDMap;
extern CCriticalSection cs_VerifiedBeacons;
Expand Down

0 comments on commit 1f388c3

Please sign in to comment.