<!-- This issue tracker is only for technical issues related to Bitcoin Core. General bitcoin questions and/or support requests are best directed to the Bitcoin StackExchange at https://bitcoin.stackexchange.com. For reporting security issues, please read instructions at https://bitcoincore.org/en/contact/. If the node is "stuck" during sync or giving "block checksum mismatch" errors, please ensure your hardware is stable by running memtest and observe CPU temperature with a load-test tool such as linpack before creating an issue! -->
<!-- Describe the issue -->
We detected a potential race on the global variable fHavePruned with a static data race detection tool.
Because the report is from a static analysis tool, we do not have a concrete execution or trace, but we have reviewed the report and are reasonably confident this variable can be accessed in parallel and is not guarded by any common locks.
We are not able to confirm 100% that this race is real, or what impacts it may have on the program, but we decided to report it just to be safe.
Writing Thread
The flag can be set to true by the thread spawned at init.cpp:1883
This thread can call ThreadImport -> LoadExternalBlockFile -> AcceptBlock -> FlushStateToDisk
And the FlushStateToDisk function can write to the fHavePruned flag.
Reading Thread
The fHavePruned flag can be read as part of the REST HTTP handler rest_block_extended which registered at rest.cpp:690.
From there the handler can call rest_block -> blockToJSON -> IsBlockPruned
And IsBlockPruned will read fHavePruned
Expected behavior
No threads reading and writing to the same variable without synchronization.
Actual behavior
It appears possible for threads to read and write fHavePruned in parallel.
To reproduce
May be difficult to reproduce consistently as we found the potential race through static analysis and data races can be non-deterministic.
System information
We analyzed commit 328aaac80 specifically, but the issue is still present at the time of posting this issue.
For reference, here is a screenshot of the report generated by our tool (Coderrect Scanner). I have summarized all the info from this report above.
