blockToJSON(...) and blockheaderToJSON(...) read the variable chainActive which requires holding the mutex cs_main. So does GetDifficulty(...).
rpc: Lock cs_main in blockToJSON/blockheaderToJSON #11618
pull practicalswift wants to merge 1 commits into bitcoin:master from practicalswift:cs_main-blockToJSON-blockheaderToJSON changing 2 files +12 −3-
practicalswift commented at 10:28 AM on November 6, 2017: contributor
- fanquake added the label RPC/REST/ZMQ on Nov 6, 2017
-
in src/rest.cpp:182 in 93f57cb6cd outdated
178 | @@ -179,6 +179,7 @@ static bool rest_headers(HTTPRequest* req, 179 | case RF_JSON: { 180 | UniValue jsonHeaders(UniValue::VARR); 181 | for (const CBlockIndex *pindex : headers) { 182 | + LOCK(cs_main);
promag commented at 2:53 PM on November 6, 2017:Lock once, before loop?
promag commented at 2:55 PM on November 6, 2017: memberAssert lock is held in those functions?
practicalswift force-pushed on Nov 6, 2017practicalswift commented at 3:22 PM on November 6, 2017: contributor@promag Feedback addressed! :-)
TheBlueMatt commented at 8:30 PM on November 6, 2017: memberKinda sucks to hold cs_main while calling httpserver methods like WriteHeader/WriteReply - care to just use the recursiveness of cs_main and lock in the ToJSON functions themselves? Longer-term I really want to move chainActive to support read-only locks so we dont block the world for trivial things like chainActive.Contains() and chainActive.Height().
practicalswift force-pushed on Nov 6, 2017practicalswift commented at 8:49 PM on November 6, 2017: contributor@TheBlueMatt Good point. Locking now moved down one level into
blockToJSON/blockheaderToJSON. Looks good?TheBlueMatt commented at 9:19 PM on November 6, 2017: memberutACK 758b126897fdc596af70424b6e82cbcda0e3baa3, though the commit message is now somewhat confused (refers to "rest" but only touches src/rpc/*).
promag commented at 9:22 PM on November 6, 2017: member@TheBlueMatt the lock scope could be reduced so that WriteHeader/WriteReply wouldn't keep it. The current approach can give inconsistencies: while iterating the chain can change, and the response can have mixed data. Even with RW locks care must be taken to ensure consistency in the responses.
So, IMO this is the correct implementation:
case RF_JSON: { UniValue jsonHeaders(UniValue::VARR); { LOCK(cs_main); for (const CBlockIndex* pindex : headers) { jsonHeaders.push_back(blockheaderToJSON(pindex)); } } std::string strJSON = jsonHeaders.write() + "\n"; req->WriteHeader("Content-Type", "application/json"); req->WriteReply(HTTP_OK, strJSON); return true; }WDYT?
practicalswift renamed this:rest: Add missing cs_main locks when calling blockToJSON/blockheaderToJSON
rpc: Add missing cs_main locks when calling blockToJSON/blockheaderToJSON
on Nov 6, 2017practicalswift force-pushed on Nov 6, 2017practicalswift renamed this:rpc: Add missing cs_main locks when calling blockToJSON/blockheaderToJSON
rpc: Lock cs_main in blockToJSON/blockheaderToJSON
on Nov 6, 2017practicalswift commented at 9:26 PM on November 6, 2017: contributor@TheBlueMatt Commit message and PR title adjusted.
TheBlueMatt commented at 9:50 PM on November 6, 2017: member@promag gah, indeed, missed that its called in a loop there - should indeed lock outside the loop.
practicalswift force-pushed on Nov 6, 2017practicalswift commented at 10:29 PM on November 6, 2017: contributor@TheBlueMatt @promag Updated! Please re-review :-)
promag commented at 11:19 PM on November 6, 2017: memberutACK 9dda6900.
in src/rest.cpp:184 in 9dda690028 outdated
177 | @@ -178,8 +178,11 @@ static bool rest_headers(HTTPRequest* req, 178 | } 179 | case RF_JSON: { 180 | UniValue jsonHeaders(UniValue::VARR); 181 | - for (const CBlockIndex *pindex : headers) { 182 | - jsonHeaders.push_back(blockheaderToJSON(pindex)); 183 | + { 184 | + LOCK(cs_main); 185 | + for (const CBlockIndex *pindex : headers) { 186 | + jsonHeaders.push_back(blockheaderToJSON(pindex));
TheBlueMatt commented at 11:41 PM on November 6, 2017:nit: bad indentation here.
TheBlueMatt commented at 11:43 PM on November 6, 2017: memberutACK 9dda690028c719ae074802933f76ef7c45f6dbb6
Add missing cs_main locks when calling blockToJSON/blockheaderToJSON a9b6ba0b7cpracticalswift force-pushed on Nov 7, 2017practicalswift commented at 6:18 AM on November 7, 2017: contributorIndentation fixed. Please re-review :-)
promag commented at 3:06 PM on November 7, 2017: memberre-utACK a9b6ba0.
MarcoFalke merged this on Nov 7, 2017MarcoFalke closed this on Nov 7, 2017MarcoFalke referenced this in commit 87d90efd69 on Nov 7, 2017MarcoFalke commented at 4:20 PM on November 7, 2017: memberutACK a9b6ba0b7
PastaPastaPasta referenced this in commit 37fcc4f275 on Dec 22, 2019PastaPastaPasta referenced this in commit 892ec3f9c9 on Jan 2, 2020PastaPastaPasta referenced this in commit 48943bb0be on Jan 4, 2020PastaPastaPasta referenced this in commit 2bb10d6dc7 on Jan 12, 2020PastaPastaPasta referenced this in commit a2b4aac850 on Jan 12, 2020PastaPastaPasta referenced this in commit 1abb315b53 on Jan 12, 2020PastaPastaPasta referenced this in commit 18df3eb524 on Jan 12, 2020PastaPastaPasta referenced this in commit fd4109d1e9 on Jan 12, 2020PastaPastaPasta referenced this in commit d7a1412e7d on Jan 12, 2020PastaPastaPasta referenced this in commit 458db168d4 on Jan 16, 2020LarryRuane referenced this in commit 9e92d9f18c on Mar 3, 2021ckti referenced this in commit 4221a588aa on Mar 28, 2021practicalswift deleted the branch on Apr 10, 2021LarryRuane referenced this in commit d8926263b7 on Apr 13, 2021zkbot referenced this in commit 0e36226271 on Apr 17, 2021gades referenced this in commit 7bcc886ddd on Jan 29, 2022DrahtBot locked this on Aug 16, 2022
This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-13 15:15 UTC
More mirrored repositories can be found on mirror.b10c.me