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
  1. practicalswift commented at 10:28 AM on November 6, 2017: contributor

    blockToJSON(...) and blockheaderToJSON(...) read the variable chainActive which requires holding the mutex cs_main. So does GetDifficulty(...).

  2. fanquake added the label RPC/REST/ZMQ on Nov 6, 2017
  3. 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?

  4. promag commented at 2:55 PM on November 6, 2017: member

    Assert lock is held in those functions?

  5. practicalswift force-pushed on Nov 6, 2017
  6. practicalswift commented at 3:22 PM on November 6, 2017: contributor

    @promag Feedback addressed! :-)

  7. TheBlueMatt commented at 8:30 PM on November 6, 2017: member

    Kinda 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().

  8. practicalswift force-pushed on Nov 6, 2017
  9. practicalswift commented at 8:49 PM on November 6, 2017: contributor

    @TheBlueMatt Good point. Locking now moved down one level into blockToJSON/blockheaderToJSON. Looks good?

  10. TheBlueMatt commented at 9:19 PM on November 6, 2017: member

    utACK 758b126897fdc596af70424b6e82cbcda0e3baa3, though the commit message is now somewhat confused (refers to "rest" but only touches src/rpc/*).

  11. 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?

  12. 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, 2017
  13. practicalswift force-pushed on Nov 6, 2017
  14. practicalswift renamed this:
    rpc: Add missing cs_main locks when calling blockToJSON/blockheaderToJSON
    rpc: Lock cs_main in blockToJSON/blockheaderToJSON
    on Nov 6, 2017
  15. practicalswift commented at 9:26 PM on November 6, 2017: contributor

    @TheBlueMatt Commit message and PR title adjusted.

  16. 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.

  17. practicalswift force-pushed on Nov 6, 2017
  18. practicalswift commented at 10:29 PM on November 6, 2017: contributor

    @TheBlueMatt @promag Updated! Please re-review :-)

  19. promag commented at 11:19 PM on November 6, 2017: member

    utACK 9dda6900.

  20. 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.

  21. TheBlueMatt commented at 11:43 PM on November 6, 2017: member

    utACK 9dda690028c719ae074802933f76ef7c45f6dbb6

  22. Add missing cs_main locks when calling blockToJSON/blockheaderToJSON a9b6ba0b7c
  23. practicalswift force-pushed on Nov 7, 2017
  24. practicalswift commented at 6:18 AM on November 7, 2017: contributor

    Indentation fixed. Please re-review :-)

  25. promag commented at 3:06 PM on November 7, 2017: member

    re-utACK a9b6ba0.

  26. MarcoFalke merged this on Nov 7, 2017
  27. MarcoFalke closed this on Nov 7, 2017

  28. MarcoFalke referenced this in commit 87d90efd69 on Nov 7, 2017
  29. MarcoFalke commented at 4:20 PM on November 7, 2017: member

    utACK a9b6ba0b7

  30. PastaPastaPasta referenced this in commit 37fcc4f275 on Dec 22, 2019
  31. PastaPastaPasta referenced this in commit 892ec3f9c9 on Jan 2, 2020
  32. PastaPastaPasta referenced this in commit 48943bb0be on Jan 4, 2020
  33. PastaPastaPasta referenced this in commit 2bb10d6dc7 on Jan 12, 2020
  34. PastaPastaPasta referenced this in commit a2b4aac850 on Jan 12, 2020
  35. PastaPastaPasta referenced this in commit 1abb315b53 on Jan 12, 2020
  36. PastaPastaPasta referenced this in commit 18df3eb524 on Jan 12, 2020
  37. PastaPastaPasta referenced this in commit fd4109d1e9 on Jan 12, 2020
  38. PastaPastaPasta referenced this in commit d7a1412e7d on Jan 12, 2020
  39. PastaPastaPasta referenced this in commit 458db168d4 on Jan 16, 2020
  40. LarryRuane referenced this in commit 9e92d9f18c on Mar 3, 2021
  41. ckti referenced this in commit 4221a588aa on Mar 28, 2021
  42. practicalswift deleted the branch on Apr 10, 2021
  43. LarryRuane referenced this in commit d8926263b7 on Apr 13, 2021
  44. zkbot referenced this in commit 0e36226271 on Apr 17, 2021
  45. gades referenced this in commit 7bcc886ddd on Jan 29, 2022
  46. DrahtBot locked this on Aug 16, 2022

github-metadata-mirror

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

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me