rpc: Avoid crash when g_thread_http was never started #19006

pull MarcoFalke wants to merge 3 commits into bitcoin:master from MarcoFalke:2005-httpNoCrash changing 3 files +21 −21
  1. MarcoFalke commented at 1:11 PM on May 18, 2020: member

    Avoid a crash during shutdown when the init sequence failed for some reason

  2. fanquake added the label RPC/REST/ZMQ on May 18, 2020
  3. MarcoFalke force-pushed on May 18, 2020
  4. MarcoFalke renamed this:
    http: Avoid crash when g_thread_http was never started
    rpc: Avoid crash when g_thread_http was never started
    on May 18, 2020
  5. DrahtBot commented at 3:05 PM on May 18, 2020: member

    <!--e57a25ab6845829454e8d69fc972939a-->

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #15363 (wip: http: Exit the event loop as soon as there are no active events by promag)

    If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

  6. ryanofsky commented at 12:43 PM on May 19, 2020: member

    Marco, any chance you could start making formatting cleanups in separate commits? Trying to skim this PR and quickly figure out what the bug is, and what the fix is is pointlessly difficult. It's like playing Where's Waldo.

  7. in src/httpserver.cpp:470 in faf838bf1c outdated
     466 | @@ -467,7 +467,7 @@ void StopHTTPServer()
     467 |      boundSockets.clear();
     468 |      if (eventBase) {
     469 |          LogPrint(BCLog::HTTP, "Waiting for HTTP event thread to exit\n");
     470 | -        threadHTTP.join();
     471 | +        if (g_thread_http.joinable()) g_thread_http.join();
    


    ryanofsky commented at 12:45 PM on May 19, 2020:

    In commit "http: Avoid crash when g_thread_http was never started" (faf838bf1cae719c46211ea603848f367ee427cf)

    I found Waldo!

  8. in test/functional/rpc_users.py:108 in faf838bf1c outdated
     113 | +        self.log.info('Check that failure to write cookie file will abort the node gracefully')
     114 | +        self.stop_node(0)
     115 | +        cookie_file = os.path.join(get_datadir_path(self.options.tmpdir, 0), self.chain, '.cookie.tmp')
     116 | +        with open(cookie_file, 'w', encoding='utf8') as f:
     117 | +            f.write('readonly')
     118 | +        os.chmod(cookie_file, S_IREAD | S_IRGRP | S_IROTH)
    


    ryanofsky commented at 12:50 PM on May 19, 2020:

    In commit "http: Avoid crash when g_thread_http was never started" (faf838bf1cae719c46211ea603848f367ee427cf)

    Not sure, but it might be more portable to make cookie_file a non-file path e.g. os.mkdir(cookie_file), because I'm not sure different file systems like fat will have good enough support for chmod.

    Possible travis errors like https://travis-ci.org/github/bitcoin/bitcoin/jobs/688369052#L5050 might be happening for this reason


    MarcoFalke commented at 5:05 PM on May 19, 2020:

    Thanks for finding out why this failed in travis, but not locally

  9. ryanofsky approved
  10. ryanofsky commented at 12:54 PM on May 19, 2020: member

    Code review ACK faf838bf1cae719c46211ea603848f367ee427cf, though it looks like the new test might be failing to trigger the bug some cases and causing travis errors

  11. MarcoFalke commented at 12:59 PM on May 19, 2020: member

    Marco, any chance you could start making formatting cleanups in separate commits?

    Are you referring to the first commit? My reasoning is that highlighting the call stack would make review easier, as it can be done with a single git command (instead of having to manually grep for the call stack). This is the way I would prefer when reviewing. Though, I will never review my own commits and if this makes review harder for others, I am happy to drop this habit.

  12. ryanofsky commented at 1:25 PM on May 19, 2020: member

    Marco, any chance you could start making formatting cleanups in separate commits?

    Are you referring to the first commit? My reasoning is that highlighting the call stack would make review easier, as it can be done with a single git command (instead of having to manually grep for the call stack). This is the way I would prefer when reviewing. Though, I will never review my own commits and if this makes review harder for others, I am happy to drop this habit.

    The issue is that most of the time when I am looking at a diff I am not reviewing it, I'm trying to understand what it does. I review it after I understand what it does, not before. Making different changes in the same commit makes it take longer to figure out what the commit does.

    But I actually don't understand what you're referring to with single git commands and calls stacks. I definitely think it's good to have a bugfix and tests for the bug in a single commit instead of different commits, because understanding the test can help you understand the bugfix and vice versa. I'm just requesting moving reformatting changes to separate commits from real code changes, because these aren't helpful to view together. Real changes break the flow of reviewing reformatting changes, and reformatting changes make it harder to find and notice real changes

  13. ryanofsky commented at 1:30 PM on May 19, 2020: member

    Are you referring to the first commit?

    I'm referring to no-op reformatting changes both commits: braces, whitespace, comments, variable renames, python log tweaks in both commits that get in the way of understanding the bugfix and test. The cleanups are great, I'm just suggesting to put them in a cleanup commit.

  14. init: Remove confusing and redundant InitError
    The "A fatal internal error occurred, see debug.log for details" is
    redundant because init.cpp will already show an InitError with a better
    error message as well as the hint to check the debug.log
    fa83b39ff3
  15. test: Replace inline-comments with logs, pep8 formatting fa12a37b27
  16. http: Avoid crash when g_thread_http was never started
    g_thread_http can not be joined when it is not joinable. Avoid crashing
    the node by adding the required check and add a test.
    faf45d1f1f
  17. MarcoFalke force-pushed on May 19, 2020
  18. MarcoFalke commented at 2:46 PM on May 19, 2020: member

    Marco, any chance you could start making formatting cleanups in separate commits

    I think going forward, you can say stuff like "Concept ACK, but I refuse to review this as long as formatting cleanups are in the same commit as bug fixes". (This leaves me with no option other than addressing the feedback and I will adjust my behaviour in the future to avoid a review round-trip time)

  19. promag commented at 4:57 PM on May 19, 2020: member

    Tested ACK faf45d1f1f997c316fc4c611a23c4456533eefe9.

    Maybe do the same check in StopTorControl?

  20. MarcoFalke commented at 5:04 PM on May 19, 2020: member

    @promag StopTorControl should be unaffected by the bug, no? Or do you have steps to reproduce?

  21. ryanofsky approved
  22. ryanofsky commented at 6:44 PM on May 19, 2020: member

    Code review ACK faf45d1f1f997c316fc4c611a23c4456533eefe9. Thanks for updates, this is much easier to parse for me now. Since previous reviews: split out and reverted some cleanups & replaced chmod with mkdir in test

  23. ryanofsky commented at 6:59 PM on May 19, 2020: member

    I think going forward, you can say stuff like "Concept ACK, but I refuse to review this as long as formatting cleanups are in the same commit as bug fixes"

    Sorry, I should have prefixed that with concept ACK. I wasn't refusing to review it though, and followed up with a regular code review ACK a few minutes after.

    This leaves me with no option other than addressing the feedback and I will adjust my behaviour in the future to avoid a review round-trip time

    I was just trying to suggest something to make this easier to review. Sometimes I forget to write it but you can always ignore my other feedback when I ACK something (I wouldn't ACK something I didn't think was an improvement.)

  24. hebasto commented at 7:09 AM on May 20, 2020: member

    Concept ACK.

  25. promag commented at 8:24 AM on May 20, 2020: member

    re #19006 (comment)

    Or do you have steps to reproduce?

    No, I don't think it's possible to hit the same problem there.

  26. hebasto approved
  27. hebasto commented at 8:45 AM on May 20, 2020: member

    ACK faf45d1f1f997c316fc4c611a23c4456533eefe9, tested on Linux Mint 19.3 with the following patch:

    --- a/src/httprpc.cpp
    +++ b/src/httprpc.cpp
    @@ -249,7 +249,7 @@ static bool InitRPCAuthentication()
         if (gArgs.GetArg("-rpcpassword", "") == "")
         {
             LogPrintf("No rpcpassword set - using random cookie authentication.\n");
    -        if (!GenerateAuthCookie(&strRPCUserColonPass)) {
    +        if (GenerateAuthCookie(&strRPCUserColonPass)) {
                 return false;
             }
         } else {
    

    No crash as expected.

    The excerpt from the log:

    2020-05-20T08:41:15Z [init] HTTP: creating work queue of depth 16
    2020-05-20T08:41:15Z [init] No rpcpassword set - using random cookie authentication.
    2020-05-20T08:41:15Z [init] Generated RPC authentication cookie /home/hebasto/.bitcoin/.cookie
    2020-05-20T08:41:15Z [init] Error: Unable to start HTTP server. See debug log for details.
    2020-05-20T08:41:15Z [init] Shutdown: In progress...
    2020-05-20T08:41:15Z [scheduler] scheduler thread exit
    2020-05-20T08:41:15Z [shutoff] Shutdown: done
    
  28. MarcoFalke merged this on May 20, 2020
  29. MarcoFalke closed this on May 20, 2020

  30. MarcoFalke deleted the branch on May 20, 2020
  31. Fabcien referenced this in commit eb6a052baa on Feb 17, 2021
  32. DrahtBot locked this on Feb 15, 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:14 UTC

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