Split up AppInit2 into multiple phases, daemonize after datadir lock errors #9010

pull laanwj wants to merge 3 commits into bitcoin:master from laanwj:2016_10_init_split_daemon changing 6 files +121 −35
  1. laanwj commented at 10:20 am on October 25, 2016: member

    Split up AppInit2 into multiple steps: This allows doing some of the steps before daemonization and some later.

    To avoid the issue described in #9009 (comment):

    • Before daemonization, just probe the data directory lock and print an early error message if possible.
    • After daemonization get the data directory lock again and hold on to it until exit. This creates a slight window for a race condition to happen, however this condition is harmless: it will at most make the process exit without printing a message to console as it will be detected after daemonization.

    Solves #9009:

    0$ src/bitcoind -testnet -daemon
    1Bitcoin server starting
    2$ src/bitcoind -testnet -daemon
    3Error: Cannot obtain a lock on data directory /home/orion/.bitcoin/testnet3. Bitcoin Core is probably already running.
    

    As for the AppInit2 split: This is something I’ve been wanting to do for a while. Note that, to reduce risk of regressions, this is just mechanic code movement and doesn’t change ordering, besides the daemon() invocation.

    Some smarter things could be done such as:

    • Move basic context initialization from AppInitBasicSetup() to SetupEnvironment() where possible, especially things such as the windows DEP activation
    • Unify InitParameterInteraction() and AppInitParameterInteraction() if/where possible. I’m not sure why this is done in two steps with just AppInitBasicSetup() in between right now.
    • Do something smarter than // Variables internal to initialization process only (e.g. an initialization context structure). Some variables there may be completely avoided by moving the code to extract the arguments.
    • Splitting up further steps, this will allow factoring out ENABLE_WALLET sections from libbitcoin_server.a (which contains init.cpp) and remove the circular dependencies (#7965)
    • It is now possible to do unit testing on our initialization steps in isolation.

    However I do not intend to do these things in this pull.

  2. laanwj added the label Refactoring on Oct 25, 2016
  3. laanwj added the label Linux/Unix on Oct 25, 2016
  4. jonasschnelli commented at 6:33 pm on October 25, 2016: contributor
    Code Review utACK feaea7001fdec6c0ec5ef6cc1b33cb214131ce25. And yes, I think we should unify InitParameterInteraction and AppInitParameterInteraction() to avoid confusion (can be done in a later PR).
  5. gmaxwell commented at 9:04 pm on November 24, 2016: contributor
    ACK.
  6. sipa commented at 8:28 pm on November 25, 2016: member
    utACK feaea7001fdec6c0ec5ef6cc1b33cb214131ce25
  7. MarcoFalke approved
  8. MarcoFalke commented at 11:47 am on November 26, 2016: member
    utACK feaea7001fdec6c0ec5ef6cc1b33cb214131ce25
  9. in src/init.cpp: in feaea7001f outdated
    987@@ -977,8 +988,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
    988     // Option to startup with mocktime set (used for regression testing):
    989     SetMockTime(GetArg("-mocktime", 0)); // SetMockTime(0) is a no-op
    990 
    991-    ServiceFlags nLocalServices = NODE_NETWORK;
    992-    ServiceFlags nRelevantServices = NODE_NETWORK;
    993+    nLocalServices = NODE_NETWORK;
    


    MarcoFalke commented at 11:52 am on November 26, 2016:
    Any reason the default value was not assigned in the namespace (like nRelevantServices)?

    laanwj commented at 4:00 pm on November 28, 2016:
    No reason, will do that.
  10. morcos commented at 3:28 pm on November 28, 2016: member
    @laanwj I don’t think you’re setting fServer correctly. I think you need to move the SoftSetBoolArg("-server", true) to before AppInitParameterInteraction.
  11. laanwj commented at 3:59 pm on November 28, 2016: member

    @laanwj I don’t think you’re setting fServer correctly. I think you need to move the SoftSetBoolArg("-server", true) to before AppInitParameterInteraction.

    There isn’t any parameter interaction based on -server, so I don’t think this makes a difference in practice. It’d be more logical to move it though, so I will.

  12. morcos commented at 4:25 pm on November 28, 2016: member

    @laanwj fServer gets set in AppInitParameterInteraction. I compiled this PR and ran src/bitcoind -daemon and was unable to connect to bitcoind using bitcoin-cli.

    EDIT: Perhaps the right solution is to remove fServer

  13. laanwj commented at 4:30 pm on November 28, 2016: member

    @laanwj fServer gets set in AppInitParameterInteraction. I compiled this PR and ran src/bitcoind -daemon and was unable to connect to bitcoind using bitcoin-cli.

    Oh you’re right, sorry. I expected the RPC tests to catch this but apparently they set -server explicitly.

    0qa/rpc-tests/test_framework/util.py:            args = [ os.getenv("BITCOIND", "bitcoind"), "-server", "-keypool=1", "-datadir="+datadir, "-discover=0" ]
    1qa/rpc-tests/test_framework/util.py:    args = [ binary, "-datadir="+datadir, "-server", "-keypool=1", "-discover=0", "-rest", "-mocktime="+str(get_mocktime()) ]
    

    EDIT: Perhaps the right solution is to remove fServer

    It exists to make running a RPC server optional (disabled by default) with the GUI. I think we should keep that. In any case the goal here is not to change the working of any options, so the setting should just be moved…

  14. morcos commented at 4:49 pm on November 28, 2016: member
    Yes I just meant get rid of the variable fServer since its used only once and in AppInitMain just check GetBoolArg("-server", false) directly, but either way.
  15. init: Split up AppInit2 into multiple phases
    This allows doing some of the steps before e.g. daemonization and some
    fater.
    0cc8b6bc44
  16. init: Try to aquire datadir lock before and after daemonization
    Before daemonization, just probe the data directory lock and print an
    early error message if possible.
    
    After daemonization get the data directory lock again and hold on to it until exit
    This creates a slight window for a race condition to happen, however this condition is harmless: it
    will at most make us exit without printing a message to console.
    
        $ src/bitcoind -testnet -daemon
        Bitcoin server starting
        $ src/bitcoind -testnet -daemon
        Error: Cannot obtain a lock on data directory /home/orion/.bitcoin/testnet3. Bitcoin Core is probably already running.
    16ca0bfd28
  17. init: Get rid of fServer flag
    There is no need to store this flag globally, the variable is only used
    inside the initialization process.
    
    Thanks to Alex Morcos for the idea.
    deec83fd2c
  18. laanwj force-pushed on Nov 29, 2016
  19. laanwj commented at 11:57 am on November 29, 2016: member

    Yes I just meant get rid of the variable fServer since its used only once and in AppInitMain just check GetBoolArg("-server", false) directly, but either way.

    Good idea. I pushed these changes:

    • Initialize nRelevantServices in the namespace like nLocalServices (MarcoFalke’s comment)
    • Move SetSoftBool("-server") to before parameter interaction in bitcoind.cpp, in case someone adds parameter interaction based on this
    • Get rid of global fServer flag
  20. morcos commented at 2:46 pm on November 29, 2016: member
    ACK deec83f
  21. MarcoFalke commented at 0:16 am on November 30, 2016: member
    reACK deec83f. Good finding @morcos!
  22. sipa merged this on Dec 1, 2016
  23. sipa closed this on Dec 1, 2016

  24. sipa referenced this in commit a143b88dbd on Dec 1, 2016
  25. codablock referenced this in commit 90a96c49b0 on Jan 16, 2018
  26. codablock referenced this in commit e11535531b on Jan 16, 2018
  27. codablock referenced this in commit b07393709e on Jan 17, 2018
  28. andvgal referenced this in commit fd4f19e8a4 on Jan 6, 2019
  29. CryptoCentric referenced this in commit c575f14059 on Feb 25, 2019
  30. MarcoFalke locked this on Sep 8, 2021

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: 2024-10-05 01:12 UTC

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