Skip to content

Conversation

benthecarman
Copy link
Contributor

@benthecarman benthecarman commented Feb 7, 2019

Thoughts for adding the feature is for users to be able to add things like electrum-personal-server or lnd to run whenever Bitcoin Core is running. Open to feedback about the feature.

@kristapsk
Copy link
Contributor

I don't think this should be the scope of Bitcoin Core. Service dependencies should be handled by the init / service management system you are using. Or you can just create a simple shell script, to run all the stuff you need. Besides, it is usually recommended to run different services under different users, if possible, to increase security.

@gmaxwell
Copy link
Contributor

gmaxwell commented Feb 7, 2019

@kristapsk Startup services can't trigger when bitcoin is ready to be actually used. That is the big reason for a notify here. Yes, as an alternative software could just poll until its ready but the same could be said for blocknotifys.

Historically speaking-- startup scripts people have created for bitcoin have been uniformly low quality (e.g. making the software unusable providing no way to get access to the rpc cookie, killing the process and corrupting its state on shutdown, etc). I have generally recommended against using these package scripts due to their low quality, so I think it would be asking a lot of them to expect them to start intelligently starting other services when so far not corrupting the Bitcoin install has been a challenge for them.

@promag
Copy link
Contributor

promag commented Feb 8, 2019

I understand the need, I'm not fond of the solution. I think it's perfectly sane to poll the interface or locally run something like netstat.

Does anyone knows an example using this strategy?

@gmaxwell
Copy link
Contributor

gmaxwell commented Feb 9, 2019

I understand the need, I'm not fond of the solution. I think it's perfectly sane to poll the interface or locally run something like netstat.

Huh. Netstat can not tell you if bitcoind is done starting. If you connect as soon as the socket is listening you'll get "bitcoin is starting" errors returned. The gain here over simply retrying is that you can start connecting only when it's actually ready.

(And yes, true you could just sit in a retry loop, but you could also poll for all the other notifys and that didn't stop us from adding them. :) )

@kristapsk
Copy link
Contributor

Startup services can't trigger when bitcoin is ready to be actually used. That is the big reason for a notify here.

Didn't thought about that. Yes, then it makes sense, concept ACK from me too.

@benthecarman
Copy link
Contributor Author

Moved function call to very end of init sequence

@DrahtBot
Copy link
Contributor

DrahtBot commented Feb 11, 2019

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

Conflicts

Reviewers, this pull request conflicts with the following ones:

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.

@promag
Copy link
Contributor

promag commented Feb 11, 2019

Still not convinced about this. I think the general approach is to poll the interface of interest.

Docker, for instance, supports a directive to check if a container is healthy https://docs.docker.com/v17.09/engine/reference/builder/#healthcheck

See also https://docs.docker.com/compose/startup-order/

Anyway, what if you want to trigger some command when both bitcoind and other service are up and available? You would have to poll the second. Take your example, what if you want to run something else after bitcoind and lnd are up?

Also, from the client point of view, I think it's a good practice to have retry mechanisms, no because of the startup, but because in runtime a service can be unavailable for some reason.

@abitfan
Copy link
Contributor

abitfan commented Feb 25, 2019

Electrum (and most likely lnd as well) already have a retry mechanism for rpc and the rpc can be unavailable for other reasons as well so this doesn't add much functionality.
You can watch debug.log for "init message: Done loading" or setup a rawtx zmq publisher and consider bitcoind started when the first notification comes in.

Not specific to this pr but running random commands is not a good security practice and can lead to disasters like privilege escalation. Consider the scenario where someone out of hurry and not wanting to deal with file permissions puts his notifywhatever script in a world writable place and forgets about it only to have his wallet flushed some months later.

@benthecarman
Copy link
Contributor Author

Consider the scenario where someone out of hurry and not wanting to deal with file permissions puts his notifywhatever script in a world writable place and forgets about it only to have his wallet flushed some months later.

While that is true the same could be done with the other notify options, which are ran much more often.

Copy link
Member

@luke-jr luke-jr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK

@maflcko
Copy link
Member

maflcko commented Aug 26, 2020

Concept ACK

@benthecarman
Copy link
Contributor Author

Rebased and added #if HAVE_SYSTEM around the config option and function definition, similar to the other notify options

Copy link
Contributor

@kristapsk kristapsk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 962d730, I have tested the code (on Linux).

Copy link
Member

@maflcko maflcko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

review ACK 962d730

@benthecarman
Copy link
Contributor Author

Incorporated @MarcoFalke's suggestions

@maflcko maflcko added this to the 0.21.0 milestone Sep 28, 2020
@maflcko
Copy link
Member

maflcko commented Sep 28, 2020

re-ACK 090530c

@dongcarl or @kristapsk would be good to get another re-ACK or so, so that this can be merged into the upcoming major release.

@dongcarl
Copy link
Contributor

tACK 090530c

@maflcko maflcko merged commit 2552702 into bitcoin:master Sep 28, 2020
@benthecarman benthecarman deleted the startup_commands branch September 28, 2020 18:48
@laanwj
Copy link
Member

laanwj commented Sep 28, 2020

Posthumous ACK

@Kixunil
Copy link

Kixunil commented Jan 19, 2021

Thank you all for working on this! Very helpful to me and came in pretty much exactly when needed. And thanks @dongcarl for pointers - didn't know about systemd-notify command.

One more tip for other readers: systemd-notify doesn't seem to work if launched from within a script that is executed by startupnotify=/path/to/somescript.sh. That's no problem startupnotify=systemd-notify --ready; /path/to/somescript.sh
works fine.

I have some trouble with this though: it occasionally times out even when timeout is 10 minutes. Is it possible for bitcoind to start more than 10 minutes (seems quite long to me) or perhaps this doesn't trigger in some cases? I tried several tests using regtest in a VM with decent host HW.

I will try with even longer timeouts but perhaps this is an indication there's something wrong with bitcoind?

It seems to get stuck on message: Adding fixed seed nodes as DNS doesn't seem to be available.

The problem was solved, I actually had overloaded system.

@Kixunil

This comment has been minimized.

whitslack added a commit to whitslack/bitcoin that referenced this pull request Jun 27, 2021
When other initscripts depend on bitcoind, it's because their daemons
want to be able to invoke bitcoin-cli or to communicate with bitcoind
via RPC. That can't happen until some time after bitcoind has forked
into the background. The -startupnotify option was added in 090530c
(bitcoin#15367) to support exactly this use case, so let's use it.

The ideal OpenRC mechanism of marking the service inactive at startup
and later calling back into the start() function with IN_BACKGROUND=yes
won't work here because bitcoind runs as an unprivileged user that
is not allowed to call "rc-service ${SVCNAME} start", and OpenRC has no
mechanism like systemd-notify, so instead we make start-stop-daemon
lock a startup dummy file with flock(1) before exec'ing bitcoind, and
then we use -startupnotify to release the lock when bitcoind is ready
to service RPC requests. In the initscript's start_post() function we
mark the service as inactive and fork a subshell process into the
background to wait for bitcoind to release the lock file, which will
happen when bitcoind either executes the -startupnotify command or
dies. After acquiring and releasing the lock, the subshell checks
whether the pid file still exists and marks the service as started or
stopped appropriately.
whitslack added a commit to whitslack/bitcoin that referenced this pull request Jun 27, 2021
When other initscripts depend on bitcoind, it's because their daemons
want to be able to invoke bitcoin-cli or to communicate with bitcoind
via RPC. That can't happen until some time after bitcoind has forked
into the background. The -startupnotify option was added in 090530c
(bitcoin#15367) to support exactly this use case, so let's use it.

A straightforward and reliable way to wait for bitcoind to invoke its
-startupnotify command is to make bitcoind hold a lock on a dummy file
and release that lock via the -startupnotify command. The initscript's
start_post() function initially marks the service as inactive and forks
a subshell process into the background to wait for bitcoind to release
the lock file, which will happen when bitcoind either executes the
-startupnotify command or dies. After acquiring and releasing the lock,
the subshell checks whether the pid file still exists and marks the
service as started or stopped appropriately.
deadalnix pushed a commit to Bitcoin-ABC/bitcoin-abc that referenced this pull request Oct 14, 2021
Summary:
> Thoughts for adding the feature is for users to be able to add things like electrum-personal-server or lnd to run whenever Bitcoin Core is running. Open to feedback about the feature.

This is a backport of [[bitcoin/bitcoin#15367 | core#15367]]

Test Plan: `ninja && src/bitcoind -startupnotify="notify-send 'bitcoind started' 'test message'"`

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D10346
@ghost ghost mentioned this pull request Oct 31, 2021
PiRK pushed a commit to PiRK/lotusd that referenced this pull request Aug 16, 2022
Summary:
> Thoughts for adding the feature is for users to be able to add things like electrum-personal-server or lnd to run whenever Bitcoin Core is running. Open to feedback about the feature.

This is a backport of [[bitcoin/bitcoin#15367 | core#15367]]

Test Plan: `ninja && src/bitcoind -startupnotify="notify-send 'bitcoind started' 'test message'"`

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D10346
@bitcoin bitcoin locked and limited conversation to collaborators Oct 30, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.