diff --git a/test/test_nodebuilder b/test/test_nodebuilder index 5c174bd37..7060c47e6 100755 --- a/test/test_nodebuilder +++ b/test/test_nodebuilder @@ -184,7 +184,6 @@ case "${TARGET_KERNEL}" in MINGW*) throw_error 'Windows is not supported.' ;; *) throw_error 'Your operating system is not supported.' ;; esac -bitcoind_pid_path="${bitcoin_data_directory}/bitcoind.pid" DOWNLOADED_NODEBUILDER='false' [ -f nodebuilder ] || { @@ -226,27 +225,40 @@ fi [ "${DOWNLOADED_NODEBUILDER:-false}" = 'true' ] && rm nodebuilder kill_tail_process -# Stop Bitcoin Core -if [ -f "${bitcoind_pid_path}" ]; then - read -r bitcoind_pid < "${bitcoind_pid_path}" - if command -v bitcoin-cli > /dev/null 2>&1; then - bitcoin-cli stop - bitcoind_stop_sleep_seconds=2 - bitcoind_stop_sleep_counter=0 - while [ -f "${bitcoind_pid_path}" ]; do - sleep "${bitcoind_stop_sleep_seconds}" - # TODO: debug intermittent stalled stop issue - # shellcheck disable=SC2009 - ps aux | grep "${bitcoind_pid}" - bitcoind_stop_sleep_counter=$((bitcoind_stop_sleep_counter + 1)) - if [ "$((bitcoind_stop_sleep_seconds * bitcoind_stop_sleep_counter))" -ge 3600 ]; then - throw_error 'Stopping Bitcoin Core took over an hour.' - fi - done - else - throw_error 'Unable to find bitcoin-cli in PATH.' - fi -fi +BITCOIN_PID_PATH="${bitcoin_data_directory}/bitcoind.pid" +readonly BITCOIN_PID_PATH + +[ -f "${BITCOIN_PID_PATH}" ] || { + sudo find / -name "bitcoind.pid" -type f 2> /dev/null + throw_error "Unable to find $(basename "${BITCOIN_PID_PATH}") in data directory." +} +read -r bitcoin_pid < "${BITCOIN_PID_PATH}" +readonly bitcoin_pid + +BITCOIN_STOP_SLEEP_SECONDS=2 +readonly BITCOIN_STOP_SLEEP_SECONDS +BITCOIN_STOP_SLEEP_MAXIMUM_SECONDS=3600 +readonly BITCOIN_STOP_SLEEP_MAXIMUM_SECONDS +bitcoin_stop_sleep_elapsed=0 + +command -v bitcoin-cli > /dev/null 2>&1 || + throw_error 'Unable to find bitcoin-cli in PATH.' +bitcoin_pname="$(ps -p "${bitcoin_pid}" -o comm=)" || + throw_error "Found bitcoind.pid but cannot find running process with PID ${bitcoin_pid}." +bitcoin-cli stop + +while [ "${bitcoin_pname}" = 'bitcoind' ] || [ "${bitcoin_pname}" = 'bitcoin-qt' ]; do + sleep "${BITCOIN_STOP_SLEEP_SECONDS}" + bitcoin_stop_sleep_elapsed="$((bitcoin_stop_sleep_elapsed + BITCOIN_STOP_SLEEP_SECONDS))" + # TODO: debug intermittent stalled stop issue + # ps aux shouldnt be used for scripting, per SC2009), + # but it's fine here since we're just printing to stdout for debug purposes + # shellcheck disable=SC2009 + ps aux | grep "${bitcoin_pid}" + [ "${bitcoin_stop_sleep_elapsed}" -ge "${BITCOIN_STOP_SLEEP_MAXIMUM_SECONDS}" ] && + throw_error 'Stopping Bitcoin Core took over an hour.' + bitcoin_pname="$(ps -p "${bitcoin_pid}" -o comm=)" || bitcoin_pname='' +done if [ -s "${STDERR_TEST_FILENAME}" ]; then printf '%s\n' 'Printing the contents of stderr:'