From f04c21ec17fde4289051bfd573ebf7d58dbe18b2 Mon Sep 17 00:00:00 2001 From: nobswebdev Date: Fri, 16 Oct 2026 23:30:04 +0200 Subject: [PATCH] Fix Electrum daemon recovery and shutdown in Docker. Clear stale locks on startup via electrum stop, use correct stop on SIGTERM, and keep the daemon alive with wait so graceful restarts do not leave a lockfile crash loop. --- electrum-daemon/docker-entrypoint.sh | 37 ++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/electrum-daemon/docker-entrypoint.sh b/electrum-daemon/docker-entrypoint.sh index f96ec27..4862597 100755 --- a/electrum-daemon/docker-entrypoint.sh +++ b/electrum-daemon/docker-entrypoint.sh @@ -4,10 +4,16 @@ set -euo pipefail NETWORK_FLAG="--${ELECTRUM_NETWORK}" WALLET_PATH="/electrum/wallet/${ELECTRUM_WALLET_NAME}" DATA_DIR="/electrum/data" +LOAD_WALLET_MAX_ATTEMPTS=90 +DAEMON_STARTUP_WAIT_SECONDS=5 run_electrum() { electrum "${NETWORK_FLAG}" --dir "${DATA_DIR}" "$@"; } run_electrum_offline() { electrum --offline "${NETWORK_FLAG}" --dir "${DATA_DIR}" "$@"; } +maybe_stop_electrum() { + run_electrum stop 2>/dev/null || true +} + mkdir -p /electrum/wallet "${DATA_DIR}" chmod 700 /electrum/wallet @@ -16,19 +22,40 @@ if [[ ! -f "${WALLET_PATH}" ]]; then run_electrum_offline create -w "${WALLET_PATH}" --password "${ELECTRUM_WALLET_PASSWORD}" chmod 600 "${WALLET_PATH}" 2>/dev/null || true - + echo "Wallet created at ${WALLET_PATH}" fi +maybe_stop_electrum + run_electrum_offline setconfig rpcuser "${ELECTRUM_DAEMON_RPC_USER}" run_electrum_offline setconfig rpcpassword "${ELECTRUM_DAEMON_RPC_PASSWORD}" run_electrum_offline setconfig rpchost 0.0.0.0 run_electrum_offline setconfig rpcport "${ELECTRUM_DAEMON_PORT}" run_electrum_offline setconfig server "${ELECTRUM_SERVER}" -trap 'run_electrum daemon stop || true; exit 0' SIGTERM SIGINT +trap 'run_electrum stop || true' SIGTERM SIGINT -run_electrum daemon -d -run_electrum load_wallet -w "${WALLET_PATH}" --password "${ELECTRUM_WALLET_PASSWORD}" +echo "Starting Electrum daemon..." +run_electrum daemon & -sleep infinity +echo "Waiting for daemon connection (${DAEMON_STARTUP_WAIT_SECONDS}s)..." +sleep "${DAEMON_STARTUP_WAIT_SECONDS}" + +load_wallet_attempt=0 +echo "Loading wallet..." + +until run_electrum load_wallet -w "${WALLET_PATH}" --password "${ELECTRUM_WALLET_PASSWORD}"; do + load_wallet_attempt=$((load_wallet_attempt + 1)) + + if [[ load_wallet_attempt -ge LOAD_WALLET_MAX_ATTEMPTS ]]; then + echo "Timed out waiting to load wallet into Electrum daemon" >&2 + exit 1 + fi + + echo "Load wallet attempt ${load_wallet_attempt}/${LOAD_WALLET_MAX_ATTEMPTS} failed. Daemon may not be ready yet. Retrying in 1s..." + sleep 1 + echo "Loading wallet..." +done + +wait