From ad7a519284eed218c2c9958c141f817c2abc9e93 Mon Sep 17 00:00:00 2001 From: Erik Arvstedt Date: Wed, 27 Nov 2019 14:04:46 +0100 Subject: [PATCH] bitcoind: wait until RPC port is open This fixes rare failures in clightning which requires an open bitcoind RPC port --- modules/bitcoind.nix | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/modules/bitcoind.nix b/modules/bitcoind.nix index eab5b40..cc073e1 100644 --- a/modules/bitcoind.nix +++ b/modules/bitcoind.nix @@ -19,7 +19,7 @@ let listen=${if cfg.listen then "1" else "0"} # RPC server options - ${optionalString (cfg.rpc.port != null) "rpcport=${toString cfg.rpc.port}"} + rpcport=${toString cfg.rpc.port} ${concatMapStringsSep "\n" (rpcUser: "rpcauth=${rpcUser.name}:${rpcUser.passwordHMAC}") (attrValues cfg.rpc.users) @@ -108,9 +108,9 @@ in { rpc = { port = mkOption { - type = types.nullOr types.ints.u16; - default = null; - description = "Override the default port on which to listen for JSON-RPC connections."; + type = types.ints.u16; + default = 8332; + description = "Port on which to listen for JSON-RPC connections."; }; users = mkOption { default = {}; @@ -241,6 +241,12 @@ in { echo "rpcpassword=$(cat /secrets/bitcoin-rpcpassword)" >> '${cfg.dataDir}/bitcoin.conf' chmod -R g+rX '${cfg.dataDir}/blocks' ''; + # Wait until RPC port is open. This usually takes just a few ms. + postStart = '' + while ! { exec 3>/dev/tcp/127.0.0.1/${toString cfg.rpc.port}; } &>/dev/null; do + sleep 0.05 + done + ''; serviceConfig = { Type = "simple"; User = "${cfg.user}";