Skip to content

Commit

Permalink
bitcoind backend: assign port at start time, not init.
Browse files Browse the repository at this point in the history
I found a similar race in our core-lightning CI: when a node restarts,
there's a race where another node could start on the same port.

In this case, we catch the exception, which was covering up the failure.
The test then talks to the wrong bitcoind, which has unpredictable results.

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed May 23, 2022
1 parent 9dc7097 commit cc020d4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
9 changes: 6 additions & 3 deletions lnprototest/backend/bitcoind.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,8 @@ def __init__(self, basedir: str):
"-logtimestamps",
"-nolisten",
]
self.port = reserve()
self.btc_version = None
logging.debug("Port is {}, dir is {}".format(self.port, self.bitcoin_dir))
logging.debug("dir is {}".format(self.bitcoin_dir))

def __init_bitcoin_conf(self):
"""Init the bitcoin core directory with all the necessary information
Expand Down Expand Up @@ -124,6 +123,10 @@ def __is__bitcoind_ready(self) -> bool:
return True

def start(self) -> None:
# Now we're about to start, we can allocate the port.
# We used to do this at init, but in parallel cases another
# bitcoin could use the port when we restart!
self.port = reserve()
if self.rpc is None:
self.__init_bitcoin_conf()
# TODO: We can move this to a single call and not use Popen
Expand All @@ -148,7 +151,7 @@ def stop(self) -> None:
shutil.rmtree(os.path.join(self.bitcoin_dir, "regtest"))

def restart(self) -> None:
# Only restart if we have to.
# Only restart if we have to: may change port!
if self.rpc.getblockcount() != 101 or self.rpc.getrawmempool() != []:
self.stop()
self.start()
6 changes: 1 addition & 5 deletions lnprototest/clightning/clightning.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,7 @@ def start(self) -> None:
self.__init_sandbox_dir()
if self.bitcoind is None:
self.bitcoind = Bitcoind(self.directory)
try:
self.bitcoind.start()
except Exception as ex:
self.logger.debug(f"Exception with message {ex}")
self.logger.debug("RUN Bitcoind")
self.bitcoind.start()
self.proc = subprocess.Popen(
[
"{}/lightningd/lightningd".format(LIGHTNING_SRC),
Expand Down

0 comments on commit cc020d4

Please sign in to comment.