From a5c117b542d21db94d16677663a84e7f352ffe73 Mon Sep 17 00:00:00 2001 From: Victor Shyba Date: Mon, 7 Sep 2020 17:17:51 -0300 Subject: [PATCH] wait for lbrycrd on start --- lbry/blockchain/lbrycrd.py | 2 ++ lbry/blockchain/sync/synchronizer.py | 12 +++++++++++- tests/integration/blockchain/test_blockchain.py | 8 ++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/lbry/blockchain/lbrycrd.py b/lbry/blockchain/lbrycrd.py index 11aa79ba2..a43d5f600 100644 --- a/lbry/blockchain/lbrycrd.py +++ b/lbry/blockchain/lbrycrd.py @@ -243,6 +243,8 @@ class Lbrycrd: self.subscription = None async def rpc(self, method, params=None): + if self.session.closed: + raise Exception("session is closed! We are shutting down.") message = { "jsonrpc": "1.0", "id": "1", diff --git a/lbry/blockchain/sync/synchronizer.py b/lbry/blockchain/sync/synchronizer.py index 003b24efa..95a68aea2 100644 --- a/lbry/blockchain/sync/synchronizer.py +++ b/lbry/blockchain/sync/synchronizer.py @@ -43,9 +43,19 @@ class BlockchainSync(Sync): self.advance_loop_task: Optional[asyncio.Task] = None self.advance_loop_event = asyncio.Event() + async def wait_for_chain_ready(self): + while True: + try: + return await self.chain.ensure_subscribable() + except asyncio.CancelledError: + raise + except Exception as e: + log.warning("Blockchain not ready, waiting for it: %s", str(e)) + await asyncio.sleep(1) + async def start(self): self.db.stop_event.clear() - await self.chain.ensure_subscribable() + await self.wait_for_chain_ready() self.advance_loop_task = asyncio.create_task(self.advance()) await self.advance_loop_task await self.chain.subscribe() diff --git a/tests/integration/blockchain/test_blockchain.py b/tests/integration/blockchain/test_blockchain.py index 7804dfc34..a706e079e 100644 --- a/tests/integration/blockchain/test_blockchain.py +++ b/tests/integration/blockchain/test_blockchain.py @@ -613,6 +613,14 @@ class TestMultiBlockFileSyncing(BasicBlockchainTestCase): class TestGeneralBlockchainSync(SyncingBlockchainTestCase): + async def test_sync_waits_for_lbrycrd_to_start(self): + await self.sync.stop() + await self.chain.stop() + sync_start = asyncio.ensure_future(self.sync.start()) + await asyncio.sleep(0) + await self.chain.start() + await sync_start + self.assertTrue(sync_start.done()) # test goal is to get here without exceptions async def test_sync_advances(self): blocks = []