URI: 
       tchanneldb.load_data: attempt at fixing race - electrum - Electrum Bitcoin wallet
  HTML git clone https://git.parazyd.org/electrum
   DIR Log
   DIR Files
   DIR Refs
   DIR Submodules
       ---
   DIR commit edf186da0d1414b444514211898344a70117a2c7
   DIR parent 4f741cfcccef00dae94e571de57d914801601d5e
  HTML Author: SomberNight <somber.night@protonmail.com>
       Date:   Thu,  5 Sep 2019 18:32:45 +0200
       
       channeldb.load_data: attempt at fixing race
       
       closes #5525
       
       Diffstat:
         M electrum/channel_db.py              |       4 ++++
         M electrum/lnpeer.py                  |       1 +
         M electrum/lnworker.py                |       1 +
       
       3 files changed, 6 insertions(+), 0 deletions(-)
       ---
   DIR diff --git a/electrum/channel_db.py b/electrum/channel_db.py
       t@@ -30,6 +30,7 @@ from collections import defaultdict
        from typing import Sequence, List, Tuple, Optional, Dict, NamedTuple, TYPE_CHECKING, Set
        import binascii
        import base64
       +import asyncio
        
        
        from .sql_db import SqlDB, sql
       t@@ -250,6 +251,7 @@ class ChannelDB(SqlDB):
                self._nodes = {}
                self._addresses = defaultdict(set)
                self._channels_for_node = defaultdict(set)
       +        self.data_loaded = asyncio.Event()
        
            def update_counts(self):
                self.num_channels = len(self._channels)
       t@@ -278,6 +280,7 @@ class ChannelDB(SqlDB):
                return LNPeerAddr(host, port, node_id)
        
            def get_recent_peers(self):
       +        assert self.data_loaded.is_set(), "channelDB load_data did not finish yet!"
                r = [self.get_last_good_address(x) for x in self._addresses.keys()]
                r = r[-self.NUM_MAX_RECENT_PEERS:]
                return r
       t@@ -546,6 +549,7 @@ class ChannelDB(SqlDB):
                self.logger.info(f'load data {len(self._channels)} {len(self._policies)} {len(self._channels_for_node)}')
                self.update_counts()
                self.count_incomplete_channels()
       +        self.data_loaded.set()
        
            def count_incomplete_channels(self):
                out = set()
   DIR diff --git a/electrum/lnpeer.py b/electrum/lnpeer.py
       t@@ -241,6 +241,7 @@ class Peer(Logger):
                    await group.spawn(self.process_gossip())
        
            async def process_gossip(self):
       +        await self.channel_db.data_loaded.wait()
                # verify in peer's TaskGroup so that we fail the connection
                while True:
                    await asyncio.sleep(5)
   DIR diff --git a/electrum/lnworker.py b/electrum/lnworker.py
       t@@ -154,6 +154,7 @@ class LNWorker(Logger):
        
            async def _get_next_peers_to_try(self) -> Sequence[LNPeerAddr]:
                now = time.time()
       +        await self.channel_db.data_loaded.wait()
                recent_peers = self.channel_db.get_recent_peers()
                # maintenance for last tried times
                # due to this, below we can just test membership in _last_tried_peer