@@ -523,7 +523,11 @@ async def wrapper_func(self, *args, **kwargs):
523523 @handle_disconnect
524524 async def main_loop (self ):
525525 async with self .taskgroup as group :
526- await group .spawn (self ._message_loop ())
526+ await group .spawn (self ._message_loop ()) # initializes connection
527+ try :
528+ await util .wait_for2 (self .initialized , LN_P2P_NETWORK_TIMEOUT )
529+ except Exception as e :
530+ raise GracefulDisconnect (f"Failed to initialize: { e !r} " ) from e
527531 await group .spawn (self ._query_gossip ())
528532 await group .spawn (self ._process_gossip ())
529533 await group .spawn (self ._send_own_gossip ())
@@ -563,6 +567,7 @@ async def _process_gossip(self):
563567 async def _send_own_gossip (self ):
564568 if self .lnworker == self .lnworker .network .lngossip :
565569 return
570+ assert self .is_initialized ()
566571 await asyncio .sleep (10 )
567572 while True :
568573 public_channels = [chan for chan in self .lnworker .channels .values () if chan .is_public ()]
@@ -583,6 +588,7 @@ def _should_forward_gossip(self) -> bool:
583588 return False
584589
585590 async def _forward_gossip (self ):
591+ assert self .is_initialized ()
586592 if not self ._should_forward_gossip ():
587593 return
588594
@@ -632,10 +638,7 @@ async def _send_gossip_messages(self, messages: List[GossipForwardingMessage]) -
632638 return amount_sent
633639
634640 async def _query_gossip (self ):
635- try :
636- await util .wait_for2 (self .initialized , LN_P2P_NETWORK_TIMEOUT )
637- except Exception as e :
638- raise GracefulDisconnect (f"Failed to initialize: { e !r} " ) from e
641+ assert self .is_initialized ()
639642 if self .lnworker == self .lnworker .network .lngossip :
640643 if not self .their_features .supports (LnFeatures .GOSSIP_QUERIES_OPT ):
641644 raise GracefulDisconnect ("remote does not support gossip_queries, which we need" )
0 commit comments