Skip to content

Commit a5cf5f7

Browse files
committed
lnpeer: await init in main_loop
Because `LNPeer.initialized` was awaited in `LNPeer._query_gossip()` instead of the main loop the other tasks got spawned concurrently and each task on its own has to wait for the initialization. In `LNPeer._send_own_gossip()` this was missing, instead there is a fixed 10 sec sleep. If the connection was not initialized but the 10 sec are exceeded `_send_own_gossip()` tries to send gossip and causes this exception as the `LNTransport` is not ready: ``` 2.13 | E | lnpeer.Peer.[LNWallet, 0288fa27c0-bc1900c8] | Exception in main_loop: AttributeError("'LNTransport' object has no attribute 'sk'") Traceback (most recent call last): File "/home/user/code/electrum-fork/electrum/util.py", line 1232, in wrapper return await func(*args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/user/code/electrum-fork/electrum/lnpeer.py", line 511, in wrapper_func return await func(self, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/user/code/electrum-fork/electrum/lnpeer.py", line 525, in main_loop async with self.taskgroup as group: ^^^^^^^^^^^^^^ File "/home/user/code/electrum-fork/env/lib/python3.14/site-packages/aiorpcx/curio.py", line 304, in __aexit__ await self.join() File "/home/user/code/electrum-fork/electrum/util.py", line 1420, in join task.result() ~~~~~~~~~~~^^ File "/home/user/code/electrum-fork/electrum/lnpeer.py", line 573, in _send_own_gossip self.send_node_announcement(alias, color) ~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^ File "/home/user/code/electrum-fork/electrum/lnpeer.py", line 1830, in send_node_announcement self.transport.send_bytes(raw_msg) ~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^ File "/home/user/code/electrum-fork/electrum/lntransport.py", line 225, in send_bytes lc = aead_encrypt(self.sk, self.sn(), b'', l) ^^^^^^^ AttributeError: 'LNTransport' object has no attribute 'sk'. Did you mean: 'sn'? ``` By awaiting the initialization directly in the `main_loop` it is more clear that the task getting spawned subsequently depend on the transport being available and separates the initialization more clearly these other functions.
1 parent d379a66 commit a5cf5f7

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

electrum/lnpeer.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)