From 046609c5d2ef16701fc9d5b8153cb57070dbb808 Mon Sep 17 00:00:00 2001 From: SomberNight Date: Sun, 5 Feb 2023 22:49:12 +0000 Subject: [PATCH] lnpeer: add note about thread-safety, and some checks I was calling methods from the Qt console (e.g. peer.pay()) and seeing weird behaviour... htlc_switch() (running on asyncio thread) was racing with pay() (running on GUI thread). --- electrum/lnpeer.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/electrum/lnpeer.py b/electrum/lnpeer.py index d1151441b..24e4c3de4 100644 --- a/electrum/lnpeer.py +++ b/electrum/lnpeer.py @@ -63,6 +63,8 @@ LN_P2P_NETWORK_TIMEOUT = 20 class Peer(Logger): + # note: in general this class is NOT thread-safe. Most methods are assumed to be running on asyncio thread. + LOGGING_SHORTCUT = 'P' ORDERED_MESSAGES = ( @@ -120,6 +122,7 @@ class Peer(Logger): self.downstream_htlc_resolved_event = asyncio.Event() def send_message(self, message_name: str, **kwargs): + assert util.get_running_loop() == util.get_asyncio_loop(), f"this must be run on the asyncio thread!" assert type(message_name) is str if message_name not in self.SPAMMY_MESSAGES: self.logger.debug(f"Sending {message_name.upper()}") @@ -1421,6 +1424,7 @@ class Peer(Logger): self.maybe_send_commitment(chan) def maybe_send_commitment(self, chan: Channel) -> bool: + assert util.get_running_loop() == util.get_asyncio_loop(), f"this must be run on the asyncio thread!" # REMOTE should revoke first before we can sign a new ctx if chan.hm.is_revack_pending(REMOTE): return False