From c2abb939d6e49d3bd20eb19ec961b62825dcadbe Mon Sep 17 00:00:00 2001 From: Adam Gibson Date: Sat, 18 Jun 2022 10:50:08 -0600 Subject: [PATCH] Ensure OnionDirectoryPeerNotFound is raised Fixes #1306. Prior to this commit, it was possible (unlikely) for a peer to exist in the active_directories nick in OnionMessageChannel, but to have False entries (i.e. disconnected) for every directory, meaning that the random choice from those directories raised an Exception which was not caught, causing a crash. This is now fixed by checking whether the list of directories to be chosen from randomly, is empty, and if so raising the correct Exception type, namely OnionDirectoryPeerNotFound. --- jmdaemon/jmdaemon/onionmc.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/jmdaemon/jmdaemon/onionmc.py b/jmdaemon/jmdaemon/onionmc.py index 86e4ac1..78354e5 100644 --- a/jmdaemon/jmdaemon/onionmc.py +++ b/jmdaemon/jmdaemon/onionmc.py @@ -1012,7 +1012,10 @@ class OnionMessageChannel(MessageChannel): adn = self.active_directories[nick] if len(adn) == 0: raise OnionDirectoryPeerNotFound - return random.choice([x for x in list(adn) if adn[x] is True]) + candidates = [x for x in list(adn) if adn[x] is True] + if len(candidates) == 0: + raise OnionDirectoryPeerNotFound + return random.choice(candidates) def forward_pubmsg_to_peers(self, msg: str, from_nick: str) -> None: """ Used by directory nodes currently. Takes a received