From a1aa9ebb022ff335a576149da46720bcc0360686 Mon Sep 17 00:00:00 2001 From: davotoula Date: Thu, 14 May 2026 20:20:56 +0200 Subject: [PATCH] fix(account): break a-tag cycle in computeRelayListToBroadcast MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit An addressable event whose `a` tags reach itself — directly (self-mention) or via any cycle of other in-cache events — would recurse unbounded through `AddressHintProvider` / `EventHintProvider` branches and crash with StackOverflowError on publish (`signAndComputeBroadcast` → `computeRelayListToBroadcast`). The crash was reproducible on the device when publishing a kind-1 note that referenced an article whose own event carries a self `a` tag (e.g. YakiHonne-authored long-form posts include their own naddr in their `a` tags). --- .../com/vitorpamplona/amethyst/model/Account.kt | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/Account.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/Account.kt index 251b0e3fe..971da63e2 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/Account.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/Account.kt @@ -965,7 +965,15 @@ class Account( return true } - fun computeRelayListToBroadcast(event: Event): Set { + fun computeRelayListToBroadcast(event: Event): Set = computeRelayListToBroadcast(event, mutableSetOf()) + + private fun computeRelayListToBroadcast( + event: Event, + visited: MutableSet, + ): Set { + // a-tagged events can form cycles; without this the two recursive descents stack-overflow. + if (!visited.add(event.id)) return emptySet() + if (event is GiftWrapEvent) { val receiver = event.recipientPubKey() return if (receiver != null) { @@ -1044,7 +1052,7 @@ class Account( } linkedNote.event?.let { linkedEvent -> - relayList.addAll(computeRelayListToBroadcast(linkedEvent)) + relayList.addAll(computeRelayListToBroadcast(linkedEvent, visited)) } } } @@ -1065,7 +1073,7 @@ class Account( } linkedNote.event?.let { linkedEvent -> - relayList.addAll(computeRelayListToBroadcast(linkedEvent)) + relayList.addAll(computeRelayListToBroadcast(linkedEvent, visited)) } } }