From b4facf04be9c24182a037ad56ba979851b0c55bc Mon Sep 17 00:00:00 2001 From: mikael-lovqvists-claude-agent Date: Mon, 30 Mar 2026 04:29:50 +0000 Subject: [PATCH] Fix multicast storm: only re-announce on new peer, not every received packet The announce thread was being woken (triggering a multicast send) on every received announcement packet, including ones from already-known peers. With two or more nodes this created a feedback loop: each incoming packet triggered an outbound multicast which triggered another incoming packet on the peer, and so on at full CPU/network speed. Gate the cond_signal on is_new so we only fast-announce when a genuinely new peer is seen. The periodic interval-based announcement continues to handle keepalives and reconnections for existing peers. Co-Authored-By: Claude Sonnet 4.6 --- src/modules/discovery/discovery.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/modules/discovery/discovery.c b/src/modules/discovery/discovery.c index 787dea8..1b0170d 100644 --- a/src/modules/discovery/discovery.c +++ b/src/modules/discovery/discovery.c @@ -233,11 +233,13 @@ static void *receive_thread_fn(void *arg) { pthread_mutex_unlock(&d->peers_mutex); - /* respond to every announcement — the sender may be a fresh instance - * that doesn't know about us yet even if we already have it in our table */ - pthread_mutex_lock(&d->announce_mutex); - pthread_cond_signal(&d->announce_cond); - pthread_mutex_unlock(&d->announce_mutex); + if (is_new) { + /* announce ourselves immediately so the new peer learns about us + * without waiting up to interval_ms */ + pthread_mutex_lock(&d->announce_mutex); + pthread_cond_signal(&d->announce_cond); + pthread_mutex_unlock(&d->announce_mutex); + } if (is_new && d->config.on_peer_found) { d->config.on_peer_found(&peer_copy, d->config.userdata);