From 2b47cb0656c84c8143b2a62ffc3cb6f258d0ea01 Mon Sep 17 00:00:00 2001 From: Deivid Soto Date: Fri, 5 Jun 2026 17:18:57 +0200 Subject: [PATCH] fix(torrent): suppress noisy UPnP AddPortMapping warnings The anacrolix/torrent client maps the listen port on the router via UPnP/NAT-PMP for inbound peers. Many home routers reject AddPortMapping with '500 Internal Server Error' and the lib retries every lease cycle, spamming the daemon log. The rejection is harmless (downloads work over DHT + outbound peers), so wrap the log handlers and drop just that line while keeping the mapping attempts for routers that do support it. --- internal/engine/torrent.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/internal/engine/torrent.go b/internal/engine/torrent.go index a0732bb..acd1838 100644 --- a/internal/engine/torrent.go +++ b/internal/engine/torrent.go @@ -23,6 +23,23 @@ import ( "golang.org/x/time/rate" ) +// portfwdFilterHandler wraps anacrolix/log handlers and drops the noisy +// UPnP/NAT-PMP port-mapping warnings (e.g. "error: AddPortMapping: 500 Internal +// Server Error") that home routers emit when they reject the mapping. Everything +// else passes through unchanged. +type portfwdFilterHandler struct { + inner []alog.Handler +} + +func (h portfwdFilterHandler) Handle(r alog.Record) { + if strings.Contains(r.Text(), "AddPortMapping") { + return + } + for _, inner := range h.inner { + inner.Handle(r) + } +} + var defaultTrackers = []string{ // Tier 1: ngosang/trackerslist "best" + newtrackon "stable" "udp://tracker.opentrackr.org:1337/announce", @@ -126,6 +143,16 @@ func NewTorrentDownloader(cfg TorrentConfig) (*TorrentDownloader, error) { tcfg.Seed = cfg.SeedEnabled tcfg.NoUpload = !cfg.SeedEnabled tcfg.Logger = alog.Default.FilterLevel(alog.Warning) + // Drop the noisy UPnP/NAT-PMP port-mapping warnings. The library attempts to + // map the listen port on the router for inbound peers (best-effort, only + // helps on routers that support it). Many home routers reject AddPortMapping + // with "500 Internal Server Error" and the lib retries on every lease cycle, + // spamming the log. The rejection is harmless (download works over DHT + + // outbound peers), so suppress just that line while keeping the attempts for + // routers that do support it. + tcfg.Logger.SetHandlers(portfwdFilterHandler{ + inner: append([]alog.Handler(nil), alog.Default.Handlers...), + }) // No browser-facing WebTorrent peer; daemon never seeds via WSS. tcfg.DisableWebtorrent = true