From 5ec4a4dab8297fed0f8f7a477c7025d28a580db8 Mon Sep 17 00:00:00 2001 From: Eugene Burkov Date: Thu, 20 Jan 2022 17:19:09 +0300 Subject: [PATCH] Pull request: 4142 stats panic Merge in DNS/adguard-home from 4142-stats-panic to master Updates #4142. Squashed commit of the following: commit bf168f50ac86bdfdab73bf7285705f09f87b6c72 Author: Eugene Burkov Date: Thu Jan 20 17:13:41 2022 +0300 stats: imp more commit bb638211da7d0c51959ded2dacb72faea00befb4 Author: Eugene Burkov Date: Thu Jan 20 17:09:31 2022 +0300 stats: imp code quality commit 27ac52f15e4e0f4112ce7a6b47b03f963463393e Author: Eugene Burkov Date: Thu Jan 20 17:00:09 2022 +0300 stats: recover panic on init commit 1ffcebbb9062438170b010e1c7bad3c6cef4cfc1 Author: Eugene Burkov Date: Thu Jan 20 14:19:01 2022 +0300 all: fix some typos --- CHANGELOG.md | 2 +- internal/aghalg/aghalg.go | 2 +- internal/home/controlinstall.go | 2 +- internal/home/dns.go | 2 +- internal/stats/unit.go | 22 ++++++++++++++++++++++ 5 files changed, 26 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7410cd93..8c9e5b4b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -51,7 +51,7 @@ TODO(a.garipov): Remove this deprecation, if v0.108.0 is released before the Go diff --git a/internal/aghalg/aghalg.go b/internal/aghalg/aghalg.go index c3b6e906..3a9b07db 100644 --- a/internal/aghalg/aghalg.go +++ b/internal/aghalg/aghalg.go @@ -24,7 +24,7 @@ func (uc UniqChecker) Add(elems ...comparable) { } } -// Merge returns a validator containing data from both v and other. +// Merge returns a checker containing data from both uc and other. func (uc UniqChecker) Merge(other UniqChecker) (merged UniqChecker) { merged = make(UniqChecker, len(uc)+len(other)) for elem, num := range uc { diff --git a/internal/home/controlinstall.go b/internal/home/controlinstall.go index b9c302ee..82598078 100644 --- a/internal/home/controlinstall.go +++ b/internal/home/controlinstall.go @@ -128,7 +128,7 @@ func (req *checkConfReq) validateWeb(uc aghalg.UniqChecker) (err error) { } // validateDNS returns error if the DNS part of the initial configuration can't -// be set. autofix is true if the port can be unbound by AdGuard Home +// be set. canAutofix is true if the port can be unbound by AdGuard Home // automatically. func (req *checkConfReq) validateDNS(uc aghalg.UniqChecker) (canAutofix bool, err error) { defer func() { err = errors.Annotate(err, "validating ports: %w") }() diff --git a/internal/home/dns.go b/internal/home/dns.go index dfd133eb..8775e152 100644 --- a/internal/home/dns.go +++ b/internal/home/dns.go @@ -54,7 +54,7 @@ func initDNSServer() (err error) { } Context.stats, err = stats.New(statsConf) if err != nil { - return fmt.Errorf("couldn't initialize statistics module") + return fmt.Errorf("init stats: %w", err) } conf := querylog.Config{ diff --git a/internal/stats/unit.go b/internal/stats/unit.go index 43119907..35d47a51 100644 --- a/internal/stats/unit.go +++ b/internal/stats/unit.go @@ -67,7 +67,29 @@ type unitDB struct { TimeAvg uint32 // usec } +// withRecovered turns the value recovered from panic if any into an error and +// combines it with the one pointed by orig. orig must be non-nil. +func withRecovered(orig *error) { + p := recover() + if p == nil { + return + } + + var err error + switch p := p.(type) { + case error: + err = fmt.Errorf("panic: %w", p) + default: + err = fmt.Errorf("panic: recovered value of type %[1]T: %[1]v", p) + } + + *orig = errors.WithDeferred(*orig, err) +} + +// createObject creates s from conf and properly initializes it. func createObject(conf Config) (s *statsCtx, err error) { + defer withRecovered(&err) + s = &statsCtx{ mu: &sync.Mutex{}, }