From 2d96dbb3a7329660df68674314d42995b375d6f1 Mon Sep 17 00:00:00 2001 From: Alex Sharov Date: Sat, 13 Jul 2024 15:25:34 +0700 Subject: [PATCH] diag: thread-safety step2 - unlock mutex in defer (#11135) --- erigon-lib/diagnostics/snapshots.go | 41 +++++++++++++++-------------- erigon-lib/diagnostics/speedtest.go | 2 +- erigon-lib/diagnostics/sys_info.go | 7 +++-- 3 files changed, 25 insertions(+), 25 deletions(-) diff --git a/erigon-lib/diagnostics/snapshots.go b/erigon-lib/diagnostics/snapshots.go index 4e453092afe..0f1d56b97f0 100644 --- a/erigon-lib/diagnostics/snapshots.go +++ b/erigon-lib/diagnostics/snapshots.go @@ -116,6 +116,26 @@ func (d *DiagnosticClient) updateSnapshotStageStats(stats SyncStageStats, subSta d.syncStages[idxs.Stage].SubStages[idxs.SubStage].Stats = stats } +func (d *DiagnosticClient) saveSnapshotStageStatsToDB() { + d.mu.Lock() + defer d.mu.Unlock() + err := d.db.Update(d.ctx, func(tx kv.RwTx) error { + err := SnapshotFillDBUpdater(d.syncStats.SnapshotFillDB)(tx) + if err != nil { + return err + } + + err = StagesListUpdater(d.syncStages)(tx) + if err != nil { + return err + } + + return nil + }) + if err != nil { + log.Debug("[Diagnostics] Failed to update snapshot download info", "err", err) + } +} func (d *DiagnosticClient) runSegmentDownloadingListener(rootCtx context.Context) { go func() { @@ -384,26 +404,7 @@ func (d *DiagnosticClient) runFillDBListener(rootCtx context.Context) { TimeLeft: "unknown", Progress: fmt.Sprintf("%d%%", (info.Stage.Current*100)/info.Stage.Total), }, "Fill DB from snapshots") - - d.mu.Lock() - err := d.db.Update(d.ctx, func(tx kv.RwTx) error { - err := SnapshotFillDBUpdater(d.syncStats.SnapshotFillDB)(tx) - if err != nil { - return err - } - - err = StagesListUpdater(d.syncStages)(tx) - if err != nil { - return err - } - - return nil - }) - - if err != nil { - log.Warn("[Diagnostics] Failed to update snapshot download info", "err", err) - } - d.mu.Unlock() + d.saveSnapshotStageStatsToDB() } } }() diff --git a/erigon-lib/diagnostics/speedtest.go b/erigon-lib/diagnostics/speedtest.go index 522f5132fd5..9be466eae18 100644 --- a/erigon-lib/diagnostics/speedtest.go +++ b/erigon-lib/diagnostics/speedtest.go @@ -31,8 +31,8 @@ func (d *DiagnosticClient) setupSpeedtestDiagnostics(rootCtx context.Context) { go func() { if d.speedTest { d.networkSpeedMutex.Lock() + defer d.networkSpeedMutex.Unlock() d.networkSpeed = d.runSpeedTest(rootCtx) - d.networkSpeedMutex.Unlock() } }() } diff --git a/erigon-lib/diagnostics/sys_info.go b/erigon-lib/diagnostics/sys_info.go index 95a94db884f..08cf6145395 100644 --- a/erigon-lib/diagnostics/sys_info.go +++ b/erigon-lib/diagnostics/sys_info.go @@ -37,6 +37,9 @@ var ( ) func (d *DiagnosticClient) setupSysInfoDiagnostics() { + d.mu.Lock() + defer d.mu.Unlock() + sysInfo := GetSysInfo(d.dataDirPath) var funcs []func(tx kv.RwTx) error @@ -52,14 +55,10 @@ func (d *DiagnosticClient) setupSysInfoDiagnostics() { return nil }) - if err != nil { log.Warn("[Diagnostics] Failed to update system info", "err", err) } - - d.mu.Lock() d.hardwareInfo = sysInfo - d.mu.Unlock() } func (d *DiagnosticClient) HardwareInfoJson(w io.Writer) {